代码之家  ›  专栏  ›  技术社区  ›  ahelix

在Spring集成中为ContentEnricher定义异常回退行为

  •  2
  • ahelix  · 技术社区  · 7 年前

    我已经用Spring集成定义了一个很好的流,在标称情况下,它可以按我所希望的那样工作。

    但是,我还没有找到一种方法来定义处理错误的行为(即将输入行标记为失败)

    下面是我得到的错误:

     [ask-scheduler-2] cMessagingTemplate$TemporaryReplyChannel : Reply message received but the receiving thread has exited due to an exception while sending the request message:ErrorMessage [payload=org.springframework.messaging.MessageHandlingException: error occurred in message handler [outboundGateway]; nested exception is...
    

    以下是我的流程配置(简化):

    @Configuration
    @EnableIntegration
    public class IntegrationConfig {
    
    
        @Autowired
        private DataSource dataSource;
    
        @Bean
        public IntegrationFlow mainFlow() {
            //noinspection unchecked
            return IntegrationFlows.from(jdbcMessageSource(),
                    c -> c.poller(Pollers.fixedRate(5000)
                            .transactional(transactionManager())))
                    .split()
                    .enrich(e -> e
                            .requestChannel(subChannel())
                            .requestPayload(Message::getPayload)
                            .propertyExpression("fooId", "payload.id"))
                    .handle(barHandler())
                    .get();
        }
    
        @Bean
        public IntegrationFlow enricherFlow() {
            return IntegrationFlows.from(subChannel())
                    .handle(outboundGateway())
                    .get();
        }
    
        @Bean
        public MessageChannel subChannel() {
            return new DirectChannel();
        }
    
        @Bean
        public MessageSource<Object> jdbcMessageSource() {
            return new JdbcPollingChannelAdapter(this.dataSource,
                    "select * from FOO");
        }
    
        @Bean
        public JdbcOutboundGateway outboundGateway() {
            ...
        }
    
        @Bean
        public JdbcMessageHandler barHandler() {
            return new JdbcMessageHandler(dataSource,
                    "INSERT INTO BAR ...");
        }
    
    
        @Bean
        public PlatformTransactionManager transactionManager() {
            return new DataSourceTransactionManager(dataSource);
        }
    }
    

    我本以为我可以在默认的errorChannel上获得错误,并在那里执行必要的任务,但我还没有找到实现这一点的方法。

    非常感谢您的任何想法和帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Artem Bilan    7 年前

    你知道,看来我们错过了揭发 errorChannel 对于 EnricherSpec 在Java DSL中,可以随意提出 JIRA issue 关于这件事。但不管怎样,看起来我们可以访问该物业:

    .enrich(e -> {
                ContentEnricher contentEnricher =
                        e.requestChannel(subChannel())
                                .requestPayload(Message::getPayload)
                                .propertyExpression("fooId", "payload.id"))
                                .get().getT2();
                contentEnricher.setErrorChannel(enricherErrorChannel());
            })
    

    现在您可以添加任何 .handle() 流向那个 enricherErrorChannel() 并处理an ErrorMessage 当你钓鱼的时候。例如,通常 错误消息 包含 payload MessagingException 我们有一个 failedMessage 所有物这张照片中包含了关于你的原始照片的信息 有效载荷 headers 也:

    https://docs.spring.io/spring-integration/docs/5.0.0.RELEASE/reference/html/configuration.html#namespace-errorhandler