在Spring Integration中为ContentEnricher定义异常的回退行为

时间:2018-01-19 12:20:44

标签: java spring spring-integration spring-integration-dsl

我使用Spring Integration定义了一个很好的流程,在名义上它可以像我想要的那样工作。

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

这是我得到的错误:

 [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 个答案:

答案 0 :(得分:1)

您知道我们似乎错过了在Java DSL中为errorChannel公开EnricherSpec - 请随时就此问题提出JIRA issue。但无论如何,我们可以访问该属性:

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

现在,您可以向.handle()添加任意enricherErrorChannel()流,并在捕获时处理ErrorMessage。例如,ErrorMessage通常payload包含MessagingExceptionfailedMessage我们有payload属性。确切地说,其中包含有关原始headers function validarRut(textBox) { var resultado; $.ajax({ type: "POST", url: "AgendarInduccion.aspx/validarRut", data: "{rut:" + JSON.stringify(textBox) + "}", contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function(response){ alert("valido"); }, failure: function (response) { alert("ERROR"); } }); } 的信息:

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