【问题标题】:Spring Integration Java DSL: strategy to handle errors / exceptions?Spring Integration Java DSL:处理错误/异常的策略?
【发布时间】:2017-11-15 04:39:44
【问题描述】:

如何处理 Java DSL 流中的错误?

假设我有一个写入 Rabbit(或数据库)的简单流程。

@Bean
public IntegrationFlow setupRabbitFlow() {
    return IntegrationFlows.from(publishSubscribeChannel)
            .handle((p, h) -> rabbitPublishActivator.publishToRabbit(p))
            .get();
}

由于数据库问题或中间连接失败,此类操作可能会导致错误。

如果在“publishToRabbit”步骤中发生某些异常,我如何增强对所采取操作的流程声明?

【问题讨论】:

  • 为什么不直接在publishToRabbit方法中记录异常?
  • 仅仅记录是不够的,我可能想调用一些逻辑稍后重新尝试发送。

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


【解决方案1】:

如果你谈论重试和类似的,你应该看看RequestHandlerRetryAdvice

另一方面,.handle() 有第二个参数 - 端点配置器。有你可以指定.advice():

.handle((GenericHandler<?>) (p, h) -> {
                    throw new RuntimeException("intentional");
                }, e -> e.advice(retryAdvice()))

    @Bean
    public RequestHandlerRetryAdvice retryAdvice() {
        RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
        requestHandlerRetryAdvice.setRecoveryCallback(new ErrorMessageSendingRecoverer(recoveryChannel()));
        return requestHandlerRetryAdvice;
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 2014-03-03
  • 2019-03-11
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多