Spring 集成中的异常处理示例

问题描述 投票:0回答:1

我正在寻找一个很好的 Spring 集成错误处理示例。我真正想做的就是当这个流程失败时调用一个方法。这个想法是被调用的方法将发出错误电子邮件。

   @Bean
    public IntegrationFlow sampleHasErrorFlow() {
        return IntegrationFlows.from(fileReadingMessageSource())
                .publishSubscribeChannel(s -> s
                        .subscribe(f -> f
                                .handle("sampleBean", "methodThatThrowsException")
                                .channel(filesProcessedChannel())))
               
                 .get();
    }

我只是想“捕获”methodThatThrowsException() 中抛出的异常。有人可以快速制作一个样品吗?

java spring spring-integration
1个回答
0
投票

不清楚为什么要使用那个

publishSubscribeChannel
,如果你只是在那里做一个
handle()
。像这样的东西:

  return IntegrationFlows.from(fileReadingMessageSource())
                   .handle("sampleBean", "methodThatThrowsException")
                   .channel(filesProcessedChannel())
                   .get();

完全一样。

因此,如果我们同意此配置,您可以查看端点配置器的

errorChannel()
选项来实现该
from(fileReadingMessageSource())
:

IntegrationFlows.from(fileReadingMessageSource(), e -> e.errorChannel()).

由于它是一个渠道,您只需使用

handle(Mail.outboundAdapter())
实现另一个流程即可根据您的要求生成电子邮件。

重点是 Spring Integration 只是重新抛出调用时的异常。对于源轮询通道适配器,它是一个计划任务,它有一个

try..catch
来向提到的
errorChannel
发送异常。

另一种方法是在

ExpressionEvaluatingRequestHandlerAdvice
的端点上使用
.handle("sampleBean", "methodThatThrowsException")
,但这有点复杂。

注意:

IntegrationFlows
很久以前就被弃用并删除了。因此,您可能使用不受支持的 Spring Integration 版本:https://spring.io/projects/spring-integration#support

在文档中查看有关错误处理的更多信息:https://docs.spring.io/spring-integration/reference/error-handling.html

© www.soinside.com 2019 - 2024. All rights reserved.