处理具有直接通道的消息拆分器后的错误



我正在开发一项使用Spring Integration Java dsl发送电子邮件的服务。

我有一条批处理消息,该消息被拆分为单个消息的集合,这些消息将转换为电子邮件。

我遇到的问题是,如果这些单独的消息之一引发错误,则批处理中的其他消息不会被处理。

有没有办法配置流,以便在消息引发异常时,正常处理异常并处理批处理中的下一条消息?

以下代码实现了我想要的功能,但我想知道是否有更简单/更好的方法来实现这一目标,理想情况下是在单个 IntegrationFlow 中?

@Bean
public MessageChannel individualFlowInputChannel() {
return MessageChannels.direct().get();
}
@Bean
public IntegrationFlow batchFlow() {
return f -> f
.split()
.handle(message -> {
try {
individualFlowInputChannel().send(message);
} catch (Exception e) {
e.printStackTrace();
}
});
}
@Bean
public IntegrationFlow individualFlow() {
return IntegrationFlows.from(individualFlowInputChannel())
.handle((payload, headers) -> {
throw new RuntimeException("BOOM!");
}).get();
}

您可以使用其trapException选项将ExpressionEvaluatingRequestHandlerAdvice添加到最后一个handle()定义中:

/**
* If true, any exception will be caught and null returned.
* Default false.
* @param trapException true to trap Exceptions.
*/
public void setTrapException(boolean trapException) {

另一方面,如果您谈论的是"发送电子邮件",那么考虑在每个拆分项目的单独线程中执行此操作不是更好吗?在这种情况下,.split()之后的ExecutorChannel会来救援!

最新更新