【问题标题】:Spring Integration Java DSL: How to continue after error situation with the split and the aggregate methods?Spring Integration Java DSL:在拆分和聚合方法出现错误情况后如何继续?
【发布时间】:2018-11-23 10:14:10
【问题描述】:

我的程序在高层执行以下操作

Task 1
  get the data from the System X
  the Java DSL split
    post the data to the System Y
    post the reply data to the X
  the Java DSL aggregate
Task 2
  get the data from the System X
  the Java DSL split
    post the data to the System Y
    post the reply data to the X
  the Java DSL aggregate
...

问题是当一个post the data to the System Y子任务失败时,错误信息被正确地发送回系统X,但之后任何其他子任务或任务都没有执行。

我的错误处理程序是这样做的:

...
Message<String> newMessage = MessageBuilder.withPayload("error occurred")
                .copyHeadersIfAbsent(message.getPayload().getFailedMessage().getHeaders()).build();
...
Set some extra headers etc. 
...
return newMessage;

可能是什么问题?

编辑:

我调试了 Spring 集成。在错误情况下,只有第一条错误消息到达方法AbstractCorrelatingMessageHandler.handleMessageInternal。其他成功和失败消息不会出现在该方法中。

如果没有错误,则所有消息都到达该方法,最后释放组。

我的程序有什么问题?

编辑 2:

这是有效的:

Http.outboundGateway 添加了advice

.handle(Http.outboundGateway(...,
                    c -> c.advice(myAdvice()))

还有myAdvice bean

@Bean
private Advice myAdvice() {
    return new MyAdvice();
}

还有MyAdvice

public class MyAdvice<T> extends AbstractRequestHandlerAdvice {
@SuppressWarnings("unchecked")
@Override
protected Object doInvoke(final ExecutionCallback callback, final Object target, final Message<?> message)
        throws Exception {
    ...

    try {
        result = (MessageBuilder<T>) callback.execute();
    } catch (final MessageHandlingException e) {
        take the exception cause for the new payload
    }

    return new message with the old headers and replyChannel header and result.payload or the exception cause as a payload
}

}

【问题讨论】:

  • 编辑了原帖。

标签: spring-integration spring-integration-dsl


【解决方案1】:

您的程序没有任何问题。这正是 Java 中常规循环的工作方式。要捕获每次迭代的异常并继续处理其他剩余项目,您肯定需要在 Java 循环中使用 try..catch。因此,您需要在此处为​​分离器申请类似的东西。它可以通过ExpressionEvaluatingRequestHandlerAdviceExectutorChannel 作为拆分器的输出来实现,也可以通过拆分器输出通道上的服务激活器调用网关来实现。

由于故事是关于聚合器的,你仍然需要以某种方式完成一个组,这只能通过从错误处理中发出一些 错误补偿消息 来返回到聚合器的输入通道。在这种情况下,您需要确保从抛出的MessagingExceptionfailedMessage 复制请求标头到错误流。聚合组后,您需要从正常消息中分离出错误消息。这只能使用特殊的有效负载来完成,或者您可能只是将异常作为有效负载,以便在聚合器的最终结果中正确区分错误和正常消息。

【讨论】:

  • 谢谢,有了建议,它正在工作!你给了 try..catch 例子也很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
相关资源
最近更新 更多