Spring integration 变压器中的异常处理

Spring integration 变压器中的异常处理,spring-integration,Spring Integration,在transformer中遇到异常时,我们面临一个问题 以下是场景: 我们有一个路由器和一个变压器,配置如下 <bean id="commonMapper" class="com.example.commonMapper"></bean> <int:router input-channel="channelA" ref="commonMapper" method="methodA" /> <int:transformer input-

在transformer中遇到异常时,我们面临一个问题

以下是场景:

我们有一个路由器和一个变压器,配置如下

<bean id="commonMapper"
    class="com.example.commonMapper"></bean>

<int:router input-channel="channelA" ref="commonMapper"
    method="methodA" />

<int:transformer input-channel="channel_2"
    ref="commonMapper" method="methodB"
    output-channel="channelC"></int:transformer>

CommonMapper.java:

public String methodA(SomeBean someBean) {
    if (<some business condition example someBean.getXValue()>) {
      return "channel_1";
    } else if(<some condition>) {
        return "channel_2";  // Assuming it enters this condition, based on this the above transformer with input-channel="channel_2" gets called
    }else if (<some condition>) {
      return "channel_3";
    } else {
      return "channel_4";
    }
}

public SomeBean methodB(Message<SomeBean> message)
          throws Exception{
    SomeBean someBean = message.getPayload();
    someBean.setY(10/0); // Purposely introducing an exception
}
publicstringmethoda(SomeBean-SomeBean){
如果(){
返回“通道1”;
}else if(){
返回“channel_2”;//假设它进入此条件,则根据此条件调用上述具有input channel=“channel_2”的转换器
}如果(){
返回“通道3”;
}否则{
返回“通道4”;
}
}
公共SomeBean方法B(消息)
抛出异常{
SomeBean SomeBean=message.getPayload();
setY(10/0);//故意引入异常
}
在调试应用程序时,我们发现每当在
methodB()
中遇到异常时,控件都会返回到路由器引用方法,即
methodA()
,再次满足条件并调用转换器(使用
input channel=“channel_2”
)。这在某些迭代中重复。然后通过
注释方法HandlerExceptionResolver->resolveException
记录异常

以下是查询:

  • 为什么路由器在transformer中遇到异常时会再次被调用
  • 这是错误还是正常行为
  • 如何解决这个问题

  • 如果您需要更多详细信息,请告诉我。

    Spring集成流只是一个普通的Java方法链调用。所以,看看这个,就像你调用这样的东西:
    foo()->bar()->baz()
    。因此,当最后一个异常发生时,在调用堆栈中没有任何
    try…catch
    ,控件将返回到
    foo()
    ,如果有重试逻辑,它将再次调用相同的流

    我不确定您的
    注释方法HandlerExceptionResolver
    是什么,但看起来您在谈论这个:

    Deprecated. 
    as of Spring 3.2, in favor of ExceptionHandlerExceptionResolver
    
    @Deprecated
    public class AnnotationMethodHandlerExceptionResolver
    extends AbstractHandlerExceptionResolver
    
    Implementation of the HandlerExceptionResolver interface that handles exceptions through the ExceptionHandler annotation.
    
    This exception resolver is enabled by default in the DispatcherServlet.
    
    这意味着您使用的是非常旧的Spring。虽然我不认为这有什么关系,但是您的调用堆栈的顶部是SpringMVC。您需要查看一下重试的情况


    同时回答您所有的问题:是的,这是一种正常的行为——请参阅上面的Java调用解释。您需要从IDE调试Spring代码,以了解MVC级别的情况,我们正在使用的通道类型是DirectChannel,以防与上述问题相关。感谢Artem的解释。我将检查重试部分。