【问题标题】:Spring integration chain error handlingSpring集成链错误处理
【发布时间】:2015-10-03 22:18:17
【问题描述】:

在同步通道的拆分器和聚合器流期间需要帮助处理链中的错误。 下面是用例,它将是同步通道。所以在链中会有一组服务激活器来执行业务逻辑。现在,如果链中存在的服务激活器中有任何异常,我希望在链本身中处理它并继续处理其他拆分的消息。

为了做到这一点,我尝试为链中的错误处理程序添加标题丰富器。但没有奏效。任何建议。

Object1 包含列表

流程:

List --> Splitter1 (for Object1) --> Splitter2 (for Object2) --> Chain --> Aggregator --> Aggregator

代码

   <int:chain input-channel="ch3" output-channel="ch10"  >
      <int:header-enricher>
        <int:error-channel  ref="exception1" ></int:error-channel>
    </int:header-enricher>

   <int:service-activator ref="myService" method="method1"></int:service-activator>

   <int:service-activator ref="myService" method="method2"></int:service-activator>
   <int:service-activator ref="myService" method="method3"></int:service-activator>
    <int:service-activator ref="myService" method="method4"></int:service-activator>
</int:chain>


    <!-- Exception channel for chain and the output should go to the chain output channel -->     
    <int:chain input-channel="exception1" output-channel="ch10"  >
        <int:service-activator ref="exp" method="myException"></int:service-activator>  
    </int:chain>

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    不幸的是,它不是那样工作的。 error-channel 标头也适用于异步情况。它允许覆盖MessagePublishingErrorHandlerPollableChannelExecutor 频道中的默认行为。换句话说,如果用原始的 Java 语言交谈,我们真的无法做到try...catch

    因此,要解决您的要求,您确实应该依赖于特定 &lt;service-activator&gt;try...catch 函数。它被称为ExpressionEvaluatingRequestHandlerAdvice,并且必须配置为&lt;request-handler-advice-chain&gt;

    在您的情况下,您应该像这样配置Advice

    <bean class="ExpressionEvaluatingRequestHandlerAdvice">
        <property name="trapException" value="true"/>
        <property name="onFailureExpression" value="#exception"/>
        <property name="failureChannel" value="myErrorChannel"/>
    </bean>
    

    trapException="true" 允许不要将异常重新抛出到顶层。在你的情况下&lt;splitter&gt;

    onFailureExpression 表示从catch 块发送到failureChannel 的内容。

    failureChannel 是您想要的 error-channel 来处理 &lt;service-activator&gt; 故障。

    顺便说一句,源代码看起来像:

       try {
            Object result = callback.execute();
            if (this.onSuccessExpression != null) {
                this.evaluateSuccessExpression(message);
            }
            return result;
        }
        catch (Exception e) {
            Exception actualException = this.unwrapExceptionIfNecessary(e);
            if (this.onFailureExpression != null) {
                Object evalResult = this.evaluateFailureExpression(message, actualException);
                if (this.returnFailureExpressionResult) {
                    return evalResult;
                }
            }
            if (!this.trapException) {
                throw actualException;
            }
            return null;
        }
    

    由于我们使用trapException="true" 防止重新抛出,我们最终使用return null。并且拥有&lt;service-activator&gt;null-payload 的忠诚度,我们允许我们的&lt;splitter&gt; 继续发送其他消息。

    HTH

    【讨论】:

    • 谢谢。我已按照上述评论实施。
    • 很高兴听到!接受其他社区成员的问题将是一种很好的语气。不客气,顺便说一句!
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 2018-07-05
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多