【问题标题】:Spring Integration error loggingSpring 集成错误日志记录
【发布时间】:2015-04-03 00:05:46
【问题描述】:

我在测试 Spring Integration 时遇到了一些有趣的事情,所以我创建了这个流程(不是很困难,它就像一个魅力):

  • 用户购买了一本书
  • 执行了一些转换
  • 异步发送 Jms 消息

但我有一个问题:如果出站通道适配器发生故障,您知道是否有任何方法可以打印日志?我不在乎 JMS 发布操作失败,因为流程结束返回正确的书籍有效负载,但显示日志会很好,而且我认为我对 Spring IO 错误处理有点混乱。

提前致谢!

<si:channel id="purchaseBookInputChannel" />
<si:channel id="purchaseBookReplyChannel" />
<si:channel id="purchaseBookRoutingChannel" />
<si:channel id="purchaseBookJmsInputChannel" />

<si:chain id="purchaseBookChain" input-channel="purchaseBookInputChannel" output-channel="purchaseBookRoutingChannel">
    <si:service-activator ref="bookServiceActivator" method="purchase" />
    <si:transformer ref="bookTransformer" method="transform" />
</si:chain>

<si:recipient-list-router id="purchaseBookRouter" input-channel="purchaseBookRoutingChannel" ignore-send-failures="true" >
        <si:recipient channel="purchaseBookJmsInputChannel" />
        <si:recipient channel="purchaseBookReplyChannel" />
</si:recipient-list-router>

<si-jms:outbound-channel-adapter channel="purchaseBookJmsInputChannel" connection-factory="connectionFactory" destination="bookQueue">
    <si-jms:request-handler-advice-chain>
        <bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">               
            <property name="onFailureExpression" value="#exception" />
            <property name="failureChannel" ref="logginChannel" />
            <property name="trapException" value="true" />
        </bean>
    </si-jms:request-handler-advice-chain>
</si-jms:outbound-channel-adapter>

<si:logging-channel-adapter id="logginChannel" level="WARN"/>

更新

您好,感谢您的宝贵时间!

我认为我做错了什么,因为我正在测试 request-handler-advice-chain/error-channel 而这就是我得到的:

  • jms 消息已发送
  • 发生 JMS 错误(我的 JMS 服务器已关闭,因为我正在测试错误处理)并打印格式化的错误日志
  • 流程已中止,未购买任何图书

这是我正在寻找的行为:

  • jms 消息已发送
  • 发生 JMS 错误并打印格式化的错误日志
  • 书买了,流程ok了!!

关于如何在不中止流程的情况下打印日志的任何线索?如果我使用 ignore-send-failures="true" 属性,流程会忽略 JMS 错误,但不会打印错误日志。

祝你有美好的一天!

【问题讨论】:

  • 我已经测试了ExpressionEvaluatingRequestHandlerAdvicetrapException 并且工作完美!我已经用这些更改更新了我附加的示例,也许有人可能会热衷于这个测试。感谢您的支持!

标签: logging spring-integration


【解决方案1】:

首先我认为你不对。

最好将消息发布到 JMS,然后再返回回复。因此,对于实际应用来说,更改&lt;si:recipient-list-router&gt; 的频道顺序会更好。

从另一端记录和抑制来自&lt;si-jms:outbound-channel-adapter&gt; 的异常,您应该将&lt;request-handler-advice-chain&gt; 添加到该适配器,并将ExpressionEvaluatingRequestHandlerAdvicefailureChannel 提供给&lt;logging-channel-adapter&gt;

更新

ExpressionEvaluatingRequestHandlerAdvice 有一个trapException 选项。将其设置为true,您的 JMS 异常将不会被重新抛出。

【讨论】:

  • 此外,如果网关或轮询器启动您的流程,您可以向该元素添加 error-channel,您可以处理错误流程中的异常。默认的errorChannel 只记录异常。
猜你喜欢
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 2011-04-05
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
相关资源
最近更新 更多