【问题标题】:Error handling in spring integrationSpring集成中的错误处理
【发布时间】:2017-12-10 19:23:42
【问题描述】:

我有三个不同的系统。我正在使用 Spring 集成来同步所有这些系统中的数据。

系统 1 调用 ---> 系统 2 通过 http:inbound gateway

<int-http:inbound-gateway id="gateway"
    path="/save" supported-methods="POST, PUT"
    request-channel="requestChannel" reply-channel="replyChannel"
    request-payload-type="com.model.Request"
    mapped-request-headers="source" error-channel="errorChannel" />

系统2将调用服务方法来持久化数据,如果请求有效则返回响应,否则抛出异常

<int:service-activator ref="serviceMethod"
    method="saveIfValid" input-channel="requestChannel"
    output-channel="serviceOutput" />

<int:recipient-list-router id="id1"
    input-channel="serviceOutput">
    <int:recipient channel="system1" />
    <int:recipient channel="system3" />
</int:recipient-list-router>

只有在操作成功的情况下,我才需要向系统 1 和系统 3 发送服务方法响应。 调用服务方法后,根据服务方法响应,使用transformer生成对系统3的请求。在转换器之后,我将请求放入 mq 队列中。

<int:transformer id="transformer1"
    method="convert" input-channel="system3"
    output-channel="jmsInput">
    <bean
        class="com.transformer.System3Transformer" />
</int:transformer> 


<int-jms:outbound-channel-adapter id="adapter"
    channel="jmsInput" destination-name="queueName">
</int-jms:outbound-channel-adapter>

更新的 JMS 出站代码

<int-jms:outbound-channel-adapter id="jms1"
		channel="jmsIn" destination-name="queueName">
		<int-jms:request-handler-advice-chain>
			<bean
				class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
				 <property name="onSuccessExpression" value="T(Boolean).TRUE" />
				<property name="successChannelName" value="afterSuccessDeleteChannel" />
			    <property name="onFailureExpression" value="T(Boolean).FALSE" />
				<property name="failureChannelName" value="afterFailRenameChannel" />
			</bean>
		</int-jms:request-handler-advice-chain>
	</int-jms:outbound-channel-adapter>

我的问题是

  • 如果服务类失败,我需要发送错误响应并停止流程
  • 如果服务方法成功持久化数据,但转换失败,系统 1 应获得成功响应并记录错误。
  • 由于我在出站适配器中使用错误通道,即使变压器发生错误,它也会返回到系统 1。
  • 请建议我如何在不使用全局错误通道的情况下处理错误以及如何处理 jms 出站适配器中的错误。
    • 感谢您回答我的问题

【问题讨论】:

    标签: error-handling spring-integration


    【解决方案1】:

    对于第一种情况,您实际上应该只依赖异常传播 - 流停止并且错误作为 HTTP 响应发送到 system1。

    对于第二个 (transformer) 案例,您应该查看 ExpressionEvaluatingRequestHandlerAdvice 并将其与 &lt;request-handler-advice-chain&gt; 一起用于 &lt;transformer&gt;

    &lt;int-jms:outbound-channel-adapter&gt; 也同样适用,如果您需要向 system1 确认。

    http://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#expression-advice

    https://github.com/spring-projects/spring-integration-samples/tree/master/intermediate/retry-and-more

    【讨论】:

    • 感谢您回答我的问题。如果 中发生任何错误,我只想记录它,我不想向系统 1 显示错误。您能帮忙实现吗?
    • &lt;int-jms:request-handler-advice-chain&gt; &lt;bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice"&gt; &lt;property name="onSuccessExpression" value="payload" /&gt; &lt;property name="successChannelName" value="afterSuccessDeleteChannel" /&gt; &lt;property name="onFailureExpression" value="payload" /&gt; &lt;property name="failureChannelName" value="afterFailRenameChannel" /&gt; &lt;/bean&gt;
    • 请看上面的代码。它进入成功通道和失败通道。您能否解释一下它如何评估它是成功还是失败? .
    • 如您所见,从 cmets 无法读取代码 sn-p。您始终可以使用正确的格式编辑您的问题。如果您只想记录,您可以订阅&lt;logging-channel-adapter&gt; 到该建议的failureChannel。如果您不想发回错误,可以使用trapException 选项。请阅读ExpressionEvaluatingRequestHandlerAdvice JavaDocs 并提及参考手册以了解其工作原理。
    • 您好,我已经用 jms 出站适配器的代码 sn-p 更新了我的问题。你能看一下吗。它重定向到成功和失败通道并抛出 org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice$MessageHandlingExpressionEvaluatingAdviceException: Handler Failed。 org.springframework.messaging.core.DestinationResolutionException:没有可用的输出通道或回复通道标头
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 2018-07-05
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多