gpt4 book ai didi

error-handling - Spring集成中的错误处理

转载 作者:行者123 更新时间:2023-12-03 07:58:29 25 4
gpt4 key购买 nike

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

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

<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 应该得到成功响应并记录错误。
  • 这里因为我在出站适配器中使用错误 channel ,即使变压器发生错误,它也会返回到系统 1。
  • 请建议我如何在不使用全局错误 channel 的情况下处理错误以及如何处理 jms 出站适配器中的错误。
  • 感谢您回答我的问题
  • 最佳答案

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

    对于第二种( transformer )情况,您应该查看 ExpressionEvaluatingRequestHandlerAdvice并将其与 <request-handler-advice-chain> 一起使用对于<transformer> .
    <int-jms:outbound-channel-adapter> 也同样适用。 ,如果您应该向 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

    关于error-handling - Spring集成中的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44954787/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com