【问题标题】:Spring integration - Retry to establish connection on exception scenariosSpring 集成 - 重试在异常情况下建立连接
【发布时间】:2019-02-18 11:56:29
【问题描述】:

我的应用程序使用 spring 集成与第三方系统通信。我发送了一个有效负载,我得到了我解析和使用的响应。都好。请在下面找到我使用的 SI xml。

现在我想应用程序重试以在我尝试连接的服务器不可用或超时或拒绝连接等异常情况下建立连接。 如何使用 SI xml 配置来实现这一点?请指导。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">



<int:gateway id="gw" service-interface=" com.RxGateway"
    default-request-channel="objectOut" />

<int:channel id="objectOut" />

<int-ip:tcp-connection-factory id="client"
    type="client" host="10.236.249.xx" port="9103" single-use="false"
    so-timeout="50000000" using-nio="false" so-keep-alive="true"
    serializer="customDSerializer" deserializer="customDSerializer" />

<bean id="customDSerializer" class="com.CustomSerializerDeserializer">
    <property name="maxMessageSize" value="4096" />
</bean>

<int-ip:tcp-outbound-gateway id="outGateway"
    request-channel="objectOut" reply-channel="toSA" connection-factory="client"
    request-timeout="100000" reply-timeout="50000"/>

<int:service-activator input-channel="toSA"
    ref="rxService" method="parseResponse"/>

<bean id="rxService" class="com.RxService"/>

<int:channel id="toSA" />
<int:channel id="bytesIn" />

</beans>

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    您可以将retry-advice 添加到您的&lt;int-ip:tcp-outbound-gateway&gt;

    <int-ip:tcp-outbound-gateway>
        <int-ip:request-handler-advice-chain>
            <int:retry-advice/>
        </int-ip:request-handler-advice-chain>
    </int-ip:tcp-outbound-gateway>
    

    在参考手册中查看更多信息:https://docs.spring.io/spring-integration/docs/current/reference/html/#message-handler-advice-chain

    【讨论】:

    • 没错,您可以通过框架中的重试支持来做到这一点。
    • 谢谢,阿尔乔姆!我想以 5 秒的间隔重试 3 次。在大致浏览了文档之后,我想我应该能够执行以下操作,&lt;int:request-handler-advice-chain&gt; &lt;int:retry-advice id="retrier" max-attempts="4" recovery-channel="myErrorChannel"&gt; &lt;int:fixed-back-off initial="5000" maximum="60000" /&gt; &lt;/int:retry-advice&gt; &lt;/request-handler-advice-chain&gt;
    • 对。是时候接受答案了:stackoverflow.com/help/someone-answers?
    • 我明天会试试这个。同时,您能否告诉我是否需要定义 recovery-channel="myErrorChannel"?我不希望抛出任何异常,但如果我无法连接到服务器,应该能够继续使用我的其余代码。我应该为此做些什么?
    • 对。如果你想吞下异常,你需要有那个recovery-channel
    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2014-08-22
    • 2014-07-12
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多