【发布时间】:2016-09-20 22:53:25
【问题描述】:
我继承了一个包含 Spring Retry 的 Spring Integration 项目。我不确定它是否已经过测试,也没有单独的测试。所以我试图用一个简单的场景来练习它。
通过模拟 RestTemplate exchange 方法,我希望能够测试重试逻辑。我可以让我想要抛出的异常发生,但它只发生一次 - 没有重试发生。
重试建议的 XML 在这里(retry-advice-context.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="retryAdvice" class="org.springframework.integration.handler.advice.RequestHandlerRetryAdvice" >
<property name="retryTemplate">
<bean class="org.springframework.retry.support.RetryTemplate">
<property name="backOffPolicy">
<bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="${retry.initialInterval}"/>
<property name="maxInterval" value="${retry.maxInterval}"/>
<property name="multiplier" value="${retry.multiplier}"/>
</bean>
</property>
<property name="retryPolicy">
<bean class="com.reachlocal.mediapublishing.shim.integration.retry.CustomRetryPolicy">
<constructor-arg name="maxAttempts" value="${retry.maxAttempts}" />
<constructor-arg name="retryableExceptions" ref="retryableExceptions" />
</bean>
</property>
</bean>
</property>
<property name="recoveryCallback">
<bean class="org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer">
<constructor-arg ref="errorChannel" />
</bean>
</property>
</bean>
<util:map id="retryableExceptions" map-class="java.util.HashMap" >
<entry key="java.net.SocketException" value="true" />
<entry key="com.examplel.ConnectionException" value="true" />
<entry key="com.example.CustomException" value="true" />
</util:map>
</beans>
这是一个 SI 处理文件的一部分:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<import resource="retry-advice-context.xml"/>
<int:channel id="channel1">
</int:channel>
<int:header-value-router id="commandTypeRouter" input-channel="commandChannel" <---DEFINED IN MASTER FILE
header-name="commandType" resolution-required="true">
<int:mapping value="COMMAND_1" channel="channel1"/>
</int:header-value-router>
<int:chain id="command1Chain" input-channel="channel1" output-channel="commandProcessed">
<int:header-enricher>
<int:error-channel ref="errorChannel" />
</int:header-enricher>
<int:service-activator ref="eventDataWriter" method = "addEventStart"/>
<int:service-activator ref="accountProcessor" method="processAccount">
<int:request-handler-advice-chain><ref bean="retryAdvice" /></int:request-handler-advice-chain>
</int:service-activator>
</int:chain>
</beans>
所以重试 bean retryAdvice 是不同链的一部分。链还有很多,所以我只想能够从服务层检查重试逻辑。代码中的任何地方都没有Retry注解(不知道是否需要)。
几个问题:
- 我可以从服务层测试重试功能还是需要执行整个链?
- 是否缺少重试机制所需的任何内容(注释、其他 XML)?
顺便说一句,这是使用 SI 4.1.3。
谢谢。
更新 1:
设法让 Gary 的项目在我的环境中运行。之后,我将retry-advice-context.xml 文件添加到主 SI xml 中。我将地图更改为只有RuntimeException。日志语句显示ExponentialBackoffPolicy 语句。我还收到了RetryTemplate 调试日志语句。
有了更多的了解,我将那里的内容转换为我正在使用的真实代码,并取得了更大的成功。我收到了我的异常发生的日志语句,最多将重试 3 次。
不幸的是,我得到的是:
17:29:26.154 DEBUG [task-scheduler-2][org.springframework.retry.support.RetryTemplate] Checking for rethrow: count=1
17:29:26.155 DEBUG [task-scheduler-2][org.springframework.retry.support.RetryTemplate] Retry failed last attempt: count=1
所以它最初知道它应该重试最多 3 次。但随后它表示它在 1 次重试后进行了最后一次尝试。
在 Gary 的工作代码中,调试语句将显示 Retry: count=2 等...以进行下一次连续尝试。
在应该进行重试期间,Spock 测试代码中有一个sleep 语句。我把时间加长和缩短都没有改变。
通过重试代码继续尝试和调试,看看为什么它在第一次重试时停止。
【问题讨论】:
-
您能否澄清问题 1 中的“服务层”是什么意思?
-
我的意思是在对外部系统进行 REST 调用的服务类中。可能必须重试的请求。它使用 Spring RestTemplate 调用代码。
标签: java groovy spring-integration spring-retry