使用RequestHandlerRetryAdvice重试Web服务操作
1.引言
有時在調(diào)用Web服務時,我們可能有興趣在發(fā)生錯誤的情況下重試該操作。 使用Spring Integration時,我們可以使用RequestHandlerRetryAdvice類實現(xiàn)此功能。 此類將使我們在放棄并引發(fā)異常之前重試指定次數(shù)的操作。 這篇文章將向您展示如何完成此任務。
測試應用程序?qū)⒄{(diào)用Web服務,如果響應失敗,它將等待指定的時間,然后重試,直到收到響應或達到重試限制為止。 如果達到限制,失敗的請求將被存儲到數(shù)據(jù)庫中。 主要,這篇文章顯示以下示例:
- 使用出站網(wǎng)關(guān)調(diào)用Web服務
- 配置重試建議以在必要時重試該操作
- MongoDB集成。
該應用程序的源代碼可以在github上找到。
您還可以在github上獲取由應用程序調(diào)用的Web服務項目的源代碼。
2.Web服務調(diào)用
用例 :客戶端調(diào)用Web服務并接收響應。
該請求通過“系統(tǒng)入口”網(wǎng)關(guān)進入消息傳遞系統(tǒng)。 然后,它到達出站網(wǎng)關(guān),調(diào)用Web服務并等待響應。 收到響應后,將其發(fā)送到響應通道。
上圖是此配置的結(jié)果:
<context:component-scan base-package="xpadro.spring.integration" /><!-- Initial service request --> <int:gateway id="systemEntry" default-request-channel="requestChannel"service-interface="xpadro.spring.integration.gateway.ClientService" /><int:channel id="requestChannel"><int:queue /> </int:channel><int-ws:outbound-gateway id="marshallingGateway"request-channel="requestChannel" reply-channel="responseChannel"uri="http://localhost:8080/spring-ws/orders" marshaller="marshaller"unmarshaller="marshaller"><int:poller fixed-rate="500" /> </int-ws:outbound-gateway><oxm:jaxb2-marshaller id="marshaller" contextPath="xpadro.spring.integration.types" /><!-- Service is running - Response received --> <int:channel id="responseChannel" /><int:service-activator ref="clientServiceActivator" method="handleServiceResult" input-channel="responseChannel" />映射到響應通道的是一個服務激活器,它僅記錄結(jié)果。
TestInvocation.java:將請求發(fā)送到入口網(wǎng)關(guān)
@ContextConfiguration({"classpath:xpadro/spring/integration/config/int-config.xml","classpath:xpadro/spring/integration/config/mongodb-config.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class TestInvocation {private Logger logger = LoggerFactory.getLogger(this.getClass());@Autowiredprivate ClientService service;@Testpublic void testInvocation() throws InterruptedException, ExecutionException {logger.info("Initiating service request...");ClientDataRequest request = new ClientDataRequest();request.setClientId("123");request.setProductId("XA-55");request.setQuantity(new BigInteger("5"));service.invoke(request);logger.info("Doing other stuff...");Thread.sleep(60000);} }使用此配置,如果服務調(diào)用失敗,則將引發(fā)MessagingException并將其發(fā)送到錯誤通道。 在下一節(jié)中,我們將添加重試配置。
3.添加重試建議
用例 :初始請求失敗,因為該服務未激活。 我們將重試該操作,直到從服務收到響應為止。
在這種情況下,我們需要將重試建議添加到Web服務出站網(wǎng)關(guān):
<int-ws:outbound-gateway id="marshallingGateway" interceptor="clientInterceptor"request-channel="requestChannel" reply-channel="responseChannel"uri="http://localhost:8080/spring-ws/orders" marshaller="marshaller"unmarshaller="marshaller"><int:poller fixed-rate="500" /><int-ws:request-handler-advice-chain><ref bean="retryAdvice" /></int-ws:request-handler-advice-chain> </int-ws:outbound-gateway>現(xiàn)在,Web服務出站網(wǎng)關(guān)會將調(diào)用委派給重試建議,重試建議將按指定的次數(shù)嘗試操作多次,直到從服務獲得響應為止。 讓我們定義重試建議:
<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.FixedBackOffPolicy"><property name="backOffPeriod" value="4000" /></bean></property><property name="retryPolicy"><bean class="org.springframework.retry.policy.SimpleRetryPolicy"><property name="maxAttempts" value="4" /></bean></property></bean></property> </bean>為了實現(xiàn)其目標,建議使用RetryTemplate,該模板由Spring Retry項目提供。 我們可以通過定義退避和重試策略來自定義其行為。
退避政策 :在每次重試之間建立一段時間。 更有趣的類型是:
- FixedBackOffPolicy :在我們的示例中使用。 每次重試之間將等待相同的指定時間。
- ExponentialBackOffPolicy :從確定的時間量開始,它將使每次重試的時間加倍。 您可以通過建立乘數(shù)來更改默認行為。
重試策略 :確定將重試失敗操作的次數(shù)。 一些類型:
- SimpleRetryPolicy :在我們的示例中使用。 指定重試次數(shù)限制。
- ExceptionClassifierRetryPolicy :允許我們根據(jù)引發(fā)的異常來建立不同的maxAttempts。
- TimeoutRetryPolicy :將繼續(xù)重試,直到達到超時為止。
4,不走運,記錄失敗的請求
用例 :服務將無法恢復,無法將請求存儲到數(shù)據(jù)庫中。
配置的最后部分如下:
<!-- Log failed invocation --> <int:service-activator ref="clientServiceActivator" method="handleFailedInvocation" input-channel="errorChannel" output-channel="logChannel" /><int:channel id="logChannel" /><bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory"><constructor-arg><bean class="com.mongodb.Mongo"/></constructor-arg><constructor-arg value="test"/> </bean><int-mongodb:outbound-channel-adapter id="mongodbAdapter" channel="logChannel"collection-name="failedRequests" mongodb-factory="mongoDbFactory" />訂閱錯誤通道的服務激活器將檢索失敗的消息并將其發(fā)送到出站適配器,出站適配器會將其插入到mongoDB數(shù)據(jù)庫中。 服務激活器:
public Message<?> handleFailedInvocation(MessagingException exception) {logger.info("Failed to succesfully invoke service. Logging to DB...");return exception.getFailedMessage(); }如果我們未能成功從服務獲得響應,則該請求將存儲到數(shù)據(jù)庫中:
六,結(jié)論
我們已經(jīng)了解了Spring Integration如何從Spring Retry項目獲得支持以實現(xiàn)操作的重試。 我們使用了int-ws:request-handler-advice-chain ,但是'int'命名空間也支持此元素,以將該功能添加到其他類型的端點。
翻譯自: https://www.javacodegeeks.com/2014/02/retry-web-service-operations-with-requesthandlerretryadvice.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的使用RequestHandlerRetryAdvice重试Web服务操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 8将默认使用传输级别安全性(T
- 下一篇: Drools:基于PHREAK堆栈的评估