javascript
Spring集成–配置Web服务客户端超时
介紹
在Spring Integration的支持下,您的應用程序可以使用出站Web服務網關來調用Web服務。 調用由該網關處理,因此您只需要擔心構建請求消息和處理響應。 但是,使用這種方法并不明顯,如何配置其他選項,例如設置超時或操作緩存。 本文將展示如何設置客戶端超時并將其與網關集成。
本文分為以下幾節:
- 源代碼可以在github上找到。
Web服務調用概述
Web服務出站網關將Web服務調用委托給Spring Web Services WebServiceTemplate 。 當消息到達出站網關時,此模板使用消息發件人以創建新連接。 下圖顯示了該流程的概述:
默認情況下,Web服務模板將HttpUrlConnectionMessageSender設置為其消息發送者,這是不支持配置選項的基本實現。 但是,可以通過設置具有設置讀取和連接超時能力的更高級的消息發送者來覆蓋此行為。
我們將在下一部分中配置消息發送者。
配置消息發件人
我們將配置消息發件人到出站網關。 這樣,網關將使用提供的模板設置模板的消息發送者。
我們在示例中提供的實現是HttpComponentsMessageSender類,該類也來自Spring Web Services項目。 該消息發件人允許我們定義以下超時:
- connectionTimeout :設置建立連接之前的超時。
- readTimeout :設置基礎HttpClient的套接字超時。 這是服務回復所需的時間。
組態:
<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender"><property name="connectionTimeout" value="${timeout.connection}"/><property name="readTimeout" value="${timeout.read}"/> </bean>屬性文件包含兩個值,它們都設置為兩秒鐘:
timeout.connection = 2000
timeout.read = 2000
配置完成后,我們將其添加到Web服務出站網關配置中:
<int-ws:outbound-gateway uri="http://localhost:8080/spring-ws-courses/courses" marshaller="marshaller" unmarshaller="marshaller" request-channel="requestChannel" message-sender="messageSender"/>要使用此消息發件人,您將需要添加以下依賴項:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.3</version> </dependency>就是這樣; 下一部分將顯示示例應用程序以查看其工作方式。
樣例應用
流程很簡單; 它包含一個向Web服務發送請求并接收響應的應用程序。 Web服務源代碼可以在github上找到。
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:int="http://www.springframework.org/schema/integration"xmlns:int-ws="http://www.springframework.org/schema/integration/ws"xmlns:oxm="http://www.springframework.org/schema/oxm"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsdhttp://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"><context:component-scan base-package="xpadro.spring.integration.ws"/><context:property-placeholder location="classpath:props/service.properties"/><!-- System entry --><int:gateway id="systemEntry" default-request-channel="requestChannel" service-interface="xpadro.spring.integration.ws.gateway.CourseService"/><!-- Web service invocation --><int-ws:outbound-gateway uri="http://localhost:8080/spring-ws-courses/courses" marshaller="marshaller" unmarshaller="marshaller" request-channel="requestChannel" message-sender="messageSender"/><oxm:jaxb2-marshaller id="marshaller" contextPath="xpadro.spring.integration.ws.types" /><bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender"><property name="connectionTimeout" value="${timeout.connection}"/><property name="readTimeout" value="${timeout.read}"/></bean></beans>網關包含我們將進入消息傳遞系統的方法:
public interface CourseService {@GatewayGetCourseResponse getCourse(GetCourseRequest request); }最后,測試:
@ContextConfiguration(locations = {"/xpadro/spring/integration/ws/config/int-course-config.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class TestIntegrationApp {@Autowiredprivate CourseService service;@Testpublic void invokeNormalOperation() {GetCourseRequest request = new GetCourseRequest();request.setCourseId("BC-45");GetCourseResponse response = service.getCourse(request);assertNotNull(response);assertEquals("Introduction to Java", response.getName());}@Testpublic void invokeTimeoutOperation() {try {GetCourseRequest request = new GetCourseRequest();request.setCourseId("DF-21");GetCourseResponse response = service.getCourse(request);assertNull(response);} catch (WebServiceIOException e) {assertTrue(e.getCause() instanceof SocketTimeoutException);}} }結論
我們已經學習了如何為Web服務出站網關設置其他選項以建立超時。 在下一篇文章中,我將解釋如何緩存此調用。
翻譯自: https://www.javacodegeeks.com/2014/05/spring-integration-configure-web-service-client-timeout.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Spring集成–配置Web服务客户端超时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: au585是什么金?
- 下一篇: Spring 3.2矩阵变量是什么? –