Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout in Spring WebServiceTemplate

I am using org.springframework.ws.client.core.WebServiceTemplate for making Web Service calls. How can i configure timeout for the call.

like image 260
Sai Kumar Avatar asked Jul 18 '11 13:07

Sai Kumar


People also ask

How do I set timeout in spring?

One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.

What is the use of WebServiceTemplate?

The WebServiceTemplate is the core class for client-side Web service access in Spring-WS. It contains methods for sending Source objects, and receiving response messages as either Source or Result .

Is Spring WebServiceTemplate thread safe?

Your configuration of WebServiceTemplate , SaajSoapMessageFactory and Jaxb2Marshaller singletons is perfectly fine, and entirely thread-safe.

What is marshalSendAndReceive?

marshalSendAndReceive(String uri, Object requestPayload) Sends a web service message that contains the given payload, marshalled by the configured Marshaller . Object.


2 Answers

If you are using Spring Webservices 2.1.0 version, You can set timeout using HttpComponentsMessageSender.

CommonsHttpMessageSender are deprecated and not recommended by Spring anymore.

The way I have it implemented, I define my WebServiceTemplate to use HttpComponentsMessageSender.

Values are in Milliseconds

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">     <property name="defaultUri" value="${endpoint.url}" />     <property name="marshaller" ref="marshaller" />     <property name="unmarshaller" ref="unmarshaller" />     <property name="messageSender">         <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender">             <property name="connectionTimeout" value="1200000" />             <property name="readTimeout" value="1200000" />         </bean>     </property> </bean> 

Just Make sure you have in your pom file, you added the following

<dependency>         <groupId>org.apache.httpcomponents</groupId>         <artifactId>httpclient</artifactId>         <version>4.2.1</version>         <scope>compile</scope>     </dependency> 
like image 160
Sathish Murugesan Avatar answered Sep 18 '22 15:09

Sathish Murugesan


Same as Sathish answer, but programmatically (Spring 4+):

@Component public class MyWebServiceGatewaySupport extends WebServiceGatewaySupport {     @Value("${my.ws.readtimeout}")     private String readTimeout;      @Value("${my.ws.connectiontimeout}")     private String connectionTimeout;      Object marshalSendAndReceive(String endpoint, Object requestPayload)     {          WebServiceTemplate wsTemplate = this.getWebServiceTemplate();         WebServiceMessageSender[] senders = wsTemplate.getMessageSenders();         for (WebServiceMessageSender sender: senders)         {             try             {                 int readTimeoutMsec = Integer.parseInt(readTimeout);                 int connTimeoutMsec = Integer.parseInt(connectionTimeout);                 HttpComponentsMessageSender httpSender = (HttpComponentsMessageSender) sender;                 httpSender.setReadTimeout(readTimeoutMsec);                 httpSender.setConnectionTimeout(connTimeoutMsec);             }             catch (ClassCastException|NumberFormatException cex)             {                  logger.warn("Cannot set WS timeout: " + cex.getMessage());             }         }          return wsTemplate.marshalSendAndReceive(endpoint, requestPayload);       }  } 
like image 24
Guildenstern70 Avatar answered Sep 19 '22 15:09

Guildenstern70