How to set timeout in Spring WebServiceTemplate

Sai Kumar picture Sai Kumar · Jul 18, 2011 · Viewed 58.7k times · Source

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

Answer

Sathish Murugesan picture Sathish Murugesan · Jul 23, 2012

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>