I would like to configure a timeout on the client side for spring webservices using RestTemplate. I tried the following configuration :
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<property name="readTimeout" value="10000" />
</bean>
</constructor-arg>
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
<ref bean="marshallingHttpMessageConverter" />
</list>
</property>
</bean>
But I have a NoClassDefFoundError when I start my tomcat :
06 févr. 2012 10:43:43,113 [ERROR,ContextLoader] Context initialization failed
java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase
However I have included commons-httpclient in my pom.xml :
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency
Any idea of how I could do/fix that?
Thanks in advance !
Snicolas answer almost worked for me, only had to change the cast Class:
RestTemplate restTemplate = new RestTemplate();
((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setReadTimeout(1000*30);
Also you can set the connect timeOut:
((SimpleClientHttpRequestFactory)restTemplate.getRequestFactory()).setConnectTimeout(1000*30);
This worked for me
( (HttpComponentsClientHttpRequestFactory) getRestTemplate().getRequestFactory() ).setReadTimeout( 120 * 1000 );
I used it in the android version of spring android rest template.
Default value is 60 * 1000
I had the same problem and first tried to fix it by modifying the Spring configuration
but my attempts were all unsucessfull.
Finally, I have partially fixed it by setting the folowing JVM system properties :
sun.net.client.defaultConnectTimeout
sun.net.client.defaultReadTimeout
(follow that link for more details about them : http://docs.oracle.com/javase/1.4.2/docs/guide/net/properties.html )
First, i inject my custom values for "Connect timeout"
and "Read timeout" stored in a property file, by using an "home made" configuration bean :
<bean id="rmProperties" class="com.mydomain.myapp.myConfigBean" scope="singleton">
...
<property name="httpRequestConnectTimeout" value="${httpRequestConnectTimeout}" />
<property name="httpRequestReadTimeout" value="${httpRequestReadTimeout}" />
...
</bean>
Then, i set the the JVM system properties by using the System.setProperty(... ) method just like this :
System.setProperty(propName, value);
I have only one trouble left : the value set in sun.net.client.defaultConnectTimeout
doesn't seem to be taken in account.
After having made some more tests, i've realized that it happens when i try to reach my target hosts through a proxy server (Squid in my case).
However ,there is an inconvenient to use that setting method : the timeout settings will be used for all the further requests
Regards
I had the same needing of being able to set timeout for webservice consumption, and I just solve it with an other spring conf.
First with the configuration bellow, I had the same problem as @jsebFrank (java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase)
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean
class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<property name="connectTimeout" value="10000" />
<property name="readTimeout" value="10000" />
</bean>
</constructor-arg>
</bean>
But as Spring support explain here (in section 16.5 Timeout Handling), you can use the SimpleClientHttpRequestFactory request factory (which is the default one for Spring restTemplate).
Using it, I don't have problem anymore:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean
class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="10000" />
<property name="readTimeout" value="10000" />
</bean>
</constructor-arg>
</bean>