Setting timeout for REST and SOAP webservices in Spring


I got in a situation where one of the external providers were missing their SLA and instead of their agreed 3 sec (hypothetical) for response, webservice was taking ~1 minute.
In this situation, the component was taking a lot of time to load -slowing down the whole page response. Following are the way to set the timeout parameter:

  • Setting JVM parameter (This will work ONLY for RESTfull services -Basically services dependent on JDK HTTP client)
    1. -Dsun.net.client.defaultConnectTimeout=1000 (Time to establish connection in millisec)
    2. -Dsun.net.client.defaultReadTimeout=1000 (Time to read content in millisec)

This works because Spring template by default uses HTTPClient from JDK, if the factory class has been overridden to non JDK class it will fail

  • Injecting the parameters for REST template
  • 	<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    		<constructor-arg>
        		<bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        			<property name="readTimeout" value="100" />
        			<property name="connectTimeout" value="100"/>
       		 	</bean>
    		</constructor-arg>
    	</bean>   
    

    Be sure not to use the depricated class “org.springframework.http.client.CommonsClientHttpRequestFactory”, otherwise it throws an error while setting connectTimeout

  • Injecting the parameters for SOAP Service
  •         <property name="messageSender">
                <bean
                    class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
                         <property name="connectionTimeout" value="10" />
                  <property name="readTimeout" value="100" />
                </bean>
            </property>
    
    Advertisements

    About ChandanPandey

    Try to come up with a good design as by product of good coding practices

    Posted on December 20, 2012, in Spring and tagged , . Bookmark the permalink. 3 Comments.

    1. Nice article. But, having issues.
      I am trying the “Injecting the parameters for REST template”.
      But running into class not found issues. I did include the httpclient.jar but it made me include httpcore.jar. Then got this error:
      Caused by: java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET

      at org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175)
      at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:158)
      at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
      at org.springframework.http.client.HttpComponentsClientHttpRequestFactory.setReadTimeout(HttpComponentsClientHttpRequestFactory.java:127)
      at org.springframework.http.client.HttpComponentsClientHttpRequestFactory.(HttpComponentsClientHttpRequestFactory.java:81)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

      Looks like the version of jars are not compatible.
      Can you please suggest me the right version of jar files?

      Thanks

    2. Hi this post helps me a lot thanks.
      My requirement slightly vary in which
      i am in a position to inject those parameter like connectionTimeout, ReadTimeout etc..
      from table. so tried like

      And in that class i will try to extend HttpComponentsMessageSender
      something like this
      ServiceHttpComponentsMessageSender extends HttpComponentsMessageSender
      i override the methods and
      then while i try setting the values i am not able to do that
      Any suggestion is appreciated

    3. Re-posting .. since the xml content doesn’t paste
      Hi this post helps me a lot thanks.
      My requirement slightly vary in which
      i am in a position to inject those parameter like connectionTimeout, ReadTimeout etc..
      from table. so tried like

      And in that class i will try to extend HttpComponentsMessageSender
      something like this
      “ServiceHttpComponentsMessageSender extends HttpComponentsMessageSender”
      i override the methods and
      then while i try setting the values i am not able to do that
      Any suggestion is appreciated

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: