Enterprise Java

Spring Integration – Configure web service client timeout

Introduction

With the support of Spring Integration, your application can invoke a web service by using an outbound web service gateway. The invocation is handled by this gateway, thus you just need to worry about building the request message and handling the response. However, with this approach it is not obvious how to configure additional options like setting timeouts or caching of operations. This article will show how to set a client timeout and integrate it with the gateway.

This article is divided in the following sections:

  1. Introduction.
  2. Web service invocation overview.
  3. Configuring a message sender.
  4. The sample application.
  5. Conclusion.
  • The source code can be found at github.

Web service invocation overview

The web service outbound gateway delegates the web service invocation to the Spring Web Services WebServiceTemplate. When a message arrives to the outbound gateway, this template uses a message sender in order to create a new connection. The diagram below shows an overview of the flow:

int-timeout-sequence

By default, the web service template sets an HttpUrlConnectionMessageSender as its message sender, which is a basic implementation without support for configuration options. This behavior though, can be overridden by setting a more advanced message sender with the capability of setting both read and connection timeouts.

We are going to configure the message sender in the next section.

Configuring a message sender

We are going to configure a message sender to the outbound gateway. This way, the gateway will set the template’s message sender with the one provided.

The implementation we are providing in the example is the HttpComponentsMessageSender class, also from the Spring Web Services project. This message sender allows us to define the following timeouts:

  • connectionTimeout: Sets the timeout until the connection is established.
  • readTimeout: Sets the socket timeout for the underlying HttpClient. This is the time required for the service to reply.

Configuration:

<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
    <property name="connectionTimeout" value="${timeout.connection}"/>
    <property name="readTimeout" value="${timeout.read}"/>
</bean>

The properties file contains the values, which are both set to two seconds:

timeout.connection=2000

timeout.read=2000

Once configured, we add it to the web service outbound gateway configuration:

<int-ws:outbound-gateway uri="http://localhost:8080/spring-ws-courses/courses" 
    marshaller="marshaller" unmarshaller="marshaller" 
    request-channel="requestChannel" message-sender="messageSender"/>

To use this message sender, you will need to add the following dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.3</version>
</dependency>

And that’s it; the next section will show the sample application to see how it works.

The sample application

The flow is simple; it consists in an application that sends a request to a web service and receives a response. The web service source code can be found at 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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/ws http://www.springframework.org/schema/integration/ws/spring-integration-ws.xsd
        http://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>

The gateway contains the method through which we will enter the messaging system:

public interface CourseService {
    
    @Gateway
    GetCourseResponse getCourse(GetCourseRequest request);
}

Finally, the test:

@ContextConfiguration(locations = {"/xpadro/spring/integration/ws/config/int-course-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestIntegrationApp {
    
    @Autowired
    private CourseService service;
    
    @Test
    public void invokeNormalOperation() {
        GetCourseRequest request = new GetCourseRequest();
        request.setCourseId("BC-45");
        
        GetCourseResponse response = service.getCourse(request);
        assertNotNull(response);
        assertEquals("Introduction to Java", response.getName());
    }
    
    @Test
    public 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);
        }
    }
}

Conclusion

We have learnt how to set additional options to the web service outbound gateway in order to establish a timeout. In the next post, I will explain how to cache this invocation.

Xavier Padro

Xavier is a software developer working in a consulting firm based in Barcelona. He is specialized in web application development with experience in both frontend and backend. He is interested in everything related to Java and the Spring framework.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ven
Ven
3 years ago

I am receiving read timeout error for 1st call after system is idle for 3 hours ..ERROR_MESSAGE_DEFINITION: Read timed out;I/O error: Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out; ERROR_MESSAGE_EXPLANATION: java.net.SocketTimeoutException;org.springframework.ws.client.WebServiceIOException; 

Ven
Ven
3 years ago

I tried increasing timeouts, still facing issue. I am deploying application on PCF environment.

Back to top button