Spring Integration – Configure web service client timeout

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. Hence, 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 configure web service 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 my Github repository.

1 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:

configure web service client timeout diagram

 

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.

2 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:

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.

3 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);
        }
    }
}

 

4 Conclusion

We have learnt how to set additional options to the web service outbound gateway in order to establish a timeout.

I’m publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.