1 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:
- Introduction.
- Web service invocation overview.
- Configuring a message sender.
- The sample application.
- Conclusion.
The source code can be found at github.
2 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:
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.
3 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:
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:
To use this message sender, you will need to add the following dependency:
And that’s it; the next section will show the sample application to see how it works.
4 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.
The gateway contains the method through which we will enter the messaging system:
Finally, the test:
5 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.
Labels: Integration, Spring, web services