Thursday, November 15, 2012

Polling an http end point using Spring Integration

It is a little non-intuitive if you want to write a flow with Spring Integration which polls an http end point and gathers some content from the http end point for further processing.

Spring Integration provides a couple of ways for integrating with a HTTP endpoint -

1. Http Outbound adapter - to send the messages to an http endpoint
2. Http Outbound gateway - to send messages to an http endpoint and to collect the response as a message

My first instinct to poll the http endpoint was to use a Http Inbound channel adapter, the wrong assumption that I made was that the adapter will be responsible for getting the information from an endpoint - what Http Inbound Gateway actually does is to expose an Http endpoint and wait for requests to come in! , this is why I started by saying that it was a little non-intuitive to me that to poll a URL and collect content from it I will actually have to use a Http Outbound gateway

With this clarified, consider an example where I want to poll the USGS Earth Quake information feed available at this url -

This is how my sample http Outbound component looks like:

<int:channel id="">
  <int:queue capacity="10"/>

 <int:channel id=""></int:channel> 

 <int-http:outbound-gateway id="quakerHttpGateway"

Here the http outbound gateway waits for messages to come into the quakeinfotrigger channel, sends out a GET request to the "" url, and places the response json string into the "" channel

Testing this is easy:

public class TestHttpOutboundGateway {
 @Autowired @Qualifier("") PollableChannel quakeinfoChannel;
 @Autowired @Qualifier("") MessageChannel quakeinfoTriggerChannel;

 public void testHttpOutbound() {
  Message<?> message = quakeinfoChannel.receive();
  assertThat(message.getPayload(), is(notNullValue()));


What I am doing here is getting a reference to the channel which triggers the outbound gateway to send a message to the http endpoint and reference to another channel where the response from the http endpoint is placed. I am triggering the test flow by placing a dummy empty message in the trigger channel and then waiting on message to be available on the response channel and asserting on the contents.

This works cleanly, however my original intent was to write a poller which would trigger polling of this endpoint once every minute or so, to do this what I have to do is essentially place a dummy message into the "" channel every minute and this is easily accomplished using a Spring Integration "poller" and a bit of Spring Expression language:

<int:inbound-channel-adapter channel="" expression="''">
 <int:poller fixed-delay="60000"></int:poller>

Here I have a Spring inbound-channel-adapter triggered attached to a poller , with the poller triggering an empty message every minute.

All this looks a little convoluted but works nicely - here is a gist with a working code


1. Based on a question I had posed at the Spring forum


  1. Great, but how do you mock the web service you are calling? In automated tests, you most certainly don't want to call an external service.

    Thank you.