Tag Archives: twitter

Easy integration with Spring Integration

So I’ve had the opportunity to use Spring Integration recently and I thought I’d do a blog post about it – partly for my own review/recap/learning but also for anyone out there looking to play around with it. In a nutshell Spring Integration lets you integrate with other systems without having to deal with all the boilerplate concerns of your typical enterprise integration solutions. It provides a number of integration adapters straight out of the box for common integration methods such as: files, jms, jdbc, ftp and so on (take a look at the Spring doco for the full set).

The general gist of things has you defining a series of channels of which the endpoint may be the entry or exit to the system. One endpoint may be the entry point to another channel. E.g. in the following A<——–>B is a channel with A as the input and B as the output endpoints. B<——–>C is another channel where the output B from the A<——–>B channel is the input and C the output.


To really see how it all hangs together I’ve knocked together a little working example to integrate with Twitter using the built in Twitter Adapter. The app basically polls Twitter with a search on a hashtag, adds a field to the header then passes the message on to a service to be dealt with – in this case we are just going to print out to System.out. Sounds complicating, but Spring makes it easy – take a look at src/main/resources/twitter-integration.xml.

<channel id="inboundMentionsChannel"/>
<channel id="inputServiceActivatorChannel"/>

This defines the channels, note that we haven’t actually specified what the endpoints are yet…


<twitter:search-inbound-channel-adapter query="#springintegration" channel="inboundMentionsChannel">
<poller fixed-rate="5000" max-messages-per-poll="3"/>

<header-enricher input-channel="inboundMentionsChannel" output-channel="inputServiceActivatorChannel">
<header name="headerField1" value="dummy header value"/>


This is the interesting stuff where we link all the endpoints and channels together. Lines 1-3 define the inbound adapter (endpoint) which connects the system with Twitter. This is a search-inbound-adapter that polls for a twitter search using the specified query – in this case the hashtag #springintegration.

Lines 4-7 defines a header enricher. This allows us to pop values into the message header that can be used downstream. For this example we’re just populating a field called ‘headerField1’ with a dummy value. We’re not actually going to use this field for anything in the example but I wanted to show how easy it was to add info to the message header. The header enricher also has an input channel and output channel specified. As you may have worked out the input to the header enricher is from the endpoint of the ‘inboundMentionsChannel’, which in this case will contain the results from the Twitter search. And the outbound channel is to a service activator which references the TwitterService bean (bean is autowired).

If you take a look at TwitterService.java you’ll see that it prints out to System.out and adds the tweet to a list. Running the test case TwitterIntegrationTest.java you can check that the output matches the expected tweet search result count of 3.