Over a million developers have joined DZone.

Spring Integration with Twitter: How to Receive Tweets From Your Twitter Account

DZone's Guide to

Spring Integration with Twitter: How to Receive Tweets From Your Twitter Account

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

In my previous post on Spring Integration with Twitter, we have seen “ How to post tweets to your Twitter Account using Spring Integration“.

In this post, I will show you how to receive the tweets from your Twitter Account.

If you were to run this example, you might have to pick the remaining missing configuration for logger etc from my previous post as mentioned above.

Spring Configuration

The Spring Configuration for receiving a Twitter update is shown below


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:twitter="http://www.springframework.org/schema/integration/twitter"
    <context:property-placeholder location="/twitter.properties" />
    <context:component-scan base-package="com.skilledmonster.spring.integration.twitter" />
    <int:channel id="twitterInbound" />
    <int:service-activator input-channel="twitterInbound"
        ref="twitterMessageConsumer" />
        channel="twitterInbound" twitter-template="twitterTemplate">
        <int:poller fixed-rate="5000" max-messages-per-poll="5" />

The inbound-update-channel-adapter is configured with the same twitter-template to send the Twitter update message to the twitterInbound channel. A poller element is required to pull the messages from the Twitter server every 5 seconds. A maximum of 5 messages are pulled everytime the server is polled for the messages.

Service Activator

The Service Activator class is used to receive the Twitter message as shown below

package com.skilledmonster.spring.integration.twitter;
import org.apache.log4j.Logger;
import org.springframework.integration.Message;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.stereotype.Component;
 * Twitter Message Consumer using the Service Activator
 * @author Jagadeesh
public class TwitterMessageConsumer {
    private static Logger LOG = Logger.getLogger(TwitterMessageConsumer.class);
    public void consume(Message<Tweet> message) {
        // get message payload
        Tweet tweet = message.getPayload();
        // log the received tweets
        LOG.info("Fetched Tweet Text from @" + tweet.getFromUser()+" # " + tweet.getText() );

Test Run

All that is required to run the Twitter inbound message example is to load the Spring Configuration file
package com.skilledmonster.spring.integration.twitter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 * Twitter Inbound class to pull the messages for the Twitter server
 * @author Jagadeesh
public class TwitterInbound {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("/twitter-inbound.xml");
        Thread.sleep(5 * 60 * 1000);
A five-minute delay is added to the code to wait for any new tweets


Spring Integration Documentation

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.


Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}