DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Spring Integration with Twitter: How to Receive Tweets From Your Twitter Account

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

Jagadeesh Motamarri user avatar by
Jagadeesh Motamarri
·
Mar. 13, 13 · Interview
Like (0)
Save
Tweet
Share
6.24K Views

Join the DZone community and get the full member experience.

Join For Free
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

twitter-inbound.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:twitter="http://www.springframework.org/schema/integration/twitter"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/integration/twitter
 
http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd
 
 
http://www.springframework.org/schema/beans
 
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 
 
http://www.springframework.org/schema/integration
 
 
http://www.springframework.org/schema/integration/spring-integration.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context.xsd">
 
    <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" />
 
    <twitter:inbound-channel-adapter
        channel="twitterInbound" twitter-template="twitterTemplate">
        <int:poller fixed-rate="5000" max-messages-per-poll="5" />
    </twitter:inbound-channel-adapter>
 
</beans>

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
 *
 */
@Component
public class TwitterMessageConsumer {
 
    private static Logger LOG = Logger.getLogger(TwitterMessageConsumer.class);
 
    @ServiceActivator
    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

References:

Spring Integration Documentation

twitter Spring Integration Spring Framework Integration

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What To Know Before Implementing IIoT
  • Event Driven 2.0
  • Top 10 Best Practices for Web Application Testing
  • A Beginner's Guide to Infrastructure as Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: