Java Reference
In-Depth Information
All this is provided by Spring Integration. All you have to do is avoid wastefully calling the service by
caching the results and feeding the results back until the ache is exhausted. Then, you just wait for the
next scheduled run. Simple, right? Let's look at the final result:
package com.apress.springenterpriserecipes.springintegration.twitter;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandler;
import org.springframework.integration.message.MessageSource;
import org.springframework.util.Assert;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
public class TwitterMessageSource implements MessageSource<Tweet>,
InitializingBean {
static private Logger logger = Logger.getLogger(TwitterMessageSource.class);
private volatile Queue<Tweet> cachedStatuses;
private volatile String userId;
private volatile String password;
private volatile Twitter twitter;
private volatile long lastStatusIdRetreived = -1;
private Tweet buildTweetFromStatus(Status firstPost) {
Tweet tweet = new Tweet(firstPost.getId(), firstPost.getUser()
.getName(), firstPost.getCreatedAt(), firstPost.getText());
return tweet;
}
public Message<Tweet> receive() {
Assert.state(cachedStatuses != null);
if (cachedStatuses.peek() == null) {
Paging paging = new Paging();
Search WWH ::




Custom Search