5
我想通過twitter4j TwitterStream對象獲取twitter上的所有推文。我不確定我是否收到了所有的推文。爲了測試流媒體API返回推文之後的延遲,我在Twitter上發佈了一條來自我的賬戶的推文。但是,即使經過很長時間,我仍然沒有收到推文。Twitter4j TwitterStream不會獲得所有推文
twitter4j是否抓住twitter上發佈的每條推文,或者失去了很多推文的比例?或者我在這裏做錯了什麼? 下面是我使用來獲取鳴叫代碼:
StatusListener listener = new StatusListener(){
int countTweets = 0; // Count to implement batch processing
public void onStatus(Status status) {
countTweets ++;
StatusDto statusDto = new StatusDto(status);
session.saveOrUpdate(statusDto);
// Save 1 round of tweets to the database
if (countTweets == BATCH_SIZE) {
countTweets = 0;
session.flush();
session.clear();
}
}
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
public void onException(Exception ex) {
ex.printStackTrace();
}
public void onScrubGeo(long arg0, long arg1) {
// TODO Auto-generated method stub
}
};
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(Twitter4jProperties.CONSUMER_KEY)
.setOAuthConsumerSecret(Twitter4jProperties.CONSUMER_SECRET)
.setOAuthAccessToken(Twitter4jProperties.ACCESS_TOKEN)
.setOAuthAccessTokenSecret(Twitter4jProperties.ACCESS_TOKEN_SECRET);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
// sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
twitterStream.sample();