I have managed to get the live tweets for a certain keyword. So far so good, I tried to add some RateLimitStatusListener
in case I reach rate limit.
The problem is, never gets called. Even if I reach rate limit it does not get called. I could not figure it out. My intention is, if I get rate limit error do some error handling. Despite that is there any other way to check whether rate limit has been reached?
public final class PrintSampleStream {
public static void main(String[] args) throws TwitterException {
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
StatusListener listener = new StatusListener() {
@Override
public void onStatus(Status status) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
};
twitterStream.addRateLimitStatusListener( new RateLimitStatusListener() {
@Override
public void onRateLimitStatus( RateLimitStatusEvent event ) {
System.out.println("Limit["+event.getRateLimitStatus().getLimit() + "], Remaining[" +event.getRateLimitStatus().getRemaining()+"]");
}
@Override
public void onRateLimitReached( RateLimitStatusEvent event ) {
System.out.println("Limit["+event.getRateLimitStatus().getLimit() + "], Remaining[" +event.getRateLimitStatus().getRemaining()+"]");
}
} );
FilterQuery filterQuery = new FilterQuery();
String[] query = {"#WorldCup2014"};
filterQuery.track(query);
twitterStream.addListener(listener);
twitterStream.filter(filterQuery);
}
}
The rate limits you are mentioning applies only for twitter rest api, not streaming api. In streaming, there is no need to add code like that, because limitations are handled in a different way:
You will be streamed 1% of total volume
Which means you will get 1% of what you are tracking or following in streaming api.
How are rate limits determined on the Streaming API?
The public streaming APIs cap the number of messages sent to your client to a small fraction of the total volume of Tweets at any given moment.
The sample hose, as documented in https://stream.twitter.com/1/statuses/sample.json, delivers a random sampling of all Tweets at a volume equal to the public streaming cap.
Filtered streams return all matching Tweets up to a volume equal to the streaming cap. If there are more tweets that would match your criteria, you'll be streamed a rate limit message indicating how many tweets were not delivered.