How to handle rate limiting with twitter4j?

I often get the following exception using twitter4j:

2015-06-02 10:04:30,802 DEBUG [Twitter Stream consumer-1[Establishing connection]] TwitterBot(116): Got an exception 420:Returned by the Search and Trends API when you are being rate limited (https://dev.twitter.com/docs/rate-limiting).
Returned by the Streaming API:
 Too many login attempts in a short period of time.
 Running too many copies of the same application authenticating with the same account name.
Exceeded connection limit for user

      

Since I am trying to avoid being banned from Twitter, I would like to ask if I am doing something wrong in my code:

I am using StatusListener on Stream API, which is filtered by my own id and some string values.

If the status meets the criteria, a response to that status is sent via twitter. This doesn't happen very often and so it shouldn't be a rate limiting exception issue.

All of this works in TomEE EJB, if that matters?

@Startup
@Singleton
public class TwitterBot implements Service {

    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TwitterBot.class);

    //this is fix for this twitter bot
    public static final String TWITTER_BOT_NAME = "botname";
    public static final long TWITTER_BOT_USER_ID = 99999L; //the bot user id

    private final TwitterStream twitterStream;
    private final Twitter twitter;

    public TwitterBot() {

        this.twitterStream = new TwitterStreamFactory().getInstance();
        this.twitter = TwitterFactory.getSingleton();
    }


    @PostConstruct
    public void listen() {

        StatusListener listener = new StatusListener() {
            @Override
            public void onStatus(Status status) {

                //this is to avoid a circle... ignore tweets coming from ourselves.
                if (status.getUser().getScreenName().equalsIgnoreCase(TWITTER_BOT_NAME)) {
                    return;
                }

                try {

                    //do something and update own status

                    StatusUpdate update = new StatusUpdate("Hello World!");
                    update.setInReplyToStatusId(status.getId());

                    twitter.updateStatus(update);

                } catch (TwitterException e) {
                    logger.error("Could not complete twitter update, {}", e.getLocalizedMessage(), e);
                }

            }

           //other Status Listener methods, which are not used (default implementation)
        };

        //filtering for ourselves here
        long[] userFilter = {TWITTER_BOT_USER_ID};

        String[] termFilter = {TWITTER_EXPERTIZER_BOT_NAME};

        FilterQuery filter = new FilterQuery(0, userFilter, termFilter);

        twitterStream.addListener(listener);

        twitterStream.filter(filter);

    }
}

      

The answer to this How to handle rate limiting with twitter4j to avoid being banned tells me that the Streaming API has no rate limits.

So what's the problem? Is there an explanation in the API documentation?

Thank you in advance!

+3


source to share


1 answer


Edit:

The problem is related to

FilterQuery filter = new FilterQuery(0, userFilter, termFilter);

      



Using such a request triggers some kind of poll in the Twitter API and therefore exceeds the connection limit.

Use instead:

 FilterQuery filter = new FilterQuery(termFilter);

      

+1


source