Java 如何使用twitter4j处理速率限制?
我经常使用twitter4j遇到以下异常:Java 如何使用twitter4j处理速率限制?,java,twitter,ejb,twitter4j,Java,Twitter,Ejb,Twitter4j,我经常使用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-li
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
由于我试图避免被禁止使用Twitter,我想问一下,如果我的代码中有错误:
我正在流API上使用StatusListener,它由我自己的ID和一些字符串值过滤
如果状态符合条件,则通过twitter发送此状态的答案。这种情况并不经常发生,因此这不应该是费率限制例外的问题
如果这很重要的话,那么整个过程都是在TOME EJB环境中运行的
@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's 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);
}
}
这个问题的答案告诉我,流式API没有速率限制
那么问题是什么呢?API文档中有解释吗
提前谢谢你 编辑:
这个问题与
FilterQuery filter = new FilterQuery(0, userFilter, termFilter);
像这样使用查询会在twitterapi上产生某种轮询,因此超出了连接限制
而是使用:
FilterQuery filter = new FilterQuery(termFilter);
问题与FilterQuery有关。如果我像其他stackoverflow帖子中描述的那样使用它,它就像一个没有速率限制的符咒。我将进一步关注这个问题。如果你最终找到了解决方案,请将此作为原始问题的答案发布