如何管理来自不同线程的不同TwitterStream



我正在尝试使用不同的TwitterStream对象执行不同的线程,只需一次身份验证:

public class Support {
    private static final String accessToken = "xxxxxxx";
    private static final String accessTokenSecret = "xxxxxxx";
    public static final AccessToken token = new AccessToken(accessToken, accessTokenSecret); 
    private static final String consumerKey = "xxxxxxx";
    private static final String consumerSecret = "xxxxxxx";
    private static final Configuration conf = new ConfigurationBuilder().setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).build(); 
    public static TwitterStreamFactory factory = new TwitterStreamFactory(conf); 
}

在我做的每一个线程中:

public MyThread1(){
   this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
        StatusListener listener = ... ;
        twitterStream.addListener(listener);
        FilterQuery fq = new FilterQuery();
        fq.track(new String[]{"hashtag1","hashtag2"});
        twitterStream.filter(fq);
    }
public MyThread2(){
   this.twitterStream = Support.factory.getInstance(Support.token);
}
public void run(){
        StatusListener listener = ... ;
        twitterStream.addListener(listener);
        FilterQuery fq = new FilterQuery();
        fq.track(new String[]{"hashtag3","hashtag4"});
        twitterStream.filter(fq);
    }

但它给了我身份验证错误。。同一身份验证的多个请求。我该如何解决?

以下是我的操作方法:

public class MyTwitterApp implements {
private Twitter twitter;
private Query query;
public MyTwitterApp (){
    twitter = TwitterFactory.getSingleton();
}
public static void main(String[] args) {
    MyTwitterApp twitterApp  = new MyTwitterApp();
    twitterApp.getStreamingTweets();
}
public void getStreamingTweets(){
    StatusListener listener = new StatusListener(){
        public void onStatus(Status status) {
                handleStatus(status);
        }
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}
        public void onException(Exception ex) {ex.printStackTrace(); }
        public void onScrubGeo(long arg0, long arg1) {}
        public void onStallWarning(StallWarning arg0) {}
    };
    twitter.addListener(listener);
    FilterQuery fq = new FilterQuery();
    fq.count(0);
    fq.track(new String[]{"#MyHashTag"});
    twitter.filter(fq);
}
protected void handleStatus(Status tweet) {
    if(tweet.isRetweet()){
        return;
    }
    if(isMyHashTagTweet(tweet)){
        //do something with tweet here      
    }       
}
private boolean isMyHashTagTweet(Status tweet) {
    HashtagEntity[] htes = tweet.getHashtagEntities();
    for(HashtagEntity hte : htes){
        if(hte.getText().equalsIgnoreCase("myhashtag")) {
            return true;
        }
    }
    return false;
}
}

每个线程都将包含这样的内容。

twitter = TwitterFactory.getSingleton();

将确保您每次都重复使用相同的连接。

twitter.addListener(listener);

将添加一个监听器,这样您将被回调到此线程(但您将从添加的每个查询中被回调)

twitter.filter(fq);

将添加一个新的搜索查询。

isMyHashTagTweet(tweet)

将检查以确保twitterStream中所有查询返回的tweet与当前线程相关

相关内容

  • 没有找到相关文章

最新更新