如何从不同的Thread管理不同的TwitterStream

时间:2013-02-20 17:38:42

标签: java twitter4j twitter-streaming-api

我正在尝试使用不同的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);
    }

但它给了我身份验证错误..同一身份验证的多个请求。我怎么解决?

1 个答案:

答案 0 :(得分:1)

我是这样做的:

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中存在的所有查询返回的推文与您当前的线程相关