Question

i'm trying to execute different Thread using different TwitterStream object with an single authentication:

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); 
}

In every Thread i do:

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);
    }

But it gives me authentication error.. multiple request of the same authentication. How can i solve?

Was it helpful?

Solution

Here is how I did it:

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;
}
}

Each thread will contain something like this.

twitter = TwitterFactory.getSingleton();

Will make sure you are reusing the same connection each time.

twitter.addListener(listener);

Will add a listener so that you will get called back to this thread (but you will get called back from every query added)

twitter.filter(fq);

Will add a new search query to follow.

isMyHashTagTweet(tweet)

Will check to make sure that the tweet returned by all queries live in your twitterStream are relevant for your current thread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top