Example usage for twitter4j AsyncTwitter addListener

List of usage examples for twitter4j AsyncTwitter addListener

Introduction

In this page you can find the example usage for twitter4j AsyncTwitter addListener.

Prototype

void addListener(TwitterListener listener);

Source Link

Document

Adds twitter listener

Usage

From source file:org.primefaces.examples.service.TwitterAPIService.java

License:Apache License

public List<Status> asyncSearch(String query) {

    TwitterListener listener = new TwitterAdapter() {
        @Override/*  www  . j  av a 2s .  c  om*/
        public void searched(QueryResult queryResult) {
            asyncTweets = queryResult.getTweets();
        }

        @Override
        public void onException(TwitterException e, TwitterMethod method) {
            logger.severe(e.getMessage());
        }
    };

    AsyncTwitter twitter = new AsyncTwitterFactory().getInstance();
    twitter.setOAuthConsumer(twitter_consumer_key, twitter_consumer_secret);
    twitter.setOAuthAccessToken(new AccessToken(access_token, access_token_secret));
    twitter.addListener(listener);
    Query q = new Query(query);
    twitter.search(q);
    return asyncTweets;
}

From source file:twitter4j.examples.async.AsyncUpdate.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.AsyncUpdate text");
        System.exit(-1);/*w ww .  j  av  a2  s  .  com*/
    }
    AsyncTwitterFactory factory = new AsyncTwitterFactory();
    AsyncTwitter twitter = factory.getInstance();
    twitter.addListener(new TwitterAdapter() {
        @Override
        public void updatedStatus(Status status) {
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
            synchronized (LOCK) {
                LOCK.notify();
            }
        }

        @Override
        public void onException(TwitterException e, TwitterMethod method) {
            if (method == UPDATE_STATUS) {
                e.printStackTrace();
                synchronized (LOCK) {
                    LOCK.notify();
                }
            } else {
                synchronized (LOCK) {
                    LOCK.notify();
                }
                throw new AssertionError("Should not happen");
            }
        }
    });
    twitter.updateStatus(args[0]);
    synchronized (LOCK) {
        LOCK.wait();
    }
}