Example usage for twitter4j TwitterStream cleanUp

List of usage examples for twitter4j TwitterStream cleanUp

Introduction

In this page you can find the example usage for twitter4j TwitterStream cleanUp.

Prototype

TwitterStream cleanUp();

Source Link

Document

shutdown internal stream consuming thread

Usage

From source file:StreamToFiles.java

License:Apache License

public static void main(String[] args) throws TwitterException, FileNotFoundException {
    String oAuthFileName = "mjlauKeys.auth";
    outFile = args[0];/*w w w . j  a  va2s.  com*/

    ConfigurationBuilder cb = Utils.createConfigurationBuilder(new File(oAuthFileName));
    if (cb == null) {
        throw new IllegalArgumentException("Configuration File Issues");
    }

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    StatusListener listener = new StatusListener() {
        long counter = 0;
        PrintWriter outputWriter = new PrintWriter(new File(outFile));

        @Override
        public void onStatus(Status status) {
            // System.out.println("@" + status.getUser().getScreenName()
            // + " - " + status.getText());
            if (status.getText().contains("#")) {
                Utils.sanitizeAndWriteTweet(status.getText(), outputWriter);
                counter++;
                if (counter % 1000 == 0) {
                    System.out.println("done with " + counter);
                }
                if (counter >= NUM_TWEETS) {
                    twitterStream.cleanUp();
                    twitterStream.shutdown();
                    outputWriter.close();
                }

            }
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            // System.out.println("Got a status deletion notice id:"
            // + statusDeletionNotice.getStatusId());
        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            // System.out.println("Got track limitation notice:"
            // + numberOfLimitedStatuses);
        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {
            // System.out.println("Got scrub_geo event userId:" + userId
            // + " upToStatusId:" + upToStatusId);
        }

        @Override
        public void onStallWarning(StallWarning warning) {
            // System.out.println("Got stall warning:" + warning);
        }

        @Override
        public void onException(Exception ex) {
            ex.printStackTrace();
        }
    };
    twitterStream.addListener(listener);
    twitterStream.sample("en");
}

From source file:com.carolinarollergirls.scoreboard.viewer.TwitterViewer.java

License:GNU General Public License

protected void reset() {
    if (null != twitter)
        twitter.shutdown();//from   w w  w . j a v a 2 s  . com
    twitter = twitterFactory.getInstance();
    userId = 0;
    requestToken = null;
    if (null != twitterStream) {
        // cleanUp has a bug where it blocks until its internal thread
        // blocks until a new status comes in or it times out.
        // We don't need or want to wait so let's do the cleanup
        // in a separate thread.
        final TwitterStream tS = twitterStream;
        Runnable r = new Runnable() {
            public void run() {
                tS.cleanUp();
            }
        };
        new Thread(r).start();
    }
    twitterStream = null;
    loggedIn = false;
}

From source file:com.ebay.pulsar.twittersample.channel.TwitterSampleChannel.java

License:GNU General Public License

@Override
public void shutDown() {
    TwitterStream s = twitterStream;
    if (s != null) {
        twitterStream = null;//from w  w w . ja va 2 s.c  o  m
        s.cleanUp();
    }
}

From source file:com.waves_rsp.ikb4stream.datasource.twitter.TwitterProducerConnector.java

License:Open Source License

/**
 * Listen tweets from twitter with a bounding box and load them with the data producer object
 *
 * @param dataProducer {@link IDataProducer} contains the data queue
 * @see TwitterProducerConnector#confBuilder
 * @see TwitterProducerConnector#boundingBox
 *//*from www.  j  a  v  a  2s  .co  m*/
@Override
public void load(IDataProducer dataProducer) {
    Objects.requireNonNull(dataProducer);
    TwitterStream twitterStream = null;
    try {
        TwitterStreamListener streamListener = new TwitterStreamListener(dataProducer);
        twitterStream = new TwitterStreamFactory(confBuilder.build()).getInstance();
        FilterQuery filterQuery = new FilterQuery();
        filterQuery.locations(boundingBox);
        twitterStream.addListener(streamListener);
        twitterStream.filter(filterQuery);
        Thread.currentThread().join();
    } catch (IllegalArgumentException | IllegalStateException err) {
        LOGGER.error("Error loading : " + err.getMessage());
        throw new IllegalStateException(err.getMessage());
    } catch (InterruptedException e) {
        LOGGER.info("Close twitter");
        Thread.currentThread().interrupt();
    } finally {
        if (twitterStream != null) {
            twitterStream.cleanUp();
            twitterStream.shutdown();
        }
        Thread.currentThread().interrupt();
    }
}

From source file:de.botshield.CaptureFilterStream.java

License:Apache License

public void execute(long[] followArray, String[] trackArray) {
    // try to set up new stream, when the listener fails due to HTTP timeout
    // for example

    this.initializeStreamListener();

    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(listener);
    try {// w  w  w  .  j  a  v  a  2s . com
        // filter() method internally creates a thread which manipulates
        // TwitterStream and calls these adequate listener methods
        // continuously.
        twitterStream.filter(new FilterQuery(0, followArray, trackArray));
    } catch (Exception ex) {
        twitterStream.removeListener(listener);
        twitterStream.cleanUp();
    }
}

From source file:org.primeoservices.cfgateway.twitter.utils.TwitterUtils.java

License:Apache License

/**
 * Unconditionally close a <code>TwitterStream</code>
 * // w ww .j ava2 s . c  om
 * @param stream the stream to be closed
 */
public static void closeQuietly(final TwitterStream stream) {
    if (stream == null)
        return;
    try {
        stream.cleanUp();
        stream.shutdown();
    } catch (Throwable t) {
        // ignore
    }
}

From source file:org.seasr.meandre.apps.twitter.TwitterToTuple.java

public void run() {
    try {/*from w w w .ja v a  2s . co  m*/
        Thread.currentThread().sleep(5000);
        console.info("Start Twitter reader");

        /*
        Twitter tweet = new Twitter("seasrSalad" , "0penNlp");
        Status status = tweet.updateStatus("Hello world2 {\"name\":\"mike\"}");
        User me = status.getUser();
        console.info("user is " + me.getId());
        */

        // twitterStream = new TwitterStream("seasrSalad", "0penNlp", this);

        // this will force an exception if invalid
        TwitterStream tsTmp = new TwitterStreamFactory().getInstance(userName, passwd);
        StatusStream tmp = tsTmp.getSampleStream();
        tmp.close();
        tsTmp.cleanup();

        // if we got this far, we are good to go
        twitterStream = new TwitterStreamFactory().getInstance(userName, passwd);
        twitterStream.setStatusListener(this);

        // for a continuous unfiltered stream:
        twitterStream.sample();

        /*
        int count = 0; // last 100 messages, NOT supported
        int[] follow = new int[0]; // don't follow any specific users
        String[] tags = new String[]{"#apple", "Balloon Boy"};
        twitterStream.filter(count, follow, tags);
        */

        // black forever
        waitForStatus();

    } catch (Exception e) {
        console.info("unable to read twitter " + e.toString());
    } finally {
        if (twitterStream != null)
            twitterStream.cleanup();
    }

    console.info("Stop Twitter service");

    noExceptions = false;
    synchronized (buffer) {
        buffer.notifyAll();
    }

}

From source file:socialImport.twitter.TwitterImport.java

License:Open Source License

@Override
public void closeStream(StreamDescriptor streamDescriptor) {
    TwitterStream twitterStream = streams.remove(streamDescriptor);

    if (twitterStream != null) {
        twitterStream.cleanUp();
    }//  w  ww  .  j  av a2  s.c  o m
}