Example usage for twitter4j StatusStream next

List of usage examples for twitter4j StatusStream next

Introduction

In this page you can find the example usage for twitter4j StatusStream next.

Prototype

void next(StatusListener listener) throws TwitterException;

Source Link

Document

Reads next status from this stream.

Usage

From source file:org.drools.examples.twittercbr.offline.TwitterDumper.java

License:Apache License

public static void main(String[] args) throws TwitterException, IOException, InterruptedException {
    final ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream("src/main/resources/twitterstream.dump"));

    final TwitterStream twitterStream = new TwitterStreamFactory().getInstance();

    final StatusListener listener = new StatusListener() {
        private int counter = 0;

        @Override/*from w w w  . j  ava 2s.  c  o  m*/
        public void onException(Exception arg0) {
        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

        @Override
        public void onScrubGeo(long arg0, long arg1) {
        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
        }

        @Override
        public void onStatus(Status arg0) {
            counter++;
            if (counter % 100 == 0) {
                System.out.println("Message = " + counter);
            }
            try {
                oos.writeObject(arg0);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }

        @Override
        public void onStallWarning(StallWarning arg0) {
        }
    };
    StatusStream stream = twitterStream.getSampleStream();
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < 300000) {
        stream.next(listener);
    }
    twitterStream.shutdown();
    Thread.currentThread().sleep(10000);
    oos.close();
}

From source file:org.example.TwitterDumper.java

License:Apache License

public static void main(String[] args) throws TwitterException, IOException, InterruptedException {
    final ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream("src/main/resources/twitterstream.dump"));

    final TwitterStream twitterStream = new TwitterStreamFactory().getInstance();

    final StatusListener listener = new StatusListener() {
        private int counter = 0;

        public void onException(Exception arg0) {
        }//from  w  w w.  j av a2  s. c  om

        public void onDeletionNotice(StatusDeletionNotice arg0) {
        }

        public void onScrubGeo(long arg0, long arg1) {
        }

        public void onTrackLimitationNotice(int arg0) {
        }

        public void onStatus(Status arg0) {
            counter++;
            if (counter % 100 == 0) {
                System.out.println("Message = " + counter);
            }
            try {
                oos.writeObject(arg0);
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }

        public void onStallWarning(StallWarning arg0) {
        }
    };
    StatusStream stream = twitterStream.getSampleStream();
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < 300000) {
        stream.next(listener);
    }
    twitterStream.shutdown();
    Thread.currentThread().sleep(10000);
    oos.close();
}