stop the Twitter stream and return List of status with twitter4j

后端 未结 3 2080
逝去的感伤
逝去的感伤 2021-01-03 04:42

Using the code example provided by Twitter4j, I\'d like t stop the stream after a list of 1,000 status have been collected, and return this list. How can I do that?

3条回答
  •  灰色年华
    2021-01-03 05:14

    There are two ways:

    If you just need to finish the thread that consumes the stream (this is the thread that calls your listener) you can use twitterStream.cleanUp();. This will gracefully stop the thread. You might want to use a boolean stopped variable in your status listener so to ignore any calls that you will receive after this event.

    You can also close the stream consuming thread along with its dispatcher thread (that is a deamon thread) by calling twitterStream.shutdown(); However this is a more brutal approach of ending communication with the twitter API. Though it works if you call this from inside the listener, I would prefer the approach suggested by @krishnakumarp

提交回复
热议问题