twitter4j
Class TwitterStreamImpl

java.lang.Object
  extended by twitter4j.TwitterBaseImpl
      extended by twitter4j.TwitterStreamImpl
All Implemented Interfaces:
java.io.Serializable, OAuthSupport, twitter4j.internal.http.HttpResponseListener, TwitterBase, TwitterStream

 class TwitterStreamImpl
extends TwitterBaseImpl
implements TwitterStream

A java representation of the Streaming API: Methods
Note that this class is NOT compatible with Google App Engine as GAE is not capable of handling requests longer than 30 seconds.

Since:
Twitter4J 2.0.4
Author:
Yusuke Yamamoto - yusuke at mac.com

Nested Class Summary
(package private)  class TwitterStreamImpl.TwitterStreamConsumer
           
 
Field Summary
(package private) static int count
           
private static twitter4j.internal.async.Dispatcher dispatcher
           
private  TwitterStreamImpl.TwitterStreamConsumer handler
           
private  twitter4j.internal.http.HttpClientWrapper http
           
private static int HTTP_ERROR_INITIAL_WAIT
           
private static int HTTP_ERROR_WAIT_CAP
           
private  java.util.List<ConnectionLifeCycleListener> lifeCycleListeners
           
private static twitter4j.internal.logging.Logger logger
           
private static int NO_WAIT
           
private static int numberOfHandlers
           
private static long serialVersionUID
           
private  StreamListener[] streamListeners
           
private static int TCP_ERROR_INITIAL_WAIT
           
private static int TCP_ERROR_WAIT_CAP
           
 
Fields inherited from class twitter4j.TwitterBaseImpl
auth, conf, id, screenName
 
Constructor Summary
TwitterStreamImpl(Configuration conf)
           
TwitterStreamImpl(Configuration conf, Authorization auth)
           
 
Method Summary
 void addConnectionLifeCycleListener(ConnectionLifeCycleListener listener)
          Adds a ConnectionLifeCycleListener
 void addListener(SiteStreamsListener listener)
          
 void addListener(StatusListener listener)
          
private  void addListener(StreamListener listener)
           
 void addListener(UserStreamListener listener)
          
 void cleanUp()
          shutdown internal stream consuming thread
private  void ensureListenerIsSet()
          check if any listener is set.
private  void ensureStatusStreamListenerIsSet()
           
 boolean equals(java.lang.Object o)
           
 void filter(FilterQuery query)
          Start consuming public statuses that match one or more filter predicates.
 void firehose(int count)
          Starts listening on all public statuses.
private  StatusStream getCountStream(java.lang.String relativeUrl, int count)
           
private  twitter4j.internal.async.Dispatcher getDispatcher()
           
 StatusStream getFilterStream(FilterQuery query)
          Returns public statuses that match one or more filter predicates.
 StatusStream getFirehoseStream(int count)
          Returns a status stream of all public statuses.
 StatusStream getLinksStream(int count)
          Returns a status stream of all public statuses containing links.
 StatusStream getRetweetStream()
          Returns a stream of all retweets.
 StatusStream getSampleStream()
          Returns a stream of random sample of all public statuses.
(package private)  java.io.InputStream getSiteStream(boolean withFollowings, long[] follow)
           
 UserStream getUserStream()
          User Streams provides real-time updates of all data needed to update a desktop application display.
 UserStream getUserStream(java.lang.String[] track)
          User Streams provides real-time updates of all data needed to update a desktop application display.
 int hashCode()
           
 void links(int count)
          Starts listening on all public statuses containing links.
 void retweet()
          Starts listening on all retweets.
 void sample()
          Starts listening on random sample of all public statuses.
 void shutdown()
          
 void site(boolean withFollowings, long[] follow)
          Site Streams, a new feature on the Streaming API, is now available for beta testing.
private  void startHandler(TwitterStreamImpl.TwitterStreamConsumer handler)
           
 void user()
          User Streams provides real-time updates of all data needed to update a desktop application display.
 void user(java.lang.String[] track)
          User Streams provides real-time updates of all data needed to update a desktop application display.
 
Methods inherited from class twitter4j.TwitterBaseImpl
addRateLimitStatusListener, ensureAuthorizationEnabled, ensureOAuthEnabled, fillInIDAndScreenName, getAuthorization, getConfiguration, getId, getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthRequestToken, getOAuthRequestToken, getScreenName, httpResponseReceived, setOAuthAccessToken, setOAuthConsumer, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface twitter4j.auth.OAuthSupport
getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthAccessToken, getOAuthRequestToken, getOAuthRequestToken, setOAuthAccessToken, setOAuthConsumer
 
Methods inherited from interface twitter4j.TwitterBase
addRateLimitStatusListener, getAuthorization, getConfiguration, getId, getScreenName
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

http

private final twitter4j.internal.http.HttpClientWrapper http

logger

private static final twitter4j.internal.logging.Logger logger

streamListeners

private StreamListener[] streamListeners

lifeCycleListeners

private java.util.List<ConnectionLifeCycleListener> lifeCycleListeners

handler

private TwitterStreamImpl.TwitterStreamConsumer handler

dispatcher

private static transient twitter4j.internal.async.Dispatcher dispatcher

numberOfHandlers

private static int numberOfHandlers

TCP_ERROR_INITIAL_WAIT

private static final int TCP_ERROR_INITIAL_WAIT
See Also:
Constant Field Values

TCP_ERROR_WAIT_CAP

private static final int TCP_ERROR_WAIT_CAP
See Also:
Constant Field Values

HTTP_ERROR_INITIAL_WAIT

private static final int HTTP_ERROR_INITIAL_WAIT
See Also:
Constant Field Values

HTTP_ERROR_WAIT_CAP

private static final int HTTP_ERROR_WAIT_CAP
See Also:
Constant Field Values

NO_WAIT

private static final int NO_WAIT
See Also:
Constant Field Values

count

static int count
Constructor Detail

TwitterStreamImpl

TwitterStreamImpl(Configuration conf,
                  Authorization auth)

TwitterStreamImpl

TwitterStreamImpl(Configuration conf)
Method Detail

firehose

public void firehose(int count)
Starts listening on all public statuses. Available only to approved parties and requires a signed agreement to access. Please do not contact us about access to the firehose. If your service warrants access to it, we'll contact you.

Specified by:
firehose in interface TwitterStream
Parameters:
count - Indicates the number of previous statuses to stream before transitioning to the live stream.
See Also:
StatusStream, Streaming API: Methods statuses/firehose

getFirehoseStream

public StatusStream getFirehoseStream(int count)
                               throws TwitterException
Returns a status stream of all public statuses. Available only to approved parties and requires a signed agreement to access. Please do not contact us about access to the firehose. If your service warrants access to it, we'll contact you.

Specified by:
getFirehoseStream in interface TwitterStream
Parameters:
count - Indicates the number of previous statuses to stream before transitioning to the live stream.
Returns:
StatusStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
StatusStream, Streaming API: Methods statuses/firehose

links

public void links(int count)
Starts listening on all public statuses containing links. Available only to approved parties and requires a signed agreement to access. Please do not contact us about access to the links stream. If your service warrants access to it, we'll contact you.

Specified by:
links in interface TwitterStream
Parameters:
count - Indicates the number of previous statuses to stream before transitioning to the live stream.
See Also:
StatusStream, Streaming API: Methods statuses/links

getLinksStream

public StatusStream getLinksStream(int count)
                            throws TwitterException
Returns a status stream of all public statuses containing links. Available only to approved parties and requires a signed agreement to access. Please do not contact us about access to the links stream. If your service warrants access to it, we'll contact you.

Specified by:
getLinksStream in interface TwitterStream
Parameters:
count - Indicates the number of previous statuses to stream before transitioning to the live stream.
Returns:
StatusStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
StatusStream, Streaming API: Methods statuses/links

getCountStream

private StatusStream getCountStream(java.lang.String relativeUrl,
                                    int count)
                             throws TwitterException
Throws:
TwitterException

retweet

public void retweet()
Starts listening on all retweets. The retweet stream is not a generally available resource. Few applications require this level of access. Creative use of a combination of other resources and various access levels can satisfy nearly every application use case. As of 9/11/2009, the site-wide retweet feature has not yet launched, so there are currently few, if any, retweets on this stream.

Specified by:
retweet in interface TwitterStream
See Also:
StatusStream, Streaming API: Methods statuses/retweet

getRetweetStream

public StatusStream getRetweetStream()
                              throws TwitterException
Returns a stream of all retweets. The retweet stream is not a generally available resource. Few applications require this level of access. Creative use of a combination of other resources and various access levels can satisfy nearly every application use case. As of 9/11/2009, the site-wide retweet feature has not yet launched, so there are currently few, if any, retweets on this stream.

Specified by:
getRetweetStream in interface TwitterStream
Returns:
StatusStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
StatusStream, Streaming API: Methods statuses/retweet

sample

public void sample()
Starts listening on random sample of all public statuses. The default access level provides a small proportion of the Firehose. The "Gardenhose" access level provides a proportion more suitable for data mining and research applications that desire a larger proportion to be statistically significant sample.

Specified by:
sample in interface TwitterStream
See Also:
StatusStream, Streaming API: Methods statuses/sample

getSampleStream

public StatusStream getSampleStream()
                             throws TwitterException
Returns a stream of random sample of all public statuses. The default access level provides a small proportion of the Firehose. The "Gardenhose" access level provides a proportion more suitable for data mining and research applications that desire a larger proportion to be statistically significant sample.

Specified by:
getSampleStream in interface TwitterStream
Returns:
StatusStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
StatusStream, Streaming API: Methods statuses/sample

user

public void user()
User Streams provides real-time updates of all data needed to update a desktop application display. Applications can request startup back-fill from the REST API and then transition to Streaming for nearly all subsequent reads. Rate limits and latency are practically eliminated. Desktop developers can stop managing rate limits and use this new data to create an entirely new user experience. On our end, we hope to reduce costs and increase site reliability.

Specified by:
user in interface TwitterStream
See Also:
User Streams

user

public void user(java.lang.String[] track)
User Streams provides real-time updates of all data needed to update a desktop application display. Applications can request startup back-fill from the REST API and then transition to Streaming for nearly all subsequent reads. Rate limits and latency are practically eliminated. Desktop developers can stop managing rate limits and use this new data to create an entirely new user experience. On our end, we hope to reduce costs and increase site reliability.

Specified by:
user in interface TwitterStream
Parameters:
track - keywords to track
See Also:
User Streams

getUserStream

public UserStream getUserStream()
                         throws TwitterException
User Streams provides real-time updates of all data needed to update a desktop application display. Applications can request startup back-fill from the REST API and then transition to Streaming for nearly all subsequent reads. Rate limits and latency are practically eliminated. Desktop developers can stop managing rate limits and use this new data to create an entirely new user experience. On our end, we hope to reduce costs and increase site reliability.

Specified by:
getUserStream in interface TwitterStream
Returns:
UserStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
User Streams

getUserStream

public UserStream getUserStream(java.lang.String[] track)
                         throws TwitterException
User Streams provides real-time updates of all data needed to update a desktop application display. Applications can request startup back-fill from the REST API and then transition to Streaming for nearly all subsequent reads. Rate limits and latency are practically eliminated. Desktop developers can stop managing rate limits and use this new data to create an entirely new user experience. On our end, we hope to reduce costs and increase site reliability.

Specified by:
getUserStream in interface TwitterStream
Parameters:
track - keywords to track
Returns:
UserStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
User Streams

site

public void site(boolean withFollowings,
                 long[] follow)
Site Streams, a new feature on the Streaming API, is now available for beta testing. Site Streams allows services, such as web sites or mobile push services, to receive real-time updates for a large number of users without any of the hassles of managing REST API rate limits. The initial version delivers events created by, or directed to, users that have shared their OAuth token with your application. The following events are streamed immediately, and without rate limits: Home Timelines, Mentions Timelines, User Timelines, Direct Messages, Mentions, Follows, Favorites, Tweets, Retweets, Profile changes, and List changes. The following limitations must be respected during the beta period. These limitations may be changed with little advance notice. We intend to increase or remove these various limitations as we move from beta test into full production:
Limit the follow count to 100 users per stream. Clients must occasionally compact users onto a smaller number of connections to minimize the total number of connections outstanding.
Open no more than 25 new connections per second and exponentially back-off on errors.

Specified by:
site in interface TwitterStream
Parameters:
withFollowings - whether to receive status updates from people following
follow - an array of users to include in the stream
See Also:
Site Streams | dev.twitter.com

getDispatcher

private twitter4j.internal.async.Dispatcher getDispatcher()

getSiteStream

java.io.InputStream getSiteStream(boolean withFollowings,
                                  long[] follow)
                            throws TwitterException
Throws:
TwitterException

filter

public void filter(FilterQuery query)
Start consuming public statuses that match one or more filter predicates. At least one predicate parameter, follow, locations, or track must be specified. Multiple parameters may be specified which allows most clients to use a single connection to the Streaming API. Placing long parameters in the URL may cause the request to be rejected for excessive URL length.
The default access level allows up to 200 track keywords, 400 follow userids and 10 1-degree location boxes. Increased access levels allow 80,000 follow userids ("shadow" role), 400,000 follow userids ("birddog" role), 10,000 track keywords ("restricted track" role), 200,000 track keywords ("partner track" role), and 200 10-degree location boxes ("locRestricted" role). Increased track access levels also pass a higher proportion of statuses before limiting the stream.

Specified by:
filter in interface TwitterStream
Parameters:
query - Filter query
See Also:
StatusStream, Streaming API: Methods statuses/filter

getFilterStream

public StatusStream getFilterStream(FilterQuery query)
                             throws TwitterException
Returns public statuses that match one or more filter predicates. At least one predicate parameter, follow, locations, or track must be specified. Multiple parameters may be specified which allows most clients to use a single connection to the Streaming API. Placing long parameters in the URL may cause the request to be rejected for excessive URL length.
The default access level allows up to 200 track keywords, 400 follow userids and 10 1-degree location boxes. Increased access levels allow 80,000 follow userids ("shadow" role), 400,000 follow userids ("birddog" role), 10,000 track keywords ("restricted track" role), 200,000 track keywords ("partner track" role), and 200 10-degree location boxes ("locRestricted" role). Increased track access levels also pass a higher proportion of statuses before limiting the stream.

Specified by:
getFilterStream in interface TwitterStream
Parameters:
query - Filter query
Returns:
StatusStream
Throws:
TwitterException - when Twitter service or network is unavailable
See Also:
StatusStream, Twitter API Wiki / Streaming API Documentation - filter

ensureListenerIsSet

private void ensureListenerIsSet()
check if any listener is set. Throws IllegalStateException if no listener is set.

Throws:
java.lang.IllegalStateException - when no listener is set.

ensureStatusStreamListenerIsSet

private void ensureStatusStreamListenerIsSet()

startHandler

private void startHandler(TwitterStreamImpl.TwitterStreamConsumer handler)

cleanUp

public void cleanUp()
shutdown internal stream consuming thread

Specified by:
cleanUp in interface TwitterStream

shutdown

public void shutdown()

Specified by:
shutdown in interface TwitterBase
Specified by:
shutdown in interface TwitterStream
Overrides:
shutdown in class TwitterBaseImpl

addConnectionLifeCycleListener

public void addConnectionLifeCycleListener(ConnectionLifeCycleListener listener)
Adds a ConnectionLifeCycleListener

Specified by:
addConnectionLifeCycleListener in interface TwitterStream
Parameters:
listener - listener to be added

addListener

public void addListener(UserStreamListener listener)

Specified by:
addListener in interface TwitterStream
Parameters:
listener - listener to add

addListener

public void addListener(StatusListener listener)

Specified by:
addListener in interface TwitterStream
Parameters:
listener - listener to add

addListener

public void addListener(SiteStreamsListener listener)

Specified by:
addListener in interface TwitterStream
Parameters:
listener - listener to add

addListener

private void addListener(StreamListener listener)

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class TwitterBaseImpl

hashCode

public int hashCode()
Overrides:
hashCode in class TwitterBaseImpl