public abstract class AStream
extends java.lang.Object
implements java.io.Closeable
Warning from Twitter: Consuming applications must tolerate duplicate statuses, out-of-order statuses (upto 3 seconds of scrambling) and non-status messages.
Modifier and Type | Class and Description |
---|---|
static interface |
AStream.IListen
Use these for push-notification of incoming tweets and stream activity.
|
static class |
AStream.Outage
What, Twitter had an outage? Here are the details.
|
Modifier and Type | Field and Description |
---|---|
static int |
MAX_BUFFER
Start dropping messages after this.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(AStream.IListen listener)
Add a listener to the front of the queue.
|
void |
addOutage(AStream.Outage outage)
The stream will track outages during use (provided
setAutoReconnect(boolean) is true). |
void |
clear()
Forget the past.
|
void |
close()
A closed stream can be restarted.
|
void |
connect()
Connect to Twitter.
|
java.lang.Exception |
fillInOutages()
Use the REST API to fill in outages when possible.
|
protected void |
finalize() |
java.util.List<TwitterEvent> |
getEvents() |
int |
getForgotten() |
java.util.List<AStream.Outage> |
getOutages() |
java.util.List<java.lang.Object[]> |
getSystemEvents() |
java.util.List<Twitter.ITweet> |
getTweets() |
boolean |
isAlive() |
boolean |
isConnected()
Many users will want to use
isAlive() instead, which takes into
account auto-reconnect behaviour. |
java.util.List<TwitterEvent> |
popEvents() |
java.util.List<java.lang.Object[]> |
popSystemEvents()
System Events
|
java.util.List<Twitter.ITweet> |
popTweets() |
boolean |
removeListener(AStream.IListen listener) |
void |
setAutoReconnect(boolean yes) |
void |
setPreviousCount(int previousCount)
Deprecated.
Twitter need to fix this :(
|
public AStream(Twitter jtwit)
public void addListener(AStream.IListen listener)
AStream.IListen
)listener
- public void addOutage(AStream.Outage outage)
setAutoReconnect(boolean)
is true). This method allows you to
manually add outages (which can then be filled in using
fillInOutages()
) -- e.g. to cover restarting Java.outage
- public void clear()
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public void connect() throws TwitterException
Do nothing if we already have a good connection. Bad or partly formed connections will be closed.
Auto-reconnect is ignored here: if there's an exception it will be thrown and a reconnect will not be attempted. This gives a fast-return.
TwitterException
reconnect()
public final java.lang.Exception fillInOutages() throws java.lang.UnsupportedOperationException
In accordance with best-practice, this method will skip over very recent
outages (which will be picked up by subsequent calls to
fillInOutages()
).
From dev.twitter.com:
Do not resume REST API polling immediately after a stream failure. Wait
at least a minute or two after the initial failure before you begin REST
API polling. This delay is crucial to prevent dog-piling the REST API in
the event of a minor hiccup on the streaming API.
java.lang.UnsupportedOperationException
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public final java.util.List<TwitterEvent> getEvents()
public final int getForgotten()
Best practice is to NOT rely on this for memory management. You
should call popEvents()
, popSystemEvents()
and
popTweets()
regularly to clear the buffers.
public final java.util.List<AStream.Outage> getOutages()
This is the actual list used. You can remove items from this list
to quietly forget about them. Use addOutage(Outage)
to
add items in the correct order. The list size is capped to avoid
memory leakage.
public final java.util.List<java.lang.Object[]> getSystemEvents()
popSystemEvents()
public final java.util.List<Twitter.ITweet> getTweets()
public final boolean isAlive()
public final boolean isConnected()
isAlive()
instead, which takes into
account auto-reconnect behaviour.isAlive()
public final java.util.List<TwitterEvent> popEvents()
Thread safety: If two threads both call popEvents / getEvents, then there is a race condition, and the caller should synchronize appropriately.
public final java.util.List<java.lang.Object[]> popSystemEvents()
Thread safety: If two threads both call popSystemEvents / getSystemEvents, then there is a race condition, and the caller should synchronize appropriately.
public final java.util.List<Twitter.ITweet> popTweets()
Thread safety: If two threads both call popTweets / getTweets, then there is a race condition, and the caller should synchronize appropriately.
public boolean removeListener(AStream.IListen listener)
public void setAutoReconnect(boolean yes)
yes
- If true, attempt to connect if disconnected. true by default.@Deprecated public void setPreviousCount(int previousCount)
previousCount
- Up to 150,000 but subject to change.
Negative values are allowed -- they mean the stream will
terminate when it reaches the end of the historical messages.