Skip to content

Commit eba1e6a

Browse files
author
tmoskovitch
committed
Check for streaming tweets for reconnect errors.
In case of reconnect restart the `tweetsQueuer` thread.
1 parent b4f3c50 commit eba1e6a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

examples/src/main/java/com/twitter/clientlib/HelloWorldStreaming.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void main(String[] args) {
5252
}
5353

5454
if(tsle.getError() != null) {
55-
System.err.println("==> Finished " + tsle.getError());
55+
System.err.println("==> Ended with error: " + tsle.getError());
5656
}
5757

5858
// // Shutdown TweetsStreamListenersExecutor

examples/src/main/java/com/twitter/clientlib/TweetsStreamListenersExecutor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
public class TweetsStreamListenersExecutor {
3636
private final static int TIMEOUT_MILLIS = 60000;
3737
private final static int SLEEP_MILLIS = 100;
38+
private final static int BACKOFF_SLEEP_INTERVAL_MILLIS = 5000;
3839
private TweetsQueuer tweetsQueuer;
3940
private ITweetsQueue tweetsQueue = new LinkedListTweetsQueue();
4041
private final AtomicBoolean isRunning = new AtomicBoolean(true);
4142
private final AtomicLong tweetStreamedTime = new AtomicLong(0);
4243
private Exception caughtException;
4344
private IStreamingHandler<?> streamingHandler;
45+
private long reconnecting = 0;
4446

4547
public StreamListenersExecutorBuilder stream() {
4648
return new StreamListenersExecutorBuilder();
@@ -75,8 +77,12 @@ private boolean isTweetStreamedError() {
7577

7678
private void restartTweetsQueuer() {
7779
tweetsQueuer.shutdownQueuer();
80+
if(reconnecting < 7) {
81+
reconnecting++;
82+
}
7883
try {
79-
Thread.sleep(SLEEP_MILLIS); // Wait a bit before starting the TweetsQueuer and calling the API again.
84+
System.out.println("sleeping " + BACKOFF_SLEEP_INTERVAL_MILLIS * reconnecting);
85+
Thread.sleep(BACKOFF_SLEEP_INTERVAL_MILLIS * reconnecting); // Wait a bit before starting the TweetsQueuer and calling the API again.
8086
} catch (InterruptedException e) {
8187
e.printStackTrace();
8288
}

0 commit comments

Comments
 (0)