3434import java .util .TimerTask ;
3535import java .util .concurrent .TimeoutException ;
3636import java .util .concurrent .atomic .AtomicBoolean ;
37+ import java .util .concurrent .atomic .AtomicInteger ;
3738
3839/**
3940 * Created by mythz on 2/9/2017.
@@ -62,6 +63,7 @@ public class ServerEventsClient implements Closeable {
6263 protected Date lastPulseAt ;
6364 protected Thread bgThread ;
6465 protected final AtomicBoolean stopped = new AtomicBoolean (false );
66+ protected final AtomicBoolean running = new AtomicBoolean (false );
6567
6668 static int BufferSize = 1024 * 64 ;
6769 static int DefaultHeartbeatMs = 10 * 1000 ;
@@ -294,7 +296,7 @@ public synchronized void restart() {
294296 return ;
295297
296298 try {
297- sleepBackOffMultiplier (errorsCount );
299+ sleepBackOffMultiplier (errorsCount . intValue () );
298300 start ();
299301 } catch (Exception e ){
300302 onExceptionReceived (e );
@@ -321,8 +323,7 @@ private void sleepBackOffMultiplier(int continuousErrorsCount) throws Interrupte
321323 MaxSleepMs
322324 );
323325
324- if (Log .isDebugEnabled ())
325- Log .d ("Sleeping for " + nextTry + "ms after " + continuousErrorsCount + " continuous errors" );
326+ Log .info ("Sleeping for " + nextTry + "ms after " + continuousErrorsCount + " continuous errors" );
326327
327328 Thread .sleep (nextTry );
328329 }
@@ -397,9 +398,10 @@ private void onMessageReceived(ServerEventMessage e) {
397398 onMessage .execute (e );
398399 }
399400
400- private int errorsCount ;
401+ private AtomicInteger errorsCount = new AtomicInteger ();
402+
401403 protected void onExceptionReceived (Exception ex ) {
402- errorsCount ++ ;
404+ errorsCount . incrementAndGet () ;
403405
404406 Log .e ("[SSE-CLIENT] OnExceptionReceived: "
405407 + ex .getMessage () + " on #" + getConnectionDisplayName (), ex );
@@ -556,9 +558,10 @@ public void run() {
556558 HttpURLConnection req = (HttpURLConnection ) streamUri .openConnection ();
557559
558560 InputStream is = new BufferedInputStream (req .getInputStream ());
561+ errorsCount .set (0 );
559562 readStream (is );
560563 } catch (IOException e ) {
561- Log .e ("Error reading from event-stream" , e );
564+ Log .e ("Error reading from event-stream, continuous errors: " + errorsCount . incrementAndGet () , e );
562565 Log .e (Utils .getStackTrace (e ));
563566 } finally {
564567 client .restart ();
0 commit comments