Skip to content

Commit

Permalink
Change heartbeat Timer to use newer ScheduledThreadPoolExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 5, 2017
1 parent 0c12d7f commit 64c6d5c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -409,6 +409,14 @@ private synchronized void internalStop() {
} catch (Exception ignore) {}
}

if (heartbeatTimer != null)
{
try {
heartbeatTimer.shutdown();
} catch (Exception ignore) {}
heartbeatTimer = null;
}

connectionInfo = null;
stopBackgroundThread();
}
Expand Down Expand Up @@ -553,7 +561,7 @@ public synchronized void raiseEvent(String eventName, ServerEventMessage message
}
}

Timer heratbeatTimer;
ScheduledThreadPoolExecutor heartbeatTimer;

private void startNewHeartbeat() {
if (connectionInfo == null || connectionInfo.getHeartbeatUrl() == null)
Expand All @@ -562,16 +570,15 @@ private void startNewHeartbeat() {
if (stopped.get())
return;

if (heratbeatTimer == null)
heratbeatTimer = new Timer("ServerEventsClient Heartbeat");
if (heartbeatTimer == null)
heartbeatTimer = new ScheduledThreadPoolExecutor(1);

//reschedule timer on every heartbeat
heratbeatTimer.schedule(new TimerTask() {
heartbeatTimer.schedule(new Runnable() {
@Override
public void run() {
Heartbeat();
}
}, connectionInfo.getHeartbeatIntervalMs(), Integer.MAX_VALUE);
}, connectionInfo.getHeartbeatIntervalMs(), TimeUnit.MILLISECONDS);
}

public void Heartbeat(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -409,6 +409,14 @@ private synchronized void internalStop() {
} catch (Exception ignore) {}
}

if (heartbeatTimer != null)
{
try {
heartbeatTimer.shutdown();
} catch (Exception ignore) {}
heartbeatTimer = null;
}

connectionInfo = null;
stopBackgroundThread();
}
Expand Down Expand Up @@ -553,7 +561,7 @@ public synchronized void raiseEvent(String eventName, ServerEventMessage message
}
}

Timer heratbeatTimer;
ScheduledThreadPoolExecutor heartbeatTimer;

private void startNewHeartbeat() {
if (connectionInfo == null || connectionInfo.getHeartbeatUrl() == null)
Expand All @@ -562,16 +570,15 @@ private void startNewHeartbeat() {
if (stopped.get())
return;

if (heratbeatTimer == null)
heratbeatTimer = new Timer("ServerEventsClient Heartbeat");
if (heartbeatTimer == null)
heartbeatTimer = new ScheduledThreadPoolExecutor(1);

//reschedule timer on every heartbeat
heratbeatTimer.schedule(new TimerTask() {
heartbeatTimer.schedule(new Runnable() {
@Override
public void run() {
Heartbeat();
}
}, connectionInfo.getHeartbeatIntervalMs(), Integer.MAX_VALUE);
}, connectionInfo.getHeartbeatIntervalMs(), TimeUnit.MILLISECONDS);
}

public void Heartbeat(){
Expand Down

0 comments on commit 64c6d5c

Please sign in to comment.