Skip to content

Commit 64c6d5c

Browse files
committed
Change heartbeat Timer to use newer ScheduledThreadPoolExecutor
1 parent 0c12d7f commit 64c6d5c

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import java.util.Locale;
3030
import java.util.Map;
3131
import java.util.Random;
32-
import java.util.Timer;
33-
import java.util.TimerTask;
32+
import java.util.concurrent.ScheduledThreadPoolExecutor;
33+
import java.util.concurrent.TimeUnit;
3434
import java.util.concurrent.TimeoutException;
3535
import java.util.concurrent.atomic.AtomicBoolean;
3636
import java.util.concurrent.atomic.AtomicInteger;
@@ -409,6 +409,14 @@ private synchronized void internalStop() {
409409
} catch (Exception ignore) {}
410410
}
411411

412+
if (heartbeatTimer != null)
413+
{
414+
try {
415+
heartbeatTimer.shutdown();
416+
} catch (Exception ignore) {}
417+
heartbeatTimer = null;
418+
}
419+
412420
connectionInfo = null;
413421
stopBackgroundThread();
414422
}
@@ -553,7 +561,7 @@ public synchronized void raiseEvent(String eventName, ServerEventMessage message
553561
}
554562
}
555563

556-
Timer heratbeatTimer;
564+
ScheduledThreadPoolExecutor heartbeatTimer;
557565

558566
private void startNewHeartbeat() {
559567
if (connectionInfo == null || connectionInfo.getHeartbeatUrl() == null)
@@ -562,16 +570,15 @@ private void startNewHeartbeat() {
562570
if (stopped.get())
563571
return;
564572

565-
if (heratbeatTimer == null)
566-
heratbeatTimer = new Timer("ServerEventsClient Heartbeat");
573+
if (heartbeatTimer == null)
574+
heartbeatTimer = new ScheduledThreadPoolExecutor(1);
567575

568-
//reschedule timer on every heartbeat
569-
heratbeatTimer.schedule(new TimerTask() {
576+
heartbeatTimer.schedule(new Runnable() {
570577
@Override
571578
public void run() {
572579
Heartbeat();
573580
}
574-
}, connectionInfo.getHeartbeatIntervalMs(), Integer.MAX_VALUE);
581+
}, connectionInfo.getHeartbeatIntervalMs(), TimeUnit.MILLISECONDS);
575582
}
576583

577584
public void Heartbeat(){

src/AndroidClient/client/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import java.util.Locale;
3030
import java.util.Map;
3131
import java.util.Random;
32-
import java.util.Timer;
33-
import java.util.TimerTask;
32+
import java.util.concurrent.ScheduledThreadPoolExecutor;
33+
import java.util.concurrent.TimeUnit;
3434
import java.util.concurrent.TimeoutException;
3535
import java.util.concurrent.atomic.AtomicBoolean;
3636
import java.util.concurrent.atomic.AtomicInteger;
@@ -409,6 +409,14 @@ private synchronized void internalStop() {
409409
} catch (Exception ignore) {}
410410
}
411411

412+
if (heartbeatTimer != null)
413+
{
414+
try {
415+
heartbeatTimer.shutdown();
416+
} catch (Exception ignore) {}
417+
heartbeatTimer = null;
418+
}
419+
412420
connectionInfo = null;
413421
stopBackgroundThread();
414422
}
@@ -553,7 +561,7 @@ public synchronized void raiseEvent(String eventName, ServerEventMessage message
553561
}
554562
}
555563

556-
Timer heratbeatTimer;
564+
ScheduledThreadPoolExecutor heartbeatTimer;
557565

558566
private void startNewHeartbeat() {
559567
if (connectionInfo == null || connectionInfo.getHeartbeatUrl() == null)
@@ -562,16 +570,15 @@ private void startNewHeartbeat() {
562570
if (stopped.get())
563571
return;
564572

565-
if (heratbeatTimer == null)
566-
heratbeatTimer = new Timer("ServerEventsClient Heartbeat");
573+
if (heartbeatTimer == null)
574+
heartbeatTimer = new ScheduledThreadPoolExecutor(1);
567575

568-
//reschedule timer on every heartbeat
569-
heratbeatTimer.schedule(new TimerTask() {
576+
heartbeatTimer.schedule(new Runnable() {
570577
@Override
571578
public void run() {
572579
Heartbeat();
573580
}
574-
}, connectionInfo.getHeartbeatIntervalMs(), Integer.MAX_VALUE);
581+
}, connectionInfo.getHeartbeatIntervalMs(), TimeUnit.MILLISECONDS);
575582
}
576583

577584
public void Heartbeat(){

0 commit comments

Comments
 (0)