Skip to content

Commit 50f3fcd

Browse files
authored
LP-9741 make heartbeat methods idempotent (#242)
* LP-9741 make heartbeat methods idempotent * create new method
1 parent 072238c commit 50f3fcd

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class Leanplum {
9696
private static boolean userSpecifiedDeviceId;
9797
private static boolean initializedMessageTemplates = false;
9898
private static boolean locationCollectionEnabled = true;
99-
private static ScheduledExecutorService heartbeatExecutor;
99+
private static ScheduledExecutorService heartbeatExecutor = null;
100100
private static Context context;
101101

102102
private static Runnable pushStartCallback;
@@ -1041,23 +1041,17 @@ private static void resumeInternal() {
10411041
*/
10421042
private static void startHeartbeat() {
10431043
synchronized (heartbeatLock) {
1044-
heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
1045-
heartbeatExecutor.scheduleAtFixedRate(new Runnable() {
1046-
public void run() {
1047-
try {
1048-
Request.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
1049-
} catch (Throwable t) {
1050-
Util.handleException(t);
1051-
}
1052-
}
1053-
}, 15, 15, TimeUnit.MINUTES);
1044+
if (heartbeatExecutor == null) {
1045+
createHeartbeatExecutor();
1046+
}
10541047
}
10551048
}
10561049

10571050
private static void pauseHeartbeat() {
10581051
synchronized (heartbeatLock) {
10591052
if (heartbeatExecutor != null) {
10601053
heartbeatExecutor.shutdown();
1054+
heartbeatExecutor = null;
10611055
}
10621056
}
10631057
}
@@ -1066,6 +1060,19 @@ private static void resumeHeartbeat() {
10661060
startHeartbeat();
10671061
}
10681062

1063+
private static void createHeartbeatExecutor() {
1064+
heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
1065+
heartbeatExecutor.scheduleAtFixedRate(new Runnable() {
1066+
public void run() {
1067+
try {
1068+
Request.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
1069+
} catch (Throwable t) {
1070+
Util.handleException(t);
1071+
}
1072+
}
1073+
}, 15, 15, TimeUnit.MINUTES);
1074+
}
1075+
10691076
/**
10701077
* Call this to explicitly end the session. This should not be used in most cases, so we won't
10711078
* make it public for now.

0 commit comments

Comments
 (0)