Skip to content

Commit 90d76d1

Browse files
miketran78727jpwsutton
authored andcommitted
Synchronize access to reconnect timer and setting to NULL after cancel (#326)
Signed-off-by: miketran <miketran@us.ibm.com>
1 parent 8ce431b commit 90d76d1

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class MqttAsyncClient implements IMqttAsyncClient { // DestinationProvide
104104
private Timer reconnectTimer; // Automatic reconnect timer
105105
private static int reconnectDelay = 1000; // Reconnect delay, starts at 1 second
106106
private boolean reconnecting = false;
107-
107+
private static Object clientLock = new Object(); // Simple lock
108108

109109

110110

@@ -1217,21 +1217,32 @@ private void stopReconnectCycle(){
12171217
String methodName = "stopReconnectCycle";
12181218
//@Trace 504=Stop reconnect timer for client: {0}
12191219
log.fine(CLASS_NAME, methodName, "504", new Object[]{this.clientId});
1220-
if(reconnectTimer != null){
1221-
reconnectTimer.cancel();
1220+
synchronized(clientLock) {
1221+
if (this.connOpts.isAutomaticReconnect()) {
1222+
if(reconnectTimer != null){
1223+
reconnectTimer.cancel();
1224+
reconnectTimer = null;
1225+
}
1226+
reconnectDelay = 1000; // Reset Delay Timer
1227+
}
12221228
}
1223-
reconnectDelay = 1000; // Reset Delay Timer
1224-
12251229
}
12261230

12271231
private void rescheduleReconnectCycle(int delay){
12281232
String methodName = "rescheduleReconnectCycle";
12291233
//@Trace 505=Rescheduling reconnect timer for client: {0}, delay: {1}
12301234
log.fine(CLASS_NAME, methodName, "505", new Object[]{this.clientId, new Long(reconnectDelay)});
1231-
if(reconnectTimer != null){
1232-
reconnectTimer.schedule(new ReconnectTask(), reconnectDelay);
1235+
synchronized(clientLock) {
1236+
if(this.connOpts.isAutomaticReconnect()) {
1237+
if (reconnectTimer != null) {
1238+
reconnectTimer.schedule(new ReconnectTask(), delay);
1239+
} else {
1240+
// The previous reconnect timer was cancelled
1241+
reconnectDelay = delay;
1242+
startReconnectCycle();
1243+
}
1244+
}
12331245
}
1234-
12351246
}
12361247

12371248
private class ReconnectTask extends TimerTask {

0 commit comments

Comments
 (0)