@@ -144,14 +144,7 @@ protected MQTTClient(@NonNull URI brokerUri, @NonNull String clientId, MQTTClien
144144 }
145145
146146 void reset () {
147- if (mqttClientInternal .isConnected ()) {
148- try {
149- mqttClientInternal .disconnect ();
150- } catch (MqttException e ) {
151- LOGGER .atError ().setCause (e ).log ("Failed to disconnect MQTT client" );
152- return ;
153- }
154- }
147+ disconnect (30_000L ); // paho default
155148 connectAndSubscribe ();
156149 }
157150
@@ -169,6 +162,32 @@ public void start() throws MessageClientException {
169162 connectAndSubscribe ();
170163 }
171164
165+ private void disconnect () {
166+ // 0ms quiescence time, just send the disconnect packet immediately
167+ disconnect (0 );
168+ }
169+
170+ @ SuppressWarnings ("PMD.CloseResource" )
171+ private void disconnect (long quiesceTimeout ) {
172+ IMqttClient client = mqttClientInternal ;
173+ if (client == null ) {
174+ return ;
175+ }
176+ try {
177+ LOGGER .debug ("Disconnecting MQTT client" );
178+ client .disconnect (quiesceTimeout );
179+ } catch (MqttException e ) {
180+ if (MqttException .REASON_CODE_CLIENT_ALREADY_DISCONNECTED != e .getReasonCode ()
181+ && MqttException .REASON_CODE_CLIENT_CLOSED != e .getReasonCode ()) {
182+ LOGGER .atError ().setCause (e ).log ("Failed to disconnect MQTT client" );
183+ return ;
184+ }
185+ }
186+ // no need to unsubscribe because we connect with cleanSession=true
187+ subscribedLocalMqttTopics .clear ();
188+ LOGGER .debug ("MQTT client disconnected" );
189+ }
190+
172191 /**
173192 * Stop the {@link MQTTClient}.
174193 */
@@ -177,29 +196,15 @@ public void start() throws MessageClientException {
177196 public void stop () {
178197 mqttClientKeyStore .unsubscribeFromUpdates (onKeyStoreUpdate );
179198 cancelConnectTask ();
180-
181- IMqttClient client = mqttClientInternal ;
182- try {
183- if (client != null && client .isConnected ()) {
184- LOGGER .debug ("Disconnecting MQTT client" );
185- // 0ms quiescence time, just send the disconnect packet immediately
186- client .disconnect (0 );
187- LOGGER .debug ("MQTT client disconnected" );
188- }
189- } catch (MqttException e ) {
190- LOGGER .atError ().setCause (e ).log ("Failed to disconnect MQTT client" );
191- } finally {
192- // no need to unsubscribe because we connect with cleanSession=true
193- subscribedLocalMqttTopics .clear ();
194- }
195-
199+ disconnect ();
196200 try {
197201 dataStore .close ();
198202 } catch (MqttPersistenceException e ) {
199203 LOGGER .atDebug ().setCause (e ).log ("Unable to close mqtt client datastore" );
200204 }
201205
202206 try {
207+ IMqttClient client = mqttClientInternal ;
203208 if (client != null ) {
204209 client .close ();
205210 }
@@ -297,44 +302,47 @@ private void cancelConnectTask() {
297302 }
298303 }
299304
300- private synchronized void doConnect () throws MqttException , KeyStoreException {
301- if (!mqttClientInternal .isConnected ()) {
302- mqttClientInternal .connect (getConnectionOptions ());
303- LOGGER .atInfo ()
304- .kv (BridgeConfig .KEY_BROKER_URI , brokerUri )
305- .kv (BridgeConfig .KEY_CLIENT_ID , clientId )
306- .log ("Connected to broker" );
307- }
308- }
309-
310305 private void reconnectAndResubscribe () {
311306 int waitBeforeRetry = MIN_WAIT_RETRY_IN_SECONDS ;
312307
313308 while (!mqttClientInternal .isConnected () && !Thread .currentThread ().isInterrupted ()) {
309+ Exception error ;
314310 try {
315311 // TODO: Clean up this loop
316- doConnect ();
317- } catch (MqttException | KeyStoreException e ) {
312+ mqttClientInternal .connect (getConnectionOptions ());
313+ break ;
314+ } catch (MqttException e ) {
318315 if (Utils .getUltimateCause (e ) instanceof InterruptedException ) {
319316 // paho doesn't reset the interrupt flag
320317 LOGGER .atDebug ().log ("Interrupted during reconnect" );
321318 Thread .currentThread ().interrupt ();
322319 return ;
323320 }
324-
325- LOGGER .atDebug ().setCause (e )
326- .log ("Unable to connect. Will be retried after {} seconds" , waitBeforeRetry );
327- try {
328- Thread .sleep (waitBeforeRetry * 1000 );
329- } catch (InterruptedException er ) {
330- Thread .currentThread ().interrupt ();
331- LOGGER .atDebug ().log ("Interrupted during reconnect" );
321+ if (MqttException .REASON_CODE_CLIENT_CLOSED == e .getReasonCode ()) {
332322 return ;
333323 }
334- waitBeforeRetry = Math .min (2 * waitBeforeRetry , MAX_WAIT_RETRY_IN_SECONDS );
324+ error = e ;
325+ } catch (KeyStoreException e ) {
326+ error = e ;
335327 }
328+
329+ LOGGER .atDebug ().setCause (error )
330+ .log ("Unable to connect. Will be retried after {} seconds" , waitBeforeRetry );
331+ try {
332+ Thread .sleep (waitBeforeRetry * 1000 );
333+ } catch (InterruptedException er ) {
334+ Thread .currentThread ().interrupt ();
335+ LOGGER .atDebug ().log ("Interrupted during reconnect" );
336+ return ;
337+ }
338+ waitBeforeRetry = Math .min (2 * waitBeforeRetry , MAX_WAIT_RETRY_IN_SECONDS );
336339 }
337340
341+ LOGGER .atInfo ()
342+ .kv (BridgeConfig .KEY_BROKER_URI , brokerUri )
343+ .kv (BridgeConfig .KEY_CLIENT_ID , clientId )
344+ .log ("Connected to broker" );
345+
338346 resubscribe ();
339347 }
340348
0 commit comments