Skip to content

Commit

Permalink
Fix MQTT reconnection and add some debug logging (openhab#1254)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
  • Loading branch information
J-N-K authored and wborn committed Dec 2, 2019
1 parent 991a8fd commit 4efa7ec
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ public void onDisconnected(Throwable t) {
// If we tried to connect via start(), use the reconnect strategy to try it again
if (connection.isConnecting) {
connection.isConnecting = false;
if (connection.reconnectStrategy != null) {
connection.reconnectStrategy.lostConnection();
}
}

if (connection.reconnectStrategy != null) {
connection.reconnectStrategy.lostConnection();
}
}

Expand Down Expand Up @@ -562,6 +563,7 @@ public CompletableFuture<Boolean> subscribe(String topic, MqttMessageSubscriber
if (client.getState().isConnected()) {
client.subscribe(topic, qos, clientCallback).whenComplete((s, t) -> {
if (t == null) {
logger.trace("Subscribed {} to topic {}", subscriber, topic);
future.complete(true);
} else {
future.completeExceptionally(new MqttException(t));
Expand Down Expand Up @@ -612,17 +614,20 @@ public CompletableFuture<Boolean> unsubscribe(String topic, MqttMessageSubscribe
synchronized (subscribers) {
final @Nullable List<MqttMessageSubscriber> list = subscribers.get(topic);
if (list == null) {
logger.trace("Tried to unsubscribe {} from topic {}, but subscriber list is empty", subscriber, topic);
return CompletableFuture.completedFuture(true);
}
list.remove(subscriber);
if (!list.isEmpty()) {
logger.trace("Removed {} from topic subscribers for topic {}, but other subscribers present", subscriber, topic);
return CompletableFuture.completedFuture(true);
}
// Remove from subscriber list
subscribers.remove(topic);
// No more subscribers to this topic. Unsubscribe topic on the broker
MqttAsyncClientWrapper mqttClient = this.client;
if (mqttClient != null) {
logger.trace("Subscriber list is empty after removing {}, unsubscribing topic {} from connection", subscriber, topic);
return unsubscribeRaw(mqttClient, topic);
} else {
return CompletableFuture.completedFuture(false);
Expand Down

0 comments on commit 4efa7ec

Please sign in to comment.