Skip to content

Commit 280eb29

Browse files
GH-3959: MqttConFailedEvent for normal disconnect (#3961)
* GH-3959: MqttConFailedEvent for normal disconnect Fixes #3959 The `MqttCallback.disconnected(MqttDisconnectResponse)` in Paho v5 client is also called when server initiates a disconnection. In this case that `MqttDisconnectResponse` does not have a `cause` value * Modify `MqttConnectionFailedEvent` to make a `cause` property optional * Fix `Mqttv5PahoMessageDrivenChannelAdapter` & `Mqttv5PahoMessageHandler` to not check for `cause`, but emit an `MqttConnectionFailedEvent` for any `disconnected()` calls Unfortunately current Paho v3 client does not call `connectionLost()` for normal disconnections and we cannot react for this callback with an `MqttConnectionFailedEvent` **Cherry-pick to `5.5.x`** * Fix language in doc Co-authored-by: Gary Russell <[email protected]> Co-authored-by: Gary Russell <[email protected]>
1 parent 026feee commit 280eb29

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/event/MqttConnectionFailedEvent.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,26 @@
1616

1717
package org.springframework.integration.mqtt.event;
1818

19+
import org.springframework.lang.Nullable;
20+
1921
/**
22+
* The {@link MqttIntegrationEvent} to notify about lost connection to the server.
23+
* When normal disconnection is happened (initiated by the server), the {@code cause} is null.
24+
*
2025
* @author Gary Russell
26+
* @author Artem Bilan
27+
*
2128
* @since 4.2.2
2229
*
2330
*/
2431
@SuppressWarnings("serial")
2532
public class MqttConnectionFailedEvent extends MqttIntegrationEvent {
2633

27-
public MqttConnectionFailedEvent(Object source, Throwable cause) {
34+
public MqttConnectionFailedEvent(Object source) {
35+
super(source);
36+
}
37+
38+
public MqttConnectionFailedEvent(Object source, @Nullable Throwable cause) {
2839
super(source, cause);
2940
}
3041

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/event/MqttIntegrationEvent.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,15 @@
1717
package org.springframework.integration.mqtt.event;
1818

1919
import org.springframework.integration.events.IntegrationEvent;
20+
import org.springframework.lang.Nullable;
2021

2122
/**
22-
* Base class for Mqtt Events. For {@link #getSourceAsType()}, you should use a sub type
23+
* Base class for Mqtt Events. For {@link #getSourceAsType()}, you should use a subtype
2324
* of {@link org.springframework.integration.mqtt.core.MqttComponent} for the receiving
2425
* variable.
26+
*
2527
* @author Gary Russell
28+
* @author Artem Bilan
2629
*
2730
* @since 4.1
2831
*/
@@ -33,7 +36,7 @@ public MqttIntegrationEvent(Object source) {
3336
super(source);
3437
}
3538

36-
public MqttIntegrationEvent(Object source, Throwable cause) {
39+
public MqttIntegrationEvent(Object source, @Nullable Throwable cause) {
3740
super(source, cause);
3841
}
3942

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/inbound/Mqttv5PahoMessageDrivenChannelAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public void messageArrived(String topic, MqttMessage mqttMessage) {
292292
public void disconnected(MqttDisconnectResponse disconnectResponse) {
293293
MqttException cause = disconnectResponse.getException();
294294
ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher();
295-
if (cause != null && applicationEventPublisher != null) {
295+
if (applicationEventPublisher != null) {
296296
applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, cause));
297297
}
298298
}

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/outbound/Mqttv5PahoMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public void deliveryComplete(IMqttToken token) {
274274
public void disconnected(MqttDisconnectResponse disconnectResponse) {
275275
MqttException cause = disconnectResponse.getException();
276276
ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher();
277-
if (cause != null && applicationEventPublisher != null) {
277+
if (applicationEventPublisher != null) {
278278
applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, cause));
279279
}
280280
}

src/reference/asciidoc/mqtt.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ public class MqttJavaApplication {
404404
Certain application events are published by the adapters.
405405

406406
* `MqttConnectionFailedEvent` - published by both adapters if we fail to connect or a connection is subsequently lost.
407+
For the MQTT v5 Paho client, this event is also emitted when the server performs a normal disconnection, in which case the `cause` of the lost connection is `null`.
407408
* `MqttMessageSentEvent` - published by the outbound adapter when a message has been sent, if running in asynchronous mode.
408409
* `MqttMessageDeliveredEvent` - published by the outbound adapter when the client indicates that a message has been delivered, if running in asynchronous mode.
409410
* `MqttSubscribedEvent` - published by the inbound adapter after subscribing to the topics.

0 commit comments

Comments
 (0)