Skip to content

GH-3697: Various lifecycle fixed for MQTT v5 CAs #3698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
*
* It is recommended to have the {@link MqttConnectionOptions#setAutomaticReconnect(boolean)}
* set to true to let an internal {@link IMqttAsyncClient} instance to handle reconnects.
* Otherwise, the manual restart of this component can only handle reconnects, e.g. via
* Otherwise, only the manual restart of this component can handle reconnects, e.g. via
* {@link MqttConnectionFailedEvent} handling on disconnection.
*
* See {@link #setPayloadType} for more information about type conversion.
Expand Down Expand Up @@ -190,6 +190,7 @@ protected void doStop() {
String[] topics = getTopic();
try {
this.mqttClient.unsubscribe(topics).waitForCompletion(getCompletionTimeout());
this.mqttClient.disconnect().waitForCompletion(getCompletionTimeout());
}
catch (MqttException ex) {
logger.error(ex, () -> "Error unsubscribing from " + Arrays.toString(topics));
Expand All @@ -199,6 +200,17 @@ protected void doStop() {
}
}

@Override
public void destroy() {
super.destroy();
try {
this.mqttClient.close(true);
}
catch (MqttException ex) {
logger.error(ex, "Failed to close 'MqttAsyncClient'");
}
}

@Override
public void addTopic(String topic, int qos) {
this.topicLock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
/**
* The {@link AbstractMqttMessageHandler} implementation for MQTT v5.
*
* It is recommended to have the {@link MqttConnectionOptions#setAutomaticReconnect(boolean)}
* set to true to let an internal {@link IMqttAsyncClient} instance to handle reconnects.
* Otherwise, only the manual restart of this component can handle reconnects, e.g. via
* {@link MqttConnectionFailedEvent} handling on disconnection.
*
*
* @author Artem Bilan
*
* @since 5.5.5
Expand Down Expand Up @@ -159,7 +165,11 @@ protected void doStart() {
this.mqttClient.connect(this.connectionOptions).waitForCompletion(getCompletionTimeout());
}
catch (MqttException ex) {
throw new IllegalStateException("Cannot connect 'MqttAsyncClient' for: " + getComponentName(), ex);
ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher();
if (applicationEventPublisher != null) {
applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, ex));
}
logger.error(ex, "MQTT client failed to connect. Will retry if 'ConnectionOptions.isAutomaticReconnect()'.");
}
}

Expand All @@ -177,7 +187,7 @@ protected void doStop() {
public void destroy() {
super.destroy();
try {
this.mqttClient.close();
this.mqttClient.close(true);
}
catch (MqttException ex) {
logger.error(ex, "Failed to close 'MqttAsyncClient'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
* @author Gary Russell
* @author Artem Bilan
*
* @since 4.0
* @since 5.5.5
*
*/
@SpringJUnitConfig
Expand Down Expand Up @@ -164,8 +164,7 @@ public IntegrationFlow mqttInFlow() {
messageProducer.setMessageConverter(mqttStringToBytesConverter());
messageProducer.setManualAcks(true);

return IntegrationFlows.from(
messageProducer)
return IntegrationFlows.from(messageProducer)
.channel(c -> c.queue("fromMqttChannel"))
.get();
}
Expand Down
5 changes: 4 additions & 1 deletion src/reference/asciidoc/mqtt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,7 @@ public IntegrationFlow mqttInFlow() {

IMPORTANT: The `org.springframework.integration.mqtt.support.MqttMessageConverter` cannot be used with the `Mqttv5PahoMessageDrivenChannelAdapter` since its contract is aimed only for the MQTT v3 protocol.

See more information in the `Mqttv5PahoMessageDrivenChannelAdapter` javadocs and its superclass.
See more information in the `Mqttv5PahoMessageDrivenChannelAdapter` javadocs and its superclass.

IMPORTANT: It is recommended to have the `MqttConnectionOptions#setAutomaticReconnect(boolean)` set to true to let an internal `IMqttAsyncClient` instance to handle reconnects.
Otherwise, only the manual restart of these channel adapters can handle reconnects, e.g. via `MqttConnectionFailedEvent` handling on disconnection.