Skip to content

Commit 20cebfc

Browse files
authored
GH-3685: Add docs for shared MQTT client feature
Related to #3685 * Add documentation for a new MQTT shared client feature Add an overview with reason for the feature as well as basic capabilities listing. Give an example with Java DSL usage for several adapters. * Fill `whats-new.adoc` with MQTT changes Add a reference to MQTT documentation with info about shared MQTT client * Couple of code review changes
1 parent 2bfcb32 commit 20cebfc

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/reference/asciidoc/mqtt.adoc

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,62 @@ IMPORTANT: The `org.springframework.integration.mqtt.support.MqttMessageConverte
500500
See more information in the `Mqttv5PahoMessageDrivenChannelAdapter` javadocs and its superclass.
501501

502502
IMPORTANT: It is recommended to have the `MqttConnectionOptions#setAutomaticReconnect(boolean)` set to true to let an internal `IMqttAsyncClient` instance to handle reconnects.
503-
Otherwise, only the manual restart of `Mqttv5PahoMessageDrivenChannelAdapter` can handle reconnects, e.g. via `MqttConnectionFailedEvent` handling on disconnection.
503+
Otherwise, only the manual restart of `Mqttv5PahoMessageDrivenChannelAdapter` can handle reconnects, e.g. via `MqttConnectionFailedEvent` handling on disconnection.
504+
505+
[[mqtt-shared-client]]
506+
=== Shared MQTT Client Support
507+
508+
If a single MQTT ClientID is required for several integrations, multiple MQTT client instances cannot be used because MQTT brokers may have a limitation on a number of connections per ClientID (typically, a single connection is allowed).
509+
For having a single client reused for different channel adapters, a `org.springframework.integration.mqtt.core.ClientManager` component may be used and passed to any channel adapter needed.
510+
It will manage MQTT connection lifecycle and do automatic reconnects if needed.
511+
Also, a custom connection options and `MqttClientPersistence` may be provided to the client manager just as currently it can be done for channel adapter components.
512+
513+
Note that both MQTT v5 and v3 channel adapters are supported.
514+
515+
The following Java DSL configuration sample demonstrates how to use this client manager in the integration flow:
516+
517+
====
518+
[source,java]
519+
----
520+
@Bean
521+
public ClientManager<IMqttAsyncClient, MqttConnectionOptions> clientManager() {
522+
MqttConnectionOptions connectionOptions = new MqttConnectionOptions();
523+
connectionOptions.setServerURIs(new String[]{ "tcp://localhost:1883" });
524+
connectionOptions.setConnectionTimeout(30000);
525+
connectionOptions.setMaxReconnectDelay(1000);
526+
connectionOptions.setAutomaticReconnect(true);
527+
Mqttv5ClientManager clientManager = new Mqttv5ClientManager(connectionOptions, "client-manager-client-id-v5");
528+
clientManager.setPersistence(new MqttDefaultFilePersistence());
529+
return clientManager;
530+
}
531+
532+
@Bean
533+
public IntegrationFlow mqttInFlowTopic1(
534+
ClientManager<IMqttAsyncClient, MqttConnectionOptions> clientManager) {
535+
536+
Mqttv5PahoMessageDrivenChannelAdapter messageProducer =
537+
new Mqttv5PahoMessageDrivenChannelAdapter(clientManager, "topic1");
538+
return IntegrationFlow.from(messageProducer)
539+
.channel(c -> c.queue("fromMqttChannel"))
540+
.get();
541+
}
542+
543+
@Bean
544+
public IntegrationFlow mqttInFlowTopic2(
545+
ClientManager<IMqttAsyncClient, MqttConnectionOptions> clientManager) {
546+
547+
Mqttv5PahoMessageDrivenChannelAdapter messageProducer =
548+
new Mqttv5PahoMessageDrivenChannelAdapter(clientManager, "topic2");
549+
return IntegrationFlow.from(messageProducer)
550+
.channel(c -> c.queue("fromMqttChannel"))
551+
.get();
552+
}
553+
554+
@Bean
555+
public IntegrationFlow mqttOutFlow(
556+
ClientManager<IMqttAsyncClient, MqttConnectionOptions> clientManager) {
557+
558+
return f -> f.handle(new Mqttv5PahoMessageHandler(clientManager));
559+
}
560+
----
561+
====

src/reference/asciidoc/whats-new.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ In general the project has been moved to Java 17 baseline and migrated from Java
1717
[[x6.0-new-components]]
1818
=== New Components
1919

20+
A new MQTT `ClientManager` has been added to support a reusable MQTT connection across different channel adapters.
21+
See <<./mqtt.adoc#mqtt-shared-client,Shared MQTT Client Support>> for more information.
22+
2023
[[x6.0-graphql]]
2124
=== GraphQL Support
2225

0 commit comments

Comments
 (0)