-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add Inbound Channel Adapter for MQTT based on hivemq #10989
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
Draft
mjd507
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
mjd507:hivemq-mqtt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...src/main/java/com/springframework/integration/hivemq/MqttClientConnectionCoordinator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.springframework.integration.hivemq; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * An abstraction for coordinating MQTT client connections and disconnections, ensures thread-safe operations. | ||
| * | ||
| * @param <T> MQTT Client type | ||
| * @param <C> MQTT Connect Message type | ||
| * @param <D> MQTT Disconnect Message type | ||
| * | ||
| * @author Jiandong Ma | ||
| * | ||
| * @since 7.2 | ||
| */ | ||
| public interface MqttClientConnectionCoordinator<T, C, D> { | ||
|
|
||
| /** | ||
| * Connect to MQTT broker using the mqttClient and mqttConnect. | ||
| * @param mqttClient the mqttClient | ||
| * @param mqttConnect the mqttConnect | ||
| * @return CompletableFuture | ||
| */ | ||
| CompletableFuture<?> connect(T mqttClient, C mqttConnect); | ||
|
|
||
| /** | ||
| * Disconnect from MQTT broker using the mqttClient and mqttDisconnect. | ||
| * @param mqttClient the mqttClient | ||
| * @param mqttDisconnect the mqttDisconnect, for MQTT v3, it is always null. | ||
| * @return CompletableFuture | ||
| */ | ||
| CompletableFuture<Void> disconnect(T mqttClient, @Nullable D mqttDisconnect); | ||
|
|
||
| } |
122 changes: 122 additions & 0 deletions
122
...rc/main/java/com/springframework/integration/hivemq/MqttClientConnectionCoordinators.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.springframework.integration.hivemq; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import com.hivemq.client.internal.mqtt.message.connect.mqtt3.Mqtt3ConnectView; | ||
| import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient; | ||
| import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck; | ||
| import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient; | ||
| import com.hivemq.client.mqtt.mqtt5.message.connect.Mqtt5Connect; | ||
| import com.hivemq.client.mqtt.mqtt5.message.connect.connack.Mqtt5ConnAck; | ||
| import com.hivemq.client.mqtt.mqtt5.message.disconnect.Mqtt5Disconnect; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * The {@link MqttClientConnectionCoordinators} for getting a particular {@link MqttClientConnectionCoordinator}. | ||
| * | ||
| * @author Jiandong Ma | ||
| * | ||
| * @since 7.2 | ||
| */ | ||
| public final class MqttClientConnectionCoordinators { | ||
|
|
||
| private static final Mqtt5AsyncClientConnectionCoordinator MQTT_5_ASYNC_CLIENT_CONNECTION_COORDINATOR | ||
| = new Mqtt5AsyncClientConnectionCoordinator(); | ||
|
|
||
| private static final Mqtt3AsyncClientConnectionCoordinator MQTT_3_ASYNC_CLIENT_CONNECTION_COORDINATOR | ||
| = new Mqtt3AsyncClientConnectionCoordinator(); | ||
|
|
||
| public static MqttClientConnectionCoordinator<Mqtt5AsyncClient, Mqtt5Connect, Mqtt5Disconnect> mqtt5AsyncClient() { | ||
| return MQTT_5_ASYNC_CLIENT_CONNECTION_COORDINATOR; | ||
| } | ||
|
|
||
| public static MqttClientConnectionCoordinator<Mqtt3AsyncClient, Mqtt3ConnectView, Object> mqtt3AsyncClient() { | ||
| return MQTT_3_ASYNC_CLIENT_CONNECTION_COORDINATOR; | ||
| } | ||
|
|
||
| private MqttClientConnectionCoordinators() { | ||
| } | ||
|
|
||
| /** | ||
| * A {@link MqttClientConnectionCoordinator} implementation for {@link Mqtt5AsyncClient}. | ||
| */ | ||
| static class Mqtt5AsyncClientConnectionCoordinator | ||
| implements MqttClientConnectionCoordinator<Mqtt5AsyncClient, Mqtt5Connect, Mqtt5Disconnect> { | ||
|
|
||
| Map<Mqtt5AsyncClient, CompletableFuture<Mqtt5ConnAck>> CONNECT_FUTURE_MAP = new ConcurrentHashMap<>(); | ||
|
|
||
| Map<Mqtt5AsyncClient, CompletableFuture<Void>> DISCONNECT_FUTURE_MAP = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public CompletableFuture<Mqtt5ConnAck> connect(Mqtt5AsyncClient mqttClient, Mqtt5Connect mqttConnect) { | ||
| return this.CONNECT_FUTURE_MAP.computeIfAbsent(mqttClient, client -> { | ||
| // Remove from disconnect map, in case dirty cache between lifecycle methods | ||
| this.DISCONNECT_FUTURE_MAP.remove(client); | ||
|
|
||
| return client.connect(mqttConnect); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> disconnect(Mqtt5AsyncClient mqttClient, @Nullable Mqtt5Disconnect mqttDisconnect) { | ||
| return this.DISCONNECT_FUTURE_MAP.computeIfAbsent(mqttClient, client -> { | ||
| // Remove from connect map, in case dirty cache between lifecycle methods | ||
| this.CONNECT_FUTURE_MAP.remove(client); | ||
|
|
||
| return mqttDisconnect != null ? client.disconnect(mqttDisconnect) : client.disconnect(); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * A {@link MqttClientConnectionCoordinator} implementation for {@link Mqtt3AsyncClient}. | ||
| */ | ||
| static class Mqtt3AsyncClientConnectionCoordinator | ||
| implements MqttClientConnectionCoordinator<Mqtt3AsyncClient, Mqtt3ConnectView, Object> { | ||
|
|
||
| Map<Mqtt3AsyncClient, CompletableFuture<Mqtt3ConnAck>> CONNECT_FUTURE_MAP = new ConcurrentHashMap<>(); | ||
|
|
||
| Map<Mqtt3AsyncClient, CompletableFuture<Void>> DISCONNECT_FUTURE_MAP = new ConcurrentHashMap<>(); | ||
|
|
||
| @Override | ||
| public CompletableFuture<Mqtt3ConnAck> connect(Mqtt3AsyncClient mqttClient, Mqtt3ConnectView mqttConnect) { | ||
| return this.CONNECT_FUTURE_MAP.computeIfAbsent(mqttClient, client -> { | ||
| // Remove from disconnect map, in case dirty cache between lifecycle methods | ||
| this.DISCONNECT_FUTURE_MAP.remove(client); | ||
|
|
||
| return client.connect(mqttConnect); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<Void> disconnect(Mqtt3AsyncClient mqttClient, @Nullable Object mqttDisconnect) { | ||
| return this.DISCONNECT_FUTURE_MAP.computeIfAbsent(mqttClient, client -> { | ||
| // Remove from connect map, in case dirty cache between lifecycle methods | ||
| this.CONNECT_FUTURE_MAP.remove(client); | ||
|
|
||
| return client.disconnect(); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
41 changes: 41 additions & 0 deletions
41
...src/main/java/com/springframework/integration/hivemq/event/MqttConnectionFailedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.springframework.integration.hivemq.event; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * The {@link MqttIntegrationEvent} to notify about lost connection to the server. | ||
| * When normal disconnection is happened (initiated by the server), the {@code cause} is null. | ||
| * | ||
| * @author Gary Russell | ||
| * @author Artem Bilan | ||
| * | ||
| * @since 7.2 | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public class MqttConnectionFailedEvent extends MqttIntegrationEvent { | ||
|
|
||
| public MqttConnectionFailedEvent(Object source) { | ||
| super(source); | ||
| } | ||
|
|
||
| public MqttConnectionFailedEvent(Object source, @Nullable Throwable cause) { | ||
| super(source, cause); | ||
| } | ||
|
|
||
| } |
42 changes: 42 additions & 0 deletions
42
...vemq/src/main/java/com/springframework/integration/hivemq/event/MqttIntegrationEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.springframework.integration.hivemq.event; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import org.springframework.integration.events.IntegrationEvent; | ||
|
|
||
| /** | ||
| * Base class for Mqtt Events. | ||
| * | ||
| * @author Gary Russell | ||
| * @author Artem Bilan | ||
| * | ||
| * @since 7.2 | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public abstract class MqttIntegrationEvent extends IntegrationEvent { | ||
|
|
||
| public MqttIntegrationEvent(Object source) { | ||
| super(source); | ||
| } | ||
|
|
||
| public MqttIntegrationEvent(Object source, @Nullable Throwable cause) { | ||
| super(source, cause); | ||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
...ivemq/src/main/java/com/springframework/integration/hivemq/event/MqttSubscribedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2026-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.springframework.integration.hivemq.event; | ||
|
|
||
| /** | ||
| * @author Gary Russell | ||
| * | ||
| * @since 7.2 | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public class MqttSubscribedEvent extends MqttIntegrationEvent { | ||
|
|
||
| private final String message; | ||
|
|
||
| public MqttSubscribedEvent(Object source, String message) { | ||
| super(source); | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return this.message; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "MqttSubscribedEvent [message=" + this.message + ", source=" + source + "]"; | ||
| } | ||
|
|
||
| } |
5 changes: 5 additions & 0 deletions
5
...ation-hivemq/src/main/java/com/springframework/integration/hivemq/event/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * ApplicationEvents generated by the hivemq MQTT module. | ||
| */ | ||
| @org.jspecify.annotations.NullMarked | ||
| package com.springframework.integration.hivemq.event; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for doing this contribution!
I know that I've asked about this name in the issue, but now when I think about this again it looks like the name is wrong.
This module is the client for MQTT protocol and doesn't matter what is the broker (or peer) is there we are connecting to.
Let's see if we can rename it into
spring-integration-mqtt-client!I am considering to deprecated existing module based on Paho.
Then one day we will remove Paho dependency and rename this new module back to just
spring-integration-mqtt.It is a bit awkward to name it
hivemqwhen it is going to work with any MQTT.Isn't it?
Sorry for confusion; and I'm open for any other suggestions!
Note: this new feature is aimed at least for the next
7.2due upcoming November release cycle.Therefore, bear with us how slow we are responding to your effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am ok to go ahead with the name
spring-integration-mqtt-client.Just It might bring a little confusion for end users to adjust the dependency, we might take effort to explain why name as this and finally as that.