-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3685: Share MQTT connection across components #3857
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3251fd2
GH-3685: Share MQTT connection across components
oxcafedead d9a0474
GH-3685: Share MQTT connection across components
oxcafedead 9f47cc8
GH-3685: Share MQTT connection across components
oxcafedead d338577
GH-3685: Share MQTT connection across components
oxcafedead 4cc094f
GH-3685: Share MQTT connection across components
oxcafedead f3fba7d
GH-3685: Share MQTT connection across components
oxcafedead f595998
GH-3685: Share MQTT connection across components
oxcafedead b5da36b
* Add Javadocs to abstract client manager
oxcafedead 318fd82
* Remove client factory as dependency for v3 client manager and use
oxcafedead 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
176 changes: 176 additions & 0 deletions
176
...tt/src/main/java/org/springframework/integration/mqtt/core/AbstractMqttClientManager.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,176 @@ | ||
/* | ||
* Copyright 2022-2022 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 org.springframework.integration.mqtt.core; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.context.SmartLifecycle; | ||
import org.springframework.integration.mqtt.inbound.AbstractMqttMessageDrivenChannelAdapter; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Abstract class for MQTT client managers which can be a base for any common v3/v5 client manager implementation. | ||
* Contains some basic utility and implementation-agnostic fields and methods. | ||
* | ||
* @param <T> MQTT client type | ||
* @param <C> MQTT connection options type (v5 or v3) | ||
* @param <P> MQTT client persistence type (for v5 or v3) | ||
* | ||
* @author Artem Vozhdayenko | ||
* | ||
* @since 6.0 | ||
*/ | ||
public abstract class AbstractMqttClientManager<T, C, P> implements ClientManager<T, C>, ApplicationEventPublisherAware { | ||
|
||
protected final Log logger = LogFactory.getLog(this.getClass()); // NOSONAR | ||
|
||
private static final int DEFAULT_MANAGER_PHASE = 0; | ||
|
||
private final Set<ConnectCallback> connectCallbacks = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
private final String clientId; | ||
|
||
private int phase = DEFAULT_MANAGER_PHASE; | ||
|
||
private boolean manualAcks; | ||
|
||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
private P persistence; | ||
|
||
private String url; | ||
|
||
private String beanName; | ||
|
||
private volatile T client; | ||
|
||
AbstractMqttClientManager(String clientId) { | ||
Assert.notNull(clientId, "'clientId' is required"); | ||
this.clientId = clientId; | ||
} | ||
|
||
protected void setManualAcks(boolean manualAcks) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that this one has to be |
||
this.manualAcks = manualAcks; | ||
} | ||
|
||
protected String getUrl() { | ||
return this.url; | ||
} | ||
|
||
protected void setUrl(String url) { | ||
this.url = url; | ||
oxcafedead marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
protected String getClientId() { | ||
return this.clientId; | ||
} | ||
|
||
protected ApplicationEventPublisher getApplicationEventPublisher() { | ||
return this.applicationEventPublisher; | ||
} | ||
|
||
protected synchronized void setClient(T client) { | ||
this.client = client; | ||
} | ||
|
||
protected P getPersistence() { | ||
return this.persistence; | ||
} | ||
|
||
/** | ||
* Set client persistence if some specific impl is required for topics QoS. | ||
* @param persistence persistence implementation to use for te client | ||
*/ | ||
public void setPersistence(P persistence) { | ||
this.persistence = persistence; | ||
} | ||
|
||
protected Set<ConnectCallback> getCallbacks() { | ||
return this.connectCallbacks; | ||
} | ||
|
||
@Override | ||
public boolean isManualAcks() { | ||
return this.manualAcks; | ||
} | ||
|
||
@Override | ||
public T getClient() { | ||
return this.client; | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
Assert.notNull(applicationEventPublisher, "'applicationEventPublisher' cannot be null"); | ||
this.applicationEventPublisher = applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
public void setBeanName(String name) { | ||
this.beanName = name; | ||
} | ||
|
||
@Override | ||
public String getBeanName() { | ||
return this.beanName; | ||
} | ||
|
||
/** | ||
* The phase of component autostart in {@link SmartLifecycle}. | ||
* If the custom one is required, note that for the correct behavior it should be less than phase of | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* {@link AbstractMqttMessageDrivenChannelAdapter} implementations. | ||
* The default phase is {@link #DEFAULT_MANAGER_PHASE}. | ||
* @return {@link SmartLifecycle} autostart phase | ||
* @see #setPhase | ||
*/ | ||
@Override | ||
public int getPhase() { | ||
return this.phase; | ||
} | ||
|
||
@Override | ||
public void addCallback(ConnectCallback connectCallback) { | ||
this.connectCallbacks.add(connectCallback); | ||
} | ||
|
||
@Override | ||
public boolean removeCallback(ConnectCallback connectCallback) { | ||
return this.connectCallbacks.remove(connectCallback); | ||
} | ||
|
||
public synchronized boolean isRunning() { | ||
return this.client != null; | ||
} | ||
|
||
/** | ||
* Set the phase of component autostart in {@link SmartLifecycle}. | ||
* If the custom one is required, note that for the correct behavior it should be less than phase of | ||
* {@link AbstractMqttMessageDrivenChannelAdapter} implementations. | ||
* @see #getPhase | ||
*/ | ||
public void setPhase(int phase) { | ||
this.phase = phase; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...tegration-mqtt/src/main/java/org/springframework/integration/mqtt/core/ClientManager.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,55 @@ | ||
/* | ||
* Copyright 2022-2022 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 org.springframework.integration.mqtt.core; | ||
|
||
import org.springframework.context.SmartLifecycle; | ||
|
||
/** | ||
* A utility abstraction over MQTT client which can be used in any MQTT-related component | ||
* without need to handle generic client callbacks, reconnects etc. | ||
* Using this manager in multiple MQTT integrations will preserve a single connection. | ||
* | ||
* @param <T> MQTT client type | ||
* @param <C> MQTT connection options type (v5 or v3) | ||
* | ||
* @author Artem Vozhdayenko | ||
* | ||
* @since 6.0 | ||
*/ | ||
public interface ClientManager<T, C> extends SmartLifecycle, MqttComponent<C> { | ||
|
||
T getClient(); | ||
|
||
boolean isManualAcks(); | ||
|
||
void addCallback(ConnectCallback connectCallback); | ||
|
||
boolean removeCallback(ConnectCallback connectCallback); | ||
|
||
/** | ||
* A contract for a custom callback if needed by a usage. | ||
artembilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @see org.eclipse.paho.mqttv5.client.MqttCallback#connectComplete | ||
* @see org.eclipse.paho.client.mqttv3.MqttCallbackExtended#connectComplete | ||
*/ | ||
interface ConnectCallback { | ||
|
||
void connectComplete(boolean isReconnect); | ||
|
||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.