public class MqttClientConnection extends CrtResource
Constructor and Description |
---|
MqttClientConnection(MqttClient mqttClient)
Constructs a new MqttClientConnection.
|
MqttClientConnection(MqttClient mqttClient,
MqttClientConnectionEvents callbacks)
Constructs a new MqttClientConnection.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
canReleaseReferencesImmediately()
Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
|
CompletableFuture<Boolean> |
connect(String clientId,
String endpoint,
int port)
Connect to the service endpoint and start a session without TLS.
|
CompletableFuture<Boolean> |
connect(String clientId,
String endpoint,
int port,
SocketOptions socketOptions,
boolean cleanSession,
int keepAliveMs,
int pingTimeoutMs)
Connect to the service endpoint and start a session
|
CompletableFuture<Void> |
disconnect()
Disconnects the current session
|
CompletableFuture<Integer> |
publish(MqttMessage message,
QualityOfService qos,
boolean retain)
Publishes a message to a topic
|
protected void |
releaseNativeHandle()
Disconnects if necessary, and frees native resources associated with this connection
|
void |
setLogin(String user,
String pass)
Sets the login credentials for the connection.
|
void |
setWill(MqttMessage message,
QualityOfService qos,
boolean retain)
Sets the last will and testament message to be delivered to a topic when this client disconnects
|
CompletableFuture<Integer> |
subscribe(String topic,
QualityOfService qos,
java.util.function.Consumer<MqttMessage> handler)
Subscribes to a topic
|
CompletableFuture<Integer> |
unsubscribe(String topic)
Unsubscribes from a topic
|
acquireNativeHandle, addReferenceTo, close, collectNativeResources, getNativeHandle, isNativeResource, isNull, logNativeResources, releaseReferences, waitForNoResources
public MqttClientConnection(MqttClient mqttClient) throws MqttException
mqttClient
- Must be non-nullMqttException
- If mqttClient is nullpublic MqttClientConnection(MqttClient mqttClient, MqttClientConnectionEvents callbacks) throws MqttException
mqttClient
- Must be non-nullcallbacks
- Optional handler for connection interruptions/resumptionsMqttException
- If mqttClient is nullprotected void releaseNativeHandle()
releaseNativeHandle
in class CrtResource
protected boolean canReleaseReferencesImmediately()
canReleaseReferencesImmediately
in class CrtResource
public void setLogin(String user, String pass) throws MqttException
user
- Login usernamepass
- Login passwordMqttException
- If the username or password are nullpublic CompletableFuture<Boolean> connect(String clientId, String endpoint, int port)
clientId
- The clientId provided to the service. Must be unique across all connected Things on the endpoint.endpoint
- The hostname of the service endpointport
- The port to connect to on the service endpoint hostpublic CompletableFuture<Boolean> connect(String clientId, String endpoint, int port, SocketOptions socketOptions, boolean cleanSession, int keepAliveMs, int pingTimeoutMs) throws MqttException
clientId
- The clientId provided to the service. Must be unique across all connected Things on the endpoint.endpoint
- The hostname of the service endpointport
- The port to connect to on the service endpoint hostsocketOptions
- Optional SocketOptions instance with options for this connectiontls
- Optional TLS context. If this is null, a TLS connection will not be attempted.cleanSession
- Whether or not to completely restart the session. If false, topics that were already
subscribed by this clientId will be resumedkeepAliveMs
- 0 = no keepalive, non-zero = ms between keepalive packetsMqttException
- If the port is out of rangepublic CompletableFuture<Void> disconnect()
public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos, java.util.function.Consumer<MqttMessage> handler)
topic
- The topic to subscribe toqos
- QualityOfService
for this subscriptionhandler
- A handler which can recieve an MqttMessage when a message is published to the topicpublic CompletableFuture<Integer> unsubscribe(String topic)
topic
- The topic to unsubscribe frompublic CompletableFuture<Integer> publish(MqttMessage message, QualityOfService qos, boolean retain)
message
- The message to publish. The message contains the topic to publish to.qos
- The QualityOfService
to use for the publish operationretain
- Whether or not the message should be retained by the broker to be delivered to future subscriberspublic void setWill(MqttMessage message, QualityOfService qos, boolean retain) throws MqttException
message
- The message to publish as the will. The message contains the topic that the message will be
published to on disconnect.qos
- The QualityOfService
of the will messageretain
- Whether or not the message should be retained by the broker to be delivered to future subscribersMqttException
- If the connection is already connected, or is otherwise unable to set the willCopyright © 2019. All rights reserved.