Package software.amazon.awssdk.crt.mqtt
Class MqttClientConnection
- java.lang.Object
-
- software.amazon.awssdk.crt.CrtResource
-
- software.amazon.awssdk.crt.mqtt.MqttClientConnection
-
- All Implemented Interfaces:
AutoCloseable
public class MqttClientConnection extends CrtResource
This class wraps aws-c-mqtt to provide the basic MQTT pub/sub functionality via the AWS Common Runtime MqttClientConnection represents a single connection from one MqttClient to an MQTT service endpoint
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class software.amazon.awssdk.crt.CrtResource
CrtResource.ResourceInstance
-
-
Constructor Summary
Constructors Constructor Description MqttClientConnection(MqttConnectionConfig config)Constructs a new MqttClientConnection.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected booleancanReleaseReferencesImmediately()Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.CompletableFuture<Boolean>connect()Connect to the service endpoint and start a sessionCompletableFuture<Void>disconnect()Disconnects the current sessionvoidonMessage(java.util.function.Consumer<MqttMessage> handler)Sets a handler to be invoked whenever a message arrives, subscription or notCompletableFuture<Integer>publish(MqttMessage message)Publishes a message to a topic.CompletableFuture<Integer>publish(MqttMessage message, QualityOfService qos, boolean retain)Deprecated.protected voidreleaseNativeHandle()Frees native resources associated with this connection.CompletableFuture<Integer>subscribe(String topic, QualityOfService qos)Subscribes to a topic without a handler (messages will only be delivered to the OnMessage handler)CompletableFuture<Integer>subscribe(String topic, QualityOfService qos, java.util.function.Consumer<MqttMessage> handler)Subscribes to a topicCompletableFuture<Integer>unsubscribe(String topic)Unsubscribes from a topic-
Methods inherited from class software.amazon.awssdk.crt.CrtResource
acquireNativeHandle, addRef, addReferenceTo, close, collectNativeResource, collectNativeResources, decRef, getNativeHandle, getResourceLogDescription, isNull, logNativeResources, releaseReferences, removeReferenceTo, setDescription, swapReferenceTo, waitForNoResources
-
-
-
-
Constructor Detail
-
MqttClientConnection
public MqttClientConnection(MqttConnectionConfig config) throws MqttException
Constructs a new MqttClientConnection. Connections are reusable after being disconnected.- Parameters:
config- Configuration to use- Throws:
MqttException- If mqttClient is null
-
-
Method Detail
-
releaseNativeHandle
protected void releaseNativeHandle()
Frees native resources associated with this connection.- Specified by:
releaseNativeHandlein classCrtResource
-
canReleaseReferencesImmediately
protected boolean canReleaseReferencesImmediately()
Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits. Resources that wait are responsible for calling releaseReferences() manually.- Specified by:
canReleaseReferencesImmediatelyin classCrtResource- Returns:
- true if this resource releases synchronously, false if this resource performs async shutdown
-
connect
public CompletableFuture<Boolean> connect() throws MqttException
Connect to the service endpoint and start a session- Returns:
- Future result is true if resuming a session, false if clean session
- Throws:
MqttException- If the port is out of range
-
disconnect
public CompletableFuture<Void> disconnect()
Disconnects the current session- Returns:
- When this future completes, the disconnection is complete
-
subscribe
public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos, java.util.function.Consumer<MqttMessage> handler)
Subscribes to a topic- Parameters:
topic- The topic to subscribe toqos-QualityOfServicefor this subscriptionhandler- A handler which can receive an MqttMessage when a message is published to the topic- Returns:
- Future result is the packet/message id associated with the subscribe operation
-
subscribe
public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos)
Subscribes to a topic without a handler (messages will only be delivered to the OnMessage handler)- Parameters:
topic- The topic to subscribe toqos-QualityOfServicefor this subscription- Returns:
- Future result is the packet/message id associated with the subscribe operation
-
onMessage
public void onMessage(java.util.function.Consumer<MqttMessage> handler)
Sets a handler to be invoked whenever a message arrives, subscription or not- Parameters:
handler- A handler which can receive any MqttMessage
-
unsubscribe
public CompletableFuture<Integer> unsubscribe(String topic)
Unsubscribes from a topic- Parameters:
topic- The topic to unsubscribe from- Returns:
- Future result is the packet/message id associated with the unsubscribe operation
-
publish
public CompletableFuture<Integer> publish(MqttMessage message)
Publishes a message to a topic.- Parameters:
message- The message to publish.- Returns:
- Future value is the packet/message id associated with the publish operation
-
publish
@Deprecated public CompletableFuture<Integer> publish(MqttMessage message, QualityOfService qos, boolean retain)
Deprecated.
-
-