public class MqttClientConnection extends CrtResource
CrtResource.ResourceInstance
Constructor and Description |
---|
MqttClientConnection(MqttConnectionConfig config)
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()
Connect to the service endpoint and start a session
|
CompletableFuture<Void> |
disconnect()
Disconnects the current session
|
void |
onMessage(java.util.function.Consumer<MqttMessage> handler)
Sets a handler to be invoked whenever a message arrives, subscription or not
|
CompletableFuture<Integer> |
publish(MqttMessage message,
QualityOfService qos,
boolean retain)
Publishes a message to a topic
|
protected void |
releaseNativeHandle()
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 topic
|
CompletableFuture<Integer> |
unsubscribe(String topic)
Unsubscribes from a topic
|
acquireNativeHandle, addRef, addReferenceTo, close, collectNativeResource, collectNativeResources, decRef, getNativeHandle, getResourceLogDescription, isNull, logNativeResources, releaseReferences, removeReferenceTo, setDescription, swapReferenceTo, waitForNoResources
public MqttClientConnection(MqttConnectionConfig config) throws MqttException
mqttClient
- Mqtt client to use. Must be non-null.clientId
- Mqtt client id to use for this connection. Must be non-null.endpoint
- Endpoint to connect to. Must be non-null.port
- Port to connect on.MqttException
- If mqttClient is nullprotected void releaseNativeHandle()
releaseNativeHandle
in class CrtResource
protected boolean canReleaseReferencesImmediately()
canReleaseReferencesImmediately
in class CrtResource
public CompletableFuture<Boolean> connect() throws MqttException
MqttException
- 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> subscribe(String topic, QualityOfService qos)
topic
- The topic to subscribe toqos
- QualityOfService
for this subscriptionpublic void onMessage(java.util.function.Consumer<MqttMessage> handler)
handler
- A handler which can receive any MqttMessagepublic 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 subscribersCopyright © 2020. All rights reserved.