public class MqttClient extends Object implements AutoCloseable
MqttClient
class can be used to create MQTT client
instances.Constructor and Description |
---|
MqttClient()
Creates an MQTT context.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Destroys an MQTT context.
|
void |
connect(MqttConfig config)
Connects to an MQTT broker.
|
void |
disconnect()
Disconnects from an MQTT broker.
|
void |
publish(String topic,
byte[] payload,
int qos,
boolean retained)
Publishes a message to a particular topic.
|
short |
publishGetMsgId(String topic,
byte[] payload,
int qos,
boolean retained)
Publishes a message to a particular topic.
|
void |
setConnectCallback(MqttConnectCallback callback)
Sets a connect callback, which is invoked when the connect is successful.
|
void |
setMessageCallback(MqttMessageCallback callback)
Sets a message callback, which is invoked when a message is received for a
subscribed topic.
|
void |
setPublishCallback(MqttPublishCallback callback)
Sets a publish callback, which is invoked when
PUBACK(QOS1)/PUBREC,PUBCOMP(QOS2).
|
void |
setSubscribeCallback(MqttSubscribeCallback callback)
Sets a subscribe callback, which is invoked when a subscription is granted or
denied.
|
void |
subscribe(String topic,
int qos)
Subscribes to a topic.
|
void |
unsubscribe(String topic)
Unsubscribes from a topic.
|
public void close()
close
in interface AutoCloseable
public void connect(MqttConfig config) throws IOException
config
- MQTT client configuration.IOException
- on failure.public void disconnect()
public void publish(String topic, byte[] payload, int qos, boolean retained) throws IOException
topic
- MQTT topic.payload
- MQTT payload.qos
- QOS to be used for the message.retained
- Retain flag.IOException
- on failure.public short publishGetMsgId(String topic, byte[] payload, int qos, boolean retained) throws IOException
topic
- MQTT topic.payload
- MQTT payload.qos
- QOS to be used for the message.retained
- Retain flag.IOException
- on failure.public void setConnectCallback(@Nullable MqttConnectCallback callback)
callback
- Callback to be invoked.public void setMessageCallback(@Nullable MqttMessageCallback callback)
callback
- Callback to be invoked.public void setPublishCallback(@Nullable MqttPublishCallback callback)
callback
- Callback to be invoked.public void setSubscribeCallback(@Nullable MqttSubscribeCallback callback)
callback
- Callback to be invoked.public void subscribe(String topic, int qos) throws IOException
topic
- Subscription topic.qos
- QoS to be used.IOException
- on failure.public void unsubscribe(String topic) throws IOException
topic
- Topic from which to unsubscribe.IOException
- on failure.