public interface IMqttClient extends AutoCloseable
This interface allows applications to utilize all features of the MQTT version 3.1 specification including:
IMqttClient provides a set of methods that block and return control to the application program once the MQTT action has completed.
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the client Releases all resource associated with the client.
|
void |
connect()
Connects to an MQTT server using the default options.
|
void |
connect(MqttConnectOptions options)
Connects to an MQTT server using the specified options.
|
void |
disconnect()
Disconnects from the server.
|
String |
getClientId()
Returns the client ID used by this client.
|
String |
getServerURI()
Returns the address of the server used by this client, as a URI.
|
boolean |
isConnected()
Determines if this client is currently connected to the server.
|
void |
publish(String topic,
byte[] payload,
int qos,
boolean retained)
Publishes a message to a topic on the server and return once it is delivered.
|
void |
publish(String topic,
MqttMessage message)
Publishes a message to a topic on the server.
|
void |
setCallback(MqttCallback callback)
Sets the callback listener to use for events that happen asynchronously.
|
void |
subscribe(String topicFilter)
Subscribe to a topic, which may include wildcards using a QoS of 1.
|
void |
subscribe(String topicFilter,
int qos)
Subscribe to a topic, which may include wildcards.
|
void |
unsubscribe(String topicFilter)
Requests the server unsubscribe the client from a topic.
|
void close() throws MqttException
close
in interface AutoCloseable
MqttException
- if the client is not disconnected.void connect() throws MqttException
The default options are specified in MqttConnectOptions
class.
MqttException
- for non security related problemsconnect(MqttConnectOptions)
void connect(MqttConnectOptions options) throws MqttException
The server to connect to is specified on the constructor. It is recommended to call
setCallback(MqttCallback)
prior to connecting in order that messages destined for the client can be
accepted as soon as the client is connected.
This is a blocking method that returns once connect completes
options
- a set of connection parameters that override the defaults.MqttException
- for non security related problems including communication errorsvoid disconnect() throws MqttException
This is a blocking method that returns once disconnect completes
MqttException
- if a problem is encountered while disconnectingString getClientId()
All clients connected to the same server or server farm must have a unique ID.
String getServerURI()
The format is the same as specified on the constructor.
MqttClient(String, String)
boolean isConnected()
true
if connected, false
otherwise.void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException
This is a convenience method, which will create a new MqttMessage
object with a byte array payload and
the specified QoS, and then publish it. All other values in the message will be set to the defaults.
topic
- to deliver the message to, for example "finance/stock/ibm".payload
- the byte array to use as the payloadqos
- the Quality of Service to deliver the message at. Valid values are 0, 1 or 2.retained
- whether or not this message should be retained by the server.IllegalArgumentException
- if value of QoS is not 0, 1 or 2.MqttException
- for other errors encountered while publishing the message. For instance client not connected.publish(String, MqttMessage)
,
MqttMessage.setQos(int)
,
MqttMessage.setRetained(boolean)
void publish(String topic, MqttMessage message) throws MqttException
Delivers a message to the server at the requested quality of service and returns control once the message has been delivered.
This is a blocking method that returns once publish completes
topic
- to deliver the message to, for example "finance/stock/ibm".message
- to delivery to the serverMqttException
- for other errors encountered while publishing the message. For instance client not connected.void setCallback(MqttCallback callback)
There are a number of events that listener will be notified about. These include:
callback
- the class to callback when for events related to the clientMqttCallback
void subscribe(String topicFilter) throws MqttException
topicFilter
- the topic to subscribe to, which can include wildcards.MqttException
- if there was an error registering the subscription.void subscribe(String topicFilter, int qos) throws MqttException
topicFilter
- the topic to subscribe to, which can include wildcards.qos
- the maximum quality of service at which to subscribe. Messages published at a lower quality of service
will be received at the published QoS. Messages published at a higher quality of service will be
received using the QoS specified on the subscribe.MqttException
- if there was an error registering the subscription.void unsubscribe(String topicFilter) throws MqttException
topicFilter
- the topic to unsubscribe from. It must match a topicFilter specified on the subscribe.MqttException
- if there was an error unregistering the subscription.