public class MqttClient extends Object implements IMqttClient
This class implements the blocking IMqttClient
client interface where all actions block until they have
completed (or timed out). This implementation is compatible with MicroEJ runtime.
An application can connect to an MQTT server using any kind of underlying transport layer that can be created from a
SocketFactory
(i.e. bi-directional lossless stream), which is likely one of:
IMqttClient
Modifier and Type | Field and Description |
---|---|
protected long |
lastOutboundActivityMillis
Client lock must be owned before reading or writing this field.
|
protected static String |
MQTT
The "MQTT" String constant.
|
protected static int |
MQTT_QOS0
MQTT QoS 0.
|
protected static int |
MQTT_QOS1
MQTT QoS 1.
|
protected MqttException |
pendingMessageAckException
The pending message acknowledgment exception returned by the server, or
null if is there is no
pending message or if the acknowledgment returned without error. |
Constructor and Description |
---|
MqttClient(String serverURI,
String clientId)
Create an MqttClient that can be used to communicate with an MQTT server.
|
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.
|
protected long lastOutboundActivityMillis
protected static final String MQTT
protected static final int MQTT_QOS0
protected static final int MQTT_QOS1
@Nullable protected MqttException pendingMessageAckException
null
if is there is no
pending message or if the acknowledgment returned without error.public MqttClient(String serverURI, String clientId) throws MqttException
The address of a server can be specified on the constructor.
The serverURI
parameter is typically used with the the clientId
parameter to form a
key. The key is used to store and reference messages while they are being delivered. Hence the serverURI
specified on the constructor must still be specified even if a list of servers is specified on an
MqttConnectOptions object. The serverURI on the constructor must remain the same across restarts of the client
for delivery of messages to be maintained from a given client to a given server or set of servers.
The address of the server to connect to is specified as a URI. Two types of connection are supported
tcp://
for a TCP connection and ssl://
for a TCP connection secured by SSL/TLS. For
example:
tcp://localhost:1883
ssl://localhost:8883
If the port is not specified, it will default to 1883 for tcp://
" URIs, and 8883 for
ssl://
URIs.
A client identifier clientId
must be specified and be less that 65535 characters. It must be unique
across all clients connecting to the same server. The clientId is used by the server to store data related to the
client, hence it is important that the clientId remain the same when connecting to a server if durable
subscriptions or reliable messaging are required.
SSL can be configured by supplying an javax.net.ssl.SSLSocketFactory
- applications can use
MqttConnectOptions.setSocketFactory(SocketFactory)
to supply a factory with the appropriate SSL settings.
serverURI
- the address of the server to connect to, specified as a URI.clientId
- a client identifier that is unique on the server being connected toIllegalArgumentException
- if the URI does not start with "tcp://" or "ssl://" or is invalidMqttException
- if any other problem was encounteredpublic void close() throws MqttException
IMqttClient
close
in interface AutoCloseable
close
in interface IMqttClient
MqttException
- if the client is not disconnected.public void connect() throws MqttException
IMqttClient
The default options are specified in MqttConnectOptions
class.
connect
in interface IMqttClient
MqttException
- for non security related problemsIMqttClient.connect(MqttConnectOptions)
public void connect(MqttConnectOptions options) throws MqttException
IMqttClient
The server to connect to is specified on the constructor. It is recommended to call
IMqttClient.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
connect
in interface IMqttClient
options
- a set of connection parameters that override the defaults.MqttException
- for non security related problems including communication errorspublic void disconnect() throws MqttException
IMqttClient
This is a blocking method that returns once disconnect completes
disconnect
in interface IMqttClient
MqttException
- if a problem is encountered while disconnectingpublic String getClientId()
IMqttClient
All clients connected to the same server or server farm must have a unique ID.
getClientId
in interface IMqttClient
public String getServerURI()
IMqttClient
The format is the same as specified on the constructor.
getServerURI
in interface IMqttClient
MqttClient(String, String)
public boolean isConnected()
IMqttClient
isConnected
in interface IMqttClient
true
if connected, false
otherwise.public void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException
IMqttClient
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.
publish
in interface IMqttClient
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.MqttException
- for other errors encountered while publishing the message. For instance client not connected.IMqttClient.publish(String, MqttMessage)
,
MqttMessage.setQos(int)
,
MqttMessage.setRetained(boolean)
public void publish(String topic, MqttMessage message) throws MqttException
IMqttClient
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
publish
in interface IMqttClient
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.public void setCallback(MqttCallback callback)
IMqttClient
There are a number of events that listener will be notified about. These include:
setCallback
in interface IMqttClient
callback
- the class to callback when for events related to the clientMqttCallback
public void subscribe(String topicFilter) throws MqttException
IMqttClient
subscribe
in interface IMqttClient
topicFilter
- the topic to subscribe to, which can include wildcards.MqttException
- if there was an error registering the subscription.public void subscribe(String topicFilter, int qos) throws MqttException
IMqttClient
subscribe
in interface IMqttClient
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.public void unsubscribe(String topicFilter) throws MqttException
IMqttClient
unsubscribe
in interface IMqttClient
topicFilter
- the topic to unsubscribe from. It must match a topicFilter specified on the subscribe.MqttException
- if there was an error unregistering the subscription.