Class MqttClient
An MQTT 3.1.1 client for QoS 0 and QoS 1.
Threading
Like the rest of the framework the client is single threaded: call it on the
EDT, and every MqttListener / MqttMessageListener callback arrives there.
Transport threads only post to the EDT, so the client itself holds no locks.
Writes happen on the calling thread.
Delivery
A QoS 1 publish stays in flight until its PUBACK and is sent again, with the DUP flag, after a reconnect; with a clean session the in flight list is dropped when connecting. A QoS 1 publish made while disconnected is queued and sent after the next successful connect. QoS 0 needs a connection. An incoming QoS 1 message is acknowledged before it is delivered.
Keep alive
A small daemon thread ticks once a second and posts the check to the EDT. A PINGREQ goes out when nothing was sent for the keep alive interval; when no PINGRESP arrives within one and a half intervals the connection is declared lost. The same tick enforces the connect timeout.
Subscriptions
A subscription made with a listener routes matching messages to it only; a
message no such subscription claims goes to the MqttListener. The client
does not re-subscribe on reconnect: do it from MqttListener.connected()
(a broker keeps the subscriptions of a persistent session, cleanSession(false)).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidconnect(MqttOptions options, MqttListener listener) Opens a connection and sends CONNECT.voidSends DISCONNECT and closes the connection.intThe number of QoS 1 publishes not yet acknowledged, queued ones included.booleanWhether the broker has accepted the connection and it is still up.voidPublishes a message.voidSubscribes; matching messages go to theMqttListener.voidsubscribe(String topicFilter, int qos, MqttMessageListener messageListener) Subscribes and routes matching messages to a listener of their own.voidunsubscribe(String topicFilter) Removes a subscription.
-
Constructor Details
-
MqttClient
public MqttClient()Creates a disconnected client.
-
-
Method Details
-
connect
Opens a connection and sends CONNECT. Returns at once; the outcome arrives as
MqttListener.connected()orMqttListener.connectionLost(String).Parameters
-
options: broker and session settings -
listener: receives the callbacks
Throws
IllegalStateExceptionwhen already connected or connecting,IllegalArgumentExceptionfor an unsupported URI -
-
isConnected
public boolean isConnected()Whether the broker has accepted the connection and it is still up. -
getInFlightCount
public int getInFlightCount()The number of QoS 1 publishes not yet acknowledged, queued ones included. -
publish
Publishes a message.
Parameters
-
topic: the topic name, without wildcards -
payload: the message body -
qos: 0 (needs a connection) or 1 (queued until connected) -
retain: ask the broker to retain the message
-
-
subscribe
Subscribes; matching messages go to the
MqttListener.Parameters
-
topicFilter: the filter,+and#allowed -
qos: the maximum QoS wanted, 0 or 1
-
-
subscribe
Subscribes and routes matching messages to a listener of their own.
Parameters
-
topicFilter: the filter,+and#allowed -
qos: the maximum QoS wanted, 0 or 1 -
messageListener: receives the matches; null means theMqttListener
-
-
unsubscribe
Removes a subscription.
Parameters
topicFilter: the filter given to subscribe
-
disconnect
public void disconnect()Sends DISCONNECT and closes the connection. NoconnectionLostfollows. QoS 1 publishes still unacknowledged stay queued for the next connect.
-