Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class MqttClient
- Object
- 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)).
Constructors
public MqttClient() | Creates a disconnected client. |
Methods
public void connect(MqttOptions options, MqttListener listener) | Opens a connection and sends CONNECT. |
public boolean isConnected() | Whether the broker has accepted the connection and it is still up. |
public int getInFlightCount() | The number of QoS 1 publishes not yet acknowledged, queued ones included. |
public void publish(String topic, byte[] payload, int qos, boolean retain) | Publishes a message. |
public void subscribe(String topicFilter, int qos) | Subscribes; matching messages go to the MqttListener. |
public void subscribe(String topicFilter, int qos, MqttMessageListener messageListener) | Subscribes and routes matching messages to a listener of their own. |
public void unsubscribe(String topicFilter) | Removes a subscription. |
public void disconnect() | Sends DISCONNECT and closes the connection. |
Inherited methods
Constructor details
MqttClient
public MqttClient()Method details
connect
public void connect(MqttOptions options, MqttListener listener)Opens a connection and sends CONNECT. Returns at once; the outcome arrives
as MqttListener.connected() or MqttListener.connectionLost(String).
Throws
IllegalStateException when already connected or connecting,
IllegalArgumentException for an unsupported URI
Parameters
optionsMqttOptions- broker and session settings
listenerMqttListener- receives the callbacks
isConnected
public boolean isConnected()getInFlightCount
public int getInFlightCount()publish
public void publish(String topic, byte[] payload, int qos, boolean retain)Parameters
topicString- the topic name, without wildcards
payloadbyte[]- the message body
qosint- 0 (needs a connection) or 1 (queued until connected)
retainboolean- ask the broker to retain the message
subscribe
public void subscribe(String topicFilter, int qos)MqttListener.Parameters
topicFilterString- the filter,
+and#allowed qosint- the maximum QoS wanted, 0 or 1
subscribe
public void subscribe(String topicFilter, int qos, MqttMessageListener messageListener)Parameters
topicFilterString- the filter,
+and#allowed qosint- the maximum QoS wanted, 0 or 1
messageListenerMqttMessageListener- receives the matches; null means the
MqttListener
unsubscribe
public void unsubscribe(String topicFilter)Parameters
topicFilterString- the filter given to subscribe
disconnect
public void disconnect()connectionLost follows.
QoS 1 publishes still unacknowledged stay queued for the next connect.