Class MqttClient

java.lang.Object
com.codename1.io.mqtt.MqttClient

public final class MqttClient extends Object

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 Details

    • MqttClient

      public MqttClient()
      Creates a disconnected client.
  • 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).

      Parameters
      • options: broker and session settings

      • listener: receives the callbacks

      Throws

      IllegalStateException when already connected or connecting, IllegalArgumentException for 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

      public void publish(String topic, byte[] payload, int qos, boolean retain)

      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

      public void subscribe(String topicFilter, int qos)

      Subscribes; matching messages go to the MqttListener.

      Parameters
      • topicFilter: the filter, + and # allowed

      • qos: the maximum QoS wanted, 0 or 1

    • subscribe

      public void subscribe(String topicFilter, int qos, MqttMessageListener messageListener)

      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 the MqttListener

    • unsubscribe

      public void unsubscribe(String topicFilter)

      Removes a subscription.

      Parameters
      • topicFilter: the filter given to subscribe
    • disconnect

      public void disconnect()
      Sends DISCONNECT and closes the connection. No connectionLost follows. QoS 1 publishes still unacknowledged stay queued for the next connect.