Class PahoMqttTransport

java.lang.Object
com.framed.interop.mqtt.PahoMqttTransport
All Implemented Interfaces:
MqttTransport

public final class PahoMqttTransport extends Object implements MqttTransport
MqttTransport backed by the Eclipse Paho MQTT 3.1.1 asynchronous client.

The async client is used deliberately so publish(java.lang.String, byte[], int) never blocks the calling thread on network I/O: it hands the message to Paho's sender and returns immediately. This matters because publishes originate on the FRAMED bus handler thread, which must stay responsive (the synchronous MqttClient would block until the broker acknowledges, indefinitely while it is down). connect() and subscribe(java.lang.String, int, java.util.function.BiConsumer<java.lang.String, byte[]>) run only at startup and wait with a bounded timeout.

A single Paho callback dispatches each incoming message to every distinct handler whose topic filter matches, so one connection can serve multiple inbound filters. A handler registered under several overlapping filters (e.g. a/# and a/+) is invoked at most once per delivery. Note that with overlapping filters the broker itself may still deliver one copy per matching subscription (permitted by MQTT 3.1.1 §3.3.5) — prefer non-overlapping subscribe filters.

  • Constructor Details

    • PahoMqttTransport

      public PahoMqttTransport(String brokerUrl, String clientId)
      Parameters:
      brokerUrl - broker URI (e.g. tcp://127.0.0.1:1883)
      clientId - MQTT client id
  • Method Details

    • connect

      public void connect() throws org.eclipse.paho.client.mqttv3.MqttException
      Description copied from interface: MqttTransport
      Establishes the broker connection.
      Specified by:
      connect in interface MqttTransport
      Throws:
      org.eclipse.paho.client.mqttv3.MqttException
    • publish

      public void publish(String topic, byte[] payload, int qos) throws org.eclipse.paho.client.mqttv3.MqttException
      Description copied from interface: MqttTransport
      Publishes a message.
      Specified by:
      publish in interface MqttTransport
      Parameters:
      topic - the topic to publish to
      payload - the message payload
      qos - the MQTT quality-of-service level (0/1/2)
      Throws:
      org.eclipse.paho.client.mqttv3.MqttException
    • subscribe

      public void subscribe(String topicFilter, int qos, BiConsumer<String,byte[]> handler) throws org.eclipse.paho.client.mqttv3.MqttException
      Description copied from interface: MqttTransport
      Subscribes to a topic filter, delivering matching messages to handler as (topic, payload).
      Specified by:
      subscribe in interface MqttTransport
      Parameters:
      topicFilter - the MQTT topic filter (may contain + / # wildcards)
      qos - the requested QoS
      handler - receives (topic, payload) for each matching message
      Throws:
      org.eclipse.paho.client.mqttv3.MqttException
    • close

      public void close()
      Description copied from interface: MqttTransport
      Closes the connection and releases resources.
      Specified by:
      close in interface MqttTransport