Class PahoMqttTransport
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the connection and releases resources.voidconnect()Establishes the broker connection.voidPublishes a message.voidsubscribe(String topicFilter, int qos, BiConsumer<String, byte[]> handler) Subscribes to a topic filter, delivering matching messages tohandleras(topic, payload).
-
Constructor Details
-
PahoMqttTransport
- 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.MqttExceptionDescription copied from interface:MqttTransportEstablishes the broker connection.- Specified by:
connectin interfaceMqttTransport- 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:MqttTransportPublishes a message.- Specified by:
publishin interfaceMqttTransport- Parameters:
topic- the topic to publish topayload- the message payloadqos- 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.MqttExceptionDescription copied from interface:MqttTransportSubscribes to a topic filter, delivering matching messages tohandleras(topic, payload).- Specified by:
subscribein interfaceMqttTransport- Parameters:
topicFilter- the MQTT topic filter (may contain+/#wildcards)qos- the requested QoShandler- receives(topic, payload)for each matching message- Throws:
org.eclipse.paho.client.mqttv3.MqttException
-
close
public void close()Description copied from interface:MqttTransportCloses the connection and releases resources.- Specified by:
closein interfaceMqttTransport
-
