Package org.chenile.mqtt.pubsub
Class MqttSubscriber
java.lang.Object
org.chenile.mqtt.pubsub.MqttSubscriber
- All Implemented Interfaces:
org.eclipse.paho.mqttv5.client.MqttCallback
MQTT subscription call back that supports receiving messages, reconnects etc.
-
Field Summary
Modifier and TypeFieldDescription(package private) org.slf4j.Logger
private final boolean
(package private) MqttEntryPoint
(package private) MqttInfoProvider
(package private) MqttPublisher
(package private) org.eclipse.paho.mqttv5.client.MqttAsyncClient
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
authPacketArrived
(int reasonCode, org.eclipse.paho.mqttv5.common.packet.MqttProperties properties) For authentication purposes.void
connectComplete
(boolean reconnect, String serverURI) This happens whenever we connect to the broker.void
deliveryComplete
(org.eclipse.paho.mqttv5.client.IMqttToken token) Upon successful delivery of messages that we have sent.void
disconnected
(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse) private void
void
messageArrived
(String topic, org.eclipse.paho.mqttv5.common.MqttMessage message) Called when a new message arrives.void
mqttErrorOccurred
(org.eclipse.paho.mqttv5.common.MqttException exception) private boolean
shouldIgnore
(org.eclipse.paho.mqttv5.common.MqttMessage message) This method performs a few checks to see if this message needs to be ignored.
-
Field Details
-
mqttEnabled
private final boolean mqttEnabled -
mqttConfig
-
v5Client
-
logger
org.slf4j.Logger logger -
mqttEntryPoint
-
mqttInfoProvider
-
publisher
-
-
Constructor Details
-
MqttSubscriber
public MqttSubscriber(boolean mqttEnabled)
-
-
Method Details
-
log
-
disconnected
public void disconnected(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse) - Specified by:
disconnected
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
-
mqttErrorOccurred
public void mqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception) - Specified by:
mqttErrorOccurred
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
-
messageArrived
public void messageArrived(String topic, org.eclipse.paho.mqttv5.common.MqttMessage message) throws Exception Called when a new message arrives. This method delegates to MqttEntryPoint for processing the message.- Specified by:
messageArrived
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
- Parameters:
topic
- the topic at which we received this messagemessage
- the message that is received- Throws:
Exception
- if we have an error in processing the message
-
shouldIgnore
This method performs a few checks to see if this message needs to be ignored. First, it checks to see if we are the source of the message in which case, we can ignore the message. (why send a message to ourselves) Second, if the target is set then it checks to see if we are the target. This is useful when the edge wants ti tell the cloud (and only the cloud) to update itself. Third, if the target is set but starts with a ! then this means that the message is meant for everyone except the target. So we check if we are the one who need to ignore the message- Parameters:
message
- the message that needs to be checked- Returns:
- true if it needs to be ignored false otherwise
- Throws:
Exception
- if an exception is thrown in sending an ack
-
deliveryComplete
public void deliveryComplete(org.eclipse.paho.mqttv5.client.IMqttToken token) Upon successful delivery of messages that we have sent. Typically, we don't have to do anything for this- Specified by:
deliveryComplete
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
- Parameters:
token
-
-
connectComplete
This happens whenever we connect to the broker. This can be a broken connection getting reconnected in which case reconnect is set to true. We will merely re-subscribe to the topics that we have already subscribed to.- Specified by:
connectComplete
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
- Parameters:
reconnect
- if this is a reconnectionserverURI
- the actual broker Server URI. It is possible we have multiple broker servers.
-
authPacketArrived
public void authPacketArrived(int reasonCode, org.eclipse.paho.mqttv5.common.packet.MqttProperties properties) For authentication purposes. Not supported by this client. We merely implement to honor the interface contract.- Specified by:
authPacketArrived
in interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
- Parameters:
reasonCode
- dummyproperties
- dummy
-