Class MqttSubscriber
java.lang.Object
org.chenile.mqtt.pubsub.MqttSubscriber
- All Implemented Interfaces:
ChenileSub, org.eclipse.paho.mqttv5.client.MqttCallback
public class MqttSubscriber
extends Object
implements org.eclipse.paho.mqttv5.client.MqttCallback, ChenileSub
MQTT subscription call back that supports receiving messages, reconnects etc.
A message is routed in one of two ways:
- If its topic is bound to a Chenile event (see
MqttEventBindingProperties), the event is raised throughEventProcessor, which runs every operation subscribed to it. - Otherwise it is a
@ChenilePubSubinvocation: the topic names the service and operation, andPubSubEntryPointinvokes that operation.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) ChenileConfigurationprivate final MqttEventBindingProperties(package private) EventProcessor(package private) org.slf4j.Loggerprivate static final Stringprivate final boolean(package private) MqttPublisher(package private) PubSubEntryPoint(package private) PubSubInfoProvider(package private) org.eclipse.paho.mqttv5.client.MqttAsyncClient -
Constructor Summary
ConstructorsConstructorDescriptionMqttSubscriber(boolean mqttEnabled, MqttEventBindingProperties eventBindingProperties) -
Method Summary
Modifier and TypeMethodDescriptionvoidauthPacketArrived(int reasonCode, org.eclipse.paho.mqttv5.common.packet.MqttProperties properties) For authentication purposes.voidconnectComplete(boolean reconnect, String serverURI) This happens whenever we connect to the broker.voiddeliveryComplete(org.eclipse.paho.mqttv5.client.IMqttToken token) Upon successful delivery of messages that we have sent.voiddisconnected(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse) private StringgetSource(org.eclipse.paho.mqttv5.common.MqttMessage message) getTarget(org.eclipse.paho.mqttv5.common.MqttMessage message) booleangetTestMode(org.eclipse.paho.mqttv5.common.MqttMessage message) private StringgetUserPropertyValue(org.eclipse.paho.mqttv5.common.MqttMessage message, String key) private voidvoidvoidmessageArrived(String topic, org.eclipse.paho.mqttv5.common.MqttMessage message) Called when a new message arrives.voidmqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception) private voidRaises the Chenile event bound to the topic.private booleanshouldIgnore(org.eclipse.paho.mqttv5.common.MqttMessage message) This method performs a few checks to see if this message needs to be ignored.voidValidates the topic to event bindings and subscribes to the bound topics.
-
Field Details
-
MQTT_ENTRY_POINT_SUB_TYPE
- See Also:
-
mqttEnabled
private final boolean mqttEnabled -
pubSubConfig
-
v5Client
-
logger
org.slf4j.Logger logger -
pubSubEntryPoint
-
pubSubInfoProvider
-
publisher
-
eventProcessor
-
chenileConfiguration
-
eventBindingProperties
-
topicToEvent
-
-
Constructor Details
-
MqttSubscriber
-
-
Method Details
-
subscribeToEventTopics
@EventListener(org.springframework.boot.context.event.ApplicationReadyEvent.class) @Order(910) public void subscribeToEventTopics() throws org.eclipse.paho.mqttv5.common.MqttExceptionValidates the topic to event bindings and subscribes to the bound topics. Runs after the Chenile configuration (including event subscriptions) has been initialized.- Throws:
org.eclipse.paho.mqttv5.common.MqttException
-
log
-
disconnected
public void disconnected(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse) - Specified by:
disconnectedin interfaceorg.eclipse.paho.mqttv5.client.MqttCallback
-
mqttErrorOccurred
public void mqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception) - Specified by:
mqttErrorOccurredin 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:
messageArrivedin 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:
deliveryCompletein 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:
connectCompletein 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:
authPacketArrivedin interfaceorg.eclipse.paho.mqttv5.client.MqttCallback- Parameters:
reasonCode- dummyproperties- dummy
-
messageArrived
- Specified by:
messageArrivedin interfaceChenileSub
-
raiseEvent
-
getUserPropertyValue
-
getSource
-
getTarget
-
getTestMode
public boolean getTestMode(org.eclipse.paho.mqttv5.common.MqttMessage message)
-
CHENILE