Class MqttSubscriber

java.lang.Object
org.chenile.mqtt.pubsub.MqttSubscriber
All Implemented Interfaces:
org.eclipse.paho.mqttv5.client.MqttCallback

public class MqttSubscriber extends Object implements org.eclipse.paho.mqttv5.client.MqttCallback
MQTT subscription call back that supports receiving messages, reconnects etc.
  • Field Details

  • Constructor Details

    • MqttSubscriber

      public MqttSubscriber(boolean mqttEnabled)
  • Method Details

    • log

      private void log(String message)
    • disconnected

      public void disconnected(org.eclipse.paho.mqttv5.client.MqttDisconnectResponse disconnectResponse)
      Specified by:
      disconnected in interface org.eclipse.paho.mqttv5.client.MqttCallback
    • mqttErrorOccurred

      public void mqttErrorOccurred(org.eclipse.paho.mqttv5.common.MqttException exception)
      Specified by:
      mqttErrorOccurred in interface org.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 interface org.eclipse.paho.mqttv5.client.MqttCallback
      Parameters:
      topic - the topic at which we received this message
      message - the message that is received
      Throws:
      Exception - if we have an error in processing the message
    • shouldIgnore

      private boolean shouldIgnore(org.eclipse.paho.mqttv5.common.MqttMessage message) throws Exception
      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 interface org.eclipse.paho.mqttv5.client.MqttCallback
      Parameters:
      token -
    • connectComplete

      public void connectComplete(boolean reconnect, String serverURI)
      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 interface org.eclipse.paho.mqttv5.client.MqttCallback
      Parameters:
      reconnect - if this is a reconnection
      serverURI - 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 interface org.eclipse.paho.mqttv5.client.MqttCallback
      Parameters:
      reasonCode - dummy
      properties - dummy