java.lang.Object
de.iip_ecosphere.platform.transport.spring.binder.mqttv3.MqttClient

@Component public class MqttClient extends Object
A MQTT client for a single binder instance. Typically, different binders subscribe to different topics. Partially public for testing. Initial implementation, not optimized.
Author:
Holger Eichelberger, SSE
  • Field Details

    • LOGGER

      private static final de.iip_ecosphere.platform.support.logging.Logger LOGGER
    • lastInstance

      private static MqttClient lastInstance
    • client

      private org.eclipse.paho.client.mqttv3.MqttAsyncClient client
    • configuration

      private MqttConfiguration configuration
    • callback

      private MqttClient.Callback callback
    • qos

      private de.iip_ecosphere.platform.transport.connectors.basics.MqttQoS qos
  • Constructor Details

    • MqttClient

      public MqttClient()
      Creates and registers an instance.
  • Method Details

    • getLastInstance

      public static MqttClient getLastInstance()
      Returns the last instance created for this class. [testing]
      Returns:
      the last instance
    • getConfiguration

      public MqttConfiguration getConfiguration()
      Returns the actual configuration. [for testing]
      Returns:
      the configuration, may be null
    • createClient

      public void createClient(MqttConfiguration config)
      Creates the client based on a given MQTT client configuration.
      Parameters:
      config - the MQTT configuration to take the connection information from
    • stopClient

      public void stopClient()
      Stops the client.
    • subscribeTo

      boolean subscribeTo(String topic, MqttClient.ArrivedCallback arrivedCallback)
      Subscribes to topic if topic is not blacklisted by MqttConfiguration.isFilteredTopic(String).
      Parameters:
      topic - the topic to unsubscribe from
      arrivedCallback - the callback to be called when a message arrived
      Returns:
      true if done/successful, false else
    • unsubscribeFrom

      boolean unsubscribeFrom(String topic)
      Unsubscribes from topic if topic is not blacklisted by MqttConfiguration.isFilteredTopic(String).
      Parameters:
      topic - the topic to unsubscribe from
      Returns:
      true if done/successful, false else
    • send

      void send(String topic, byte[] payload)
      Sends payload to topic.
      Parameters:
      topic - the topic to send to
      payload - the payload to send
    • waitForCompletion

      void waitForCompletion(org.eclipse.paho.client.mqttv3.IMqttToken token) throws org.eclipse.paho.client.mqttv3.MqttException
      Waits for completion until the token is processed.
      Parameters:
      token - the token
      Throws:
      org.eclipse.paho.client.mqttv3.MqttException - in case that processing of the token fails