Class MqttClient


  • @Component
    public class MqttClient
    extends java.lang.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 Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • lastInstance

        private static MqttClient lastInstance
      • client

        private org.eclipse.paho.mqttv5.client.MqttAsyncClient client
      • qos

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

      • MqttClient

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

      • 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​(java.lang.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​(java.lang.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​(java.lang.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.mqttv5.client.IMqttToken token)
                        throws org.eclipse.paho.mqttv5.common.MqttException
        Waits for completion until the token is processed.
        Parameters:
        token - the token
        Throws:
        org.eclipse.paho.mqttv5.common.MqttException - in case that processing of the token fails