Interface IMqttClient

  • All Superinterfaces:
    AutoCloseable
    All Known Implementing Classes:
    MqttClient

    public interface IMqttClient
    extends AutoCloseable
    Enables an application to communicate with an MQTT server using blocking methods.

    This interface allows applications to utilize all features of the MQTT version 3.1 specification including:

    • connect
    • publish
    • subscribe
    • unsubscribe
    • disconnect

    IMqttClient provides a set of methods that block and return control to the application program once the MQTT action has completed.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void close()
      Close the client Releases all resource associated with the client.
      void connect()
      Connects to an MQTT server using the default options.
      void connect​(MqttConnectOptions options)
      Connects to an MQTT server using the specified options.
      void disconnect()
      Disconnects from the server.
      String getClientId()
      Returns the client ID used by this client.
      String getServerURI()
      Returns the address of the server used by this client, as a URI.
      boolean isConnected()
      Determines if this client is currently connected to the server.
      void publish​(String topic, byte[] payload, int qos, boolean retained)
      Publishes a message to a topic on the server and return once it is delivered.
      void publish​(String topic, MqttMessage message)
      Publishes a message to a topic on the server.
      void setCallback​(MqttCallback callback)
      Sets the callback listener to use for events that happen asynchronously.
      void subscribe​(String topicFilter)
      Subscribe to a topic, which may include wildcards using a QoS of 1.
      void subscribe​(String topicFilter, int qos)
      Subscribe to a topic, which may include wildcards.
      void unsubscribe​(String topicFilter)
      Requests the server unsubscribe the client from a topic.
    • Method Detail

      • connect

        void connect​(MqttConnectOptions options)
              throws MqttException
        Connects to an MQTT server using the specified options.

        The server to connect to is specified on the constructor. It is recommended to call setCallback(MqttCallback) prior to connecting in order that messages destined for the client can be accepted as soon as the client is connected.

        This is a blocking method that returns once connect completes

        Parameters:
        options - a set of connection parameters that override the defaults.
        Throws:
        MqttException - for non security related problems including communication errors
      • disconnect

        void disconnect()
                 throws MqttException
        Disconnects from the server.

        This is a blocking method that returns once disconnect completes

        Throws:
        MqttException - if a problem is encountered while disconnecting
      • subscribe

        void subscribe​(String topicFilter)
                throws MqttException
        Subscribe to a topic, which may include wildcards using a QoS of 1.
        Parameters:
        topicFilter - the topic to subscribe to, which can include wildcards.
        Throws:
        MqttException - if there was an error registering the subscription.
      • subscribe

        void subscribe​(String topicFilter,
                       int qos)
                throws MqttException
        Subscribe to a topic, which may include wildcards.
        Parameters:
        topicFilter - the topic to subscribe to, which can include wildcards.
        qos - the maximum quality of service at which to subscribe. Messages published at a lower quality of service will be received at the published QoS. Messages published at a higher quality of service will be received using the QoS specified on the subscribe.
        Throws:
        MqttException - if there was an error registering the subscription.
      • unsubscribe

        void unsubscribe​(String topicFilter)
                  throws MqttException
        Requests the server unsubscribe the client from a topic.
        Parameters:
        topicFilter - the topic to unsubscribe from. It must match a topicFilter specified on the subscribe.
        Throws:
        MqttException - if there was an error unregistering the subscription.
      • publish

        void publish​(String topic,
                     byte[] payload,
                     int qos,
                     boolean retained)
              throws MqttException
        Publishes a message to a topic on the server and return once it is delivered.

        This is a convenience method, which will create a new MqttMessage object with a byte array payload and the specified QoS, and then publish it. All other values in the message will be set to the defaults.

        Parameters:
        topic - to deliver the message to, for example "finance/stock/ibm".
        payload - the byte array to use as the payload
        qos - the Quality of Service to deliver the message at. Valid values are 0, 1 or 2.
        retained - whether or not this message should be retained by the server.
        Throws:
        IllegalArgumentException - if value of QoS is not 0, 1 or 2.
        MqttException - for other errors encountered while publishing the message. For instance client not connected.
        See Also:
        publish(String, MqttMessage), MqttMessage.setQos(int), MqttMessage.setRetained(boolean)
      • publish

        void publish​(String topic,
                     MqttMessage message)
              throws MqttException
        Publishes a message to a topic on the server.

        Delivers a message to the server at the requested quality of service and returns control once the message has been delivered.

        This is a blocking method that returns once publish completes

        Parameters:
        topic - to deliver the message to, for example "finance/stock/ibm".
        message - to delivery to the server
        Throws:
        MqttException - for other errors encountered while publishing the message. For instance client not connected.
      • setCallback

        void setCallback​(MqttCallback callback)
        Sets the callback listener to use for events that happen asynchronously.

        There are a number of events that listener will be notified about. These include:

        • A new message has arrived and is ready to be processed
        • The connection to the server has been lost
        • Delivery of a message to the server has completed.
        Parameters:
        callback - the class to callback when for events related to the client
        See Also:
        MqttCallback
      • isConnected

        boolean isConnected()
        Determines if this client is currently connected to the server.
        Returns:
        true if connected, false otherwise.
      • getClientId

        String getClientId()
        Returns the client ID used by this client.

        All clients connected to the same server or server farm must have a unique ID.

        Returns:
        the client ID used by this client.
      • getServerURI

        String getServerURI()
        Returns the address of the server used by this client, as a URI.

        The format is the same as specified on the constructor.

        Returns:
        the server's address, as a URI String.
        See Also:
        MqttClient(String, String)
      • close

        void close()
            throws MqttException
        Close the client Releases all resource associated with the client. After the client has been closed it cannot be reused. For instance attempts to connect will fail.
        Specified by:
        close in interface AutoCloseable
        Throws:
        MqttException - if the client is not disconnected.