Package 

Class MqttToken

  • All Implemented Interfaces:
    org.eclipse.paho.client.mqttv3.IMqttToken , org.eclipse.paho.client.mqttv3.IToken

    
    public class MqttToken
     implements IMqttToken, IToken
                        

    Provides a mechanism for tracking the completion of an asynchronous action.

    A token that implements the ImqttToken interface is returned from all non-blocking method with the exception of publish.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      public Token internalTok
    • Method Summary

      Modifier and Type Method Description
      MqttException getException() Returns an exception providing more detail if an operation failed.
      boolean isComplete() Returns whether or not the action has finished.
      void setActionCallback(IMqttActionListener listener) Register a listener to be notified when an action completes.
      IMqttActionListener getActionCallback() Return the async listener for this token.
      void waitForCompletion() Blocks the current thread until the action this token is associated with has completed.
      void waitForCompletion(long timeout) Blocks the current thread until the action this token is associated with has completed.
      IMqttAsyncClient getClient() Returns the MQTT client that is responsible for processing the asynchronous action
      Array<String> getTopics() Returns the topic string(s) for the action being tracked by this token.
      Object getUserContext() Retrieve the context associated with an action.
      void setUserContext(Object userContext) Store some context associated with an action.
      int getMessageId() Returns the message ID of the message that is associated with the token.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MqttToken

        MqttToken()
      • MqttToken

        MqttToken(String logContext)
    • Method Detail

      • getException

         MqttException getException()

        Returns an exception providing more detail if an operation failed.

        While an action in in progress and when an action completes successfully null will be returned. Certain errors like timeout or shutting down will not set the exception as the action has not failed or completed at that time

      • isComplete

         boolean isComplete()

        Returns whether or not the action has finished.

        True will be returned both in the case where the action finished successfully and in the case where it failed. If the action failed getException will be non null.

      • setActionCallback

         void setActionCallback(IMqttActionListener listener)

        Register a listener to be notified when an action completes.

        Once a listener is registered it will be invoked when the action the token is associated with either succeeds or fails.

        Parameters:
        listener - to be invoked once the action completes
      • waitForCompletion

         void waitForCompletion()

        Blocks the current thread until the action this token is associated with has completed.

      • waitForCompletion

         void waitForCompletion(long timeout)

        Blocks the current thread until the action this token is associated with has completed.

        The timeout specifies the maximum time it will block for. If the action completes before the timeout then control returns immediately, if not it will block until the timeout expires.

        If the action being tracked fails or the timeout expires an exception will be thrown. In the event of a timeout the action may complete after timeout.

        Parameters:
        timeout - the maximum amount of time to wait for, in milliseconds.
      • getTopics

         Array<String> getTopics()

        Returns the topic string(s) for the action being tracked by this token. If the action has not been initiated or the action has not topic associated with it such as connect then null will be returned.

      • getUserContext

         Object getUserContext()

        Retrieve the context associated with an action.

        Allows the ActionListener associated with an action to retrieve any context that was associated with the action when the action was invoked. If not context was provided null is returned.

      • setUserContext

         void setUserContext(Object userContext)

        Store some context associated with an action.

        Allows the caller of an action to store some context that can be accessed from within the ActionListener associated with the action. This can be useful when the same ActionListener is associated with multiple actions

        Parameters:
        userContext - to associate with an action
      • getMessageId

         int getMessageId()

        Returns the message ID of the message that is associated with the token. A message id of zero will be returned for tokens associated with connect, disconnect and ping operations as there can only ever be one of these outstanding at a time. For other operations the MQTT message id flowed over the network.