org.eclipse.paho.client.mqttv3
Interface IMqttAsyncClient

All Known Implementing Classes:
MqttAsyncClient

public interface IMqttAsyncClient

Enables an application to communicate with an MQTT server using non-blocking methods.

It provides applications a simple programming interface to all features of the MQTT version 3.1 specification including:

There are two styles of MQTT client, this one and IMqttClient.

An application is not restricted to using one style if an IMqttAsyncClient based client is used as both blocking and non-blocking methods can be used in the same application. If an IMqttClient based client is used then only blocking methods are available to the application. For more details on the blocking client see IMqttClient

There are two forms of non-blocking method:

  1.      IMqttToken token = asyncClient.method(parms)
         

    In this form the method returns a token that can be used to track the progress of the action (method). The method provides a waitForCompletion() method that once invoked will block until the action completes. Once completed there are method on the token that can be used to check if the action completed successfully or not. For example to wait until a connect completes:

          IMqttToken conToken;
            conToken = asyncClient.client.connect(conToken);
         ... do some work...
            conToken.waitForCompletion();
         

    To turn a method into a blocking invocation the following form can be used:

         IMqttToken token;
         token = asyncClient.method(parms).waitForCompletion();
         
  2.      IMqttToken token method(parms, Object userContext, IMqttActionListener callback)
         

    In this form a callback is registered with the method. The callback will be notified when the action succeeds or fails. The callback is invoked on the thread managed by the MQTT client so it is important that processing is minimised in the callback. If not the operation of the MQTT client will be inhibited. For example to be notified (called back) when a connect completes:

            IMqttToken conToken;
                conToken = asyncClient.connect("some context",new new MqttAsyncActionListener() {
                            public void onSuccess(IMqttToken asyncActionToken) {
                                    log("Connected");
                            }
    
                            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                                    log ("connect failed" +exception);
                            }
                      });
          
    An optional context object can be passed into the method which will then be made available in the callback. The context is stored by the MQTT client) in the token which is then returned to the invoker. The token is provided to the callback methods where the context can then be accessed.

To understand when the delivery of a message is complete either of the two methods above can be used to either wait on or be notified when the publish completes. An alternative is to use the MqttCallback.deliveryComplete(IMqttDeliveryToken) method which will also be notified when a message has been delivered to the requested quality of service.


Method Summary
 void close()
          Close the client Releases all resource associated with the client.
 IMqttToken connect()
          Connects to an MQTT server using the default options.
 IMqttToken connect(MqttConnectOptions options)
          Connects to an MQTT server using the provided connect options.
 IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttActionListener callback)
          Connects to an MQTT server using the specified options.
 IMqttToken connect(Object userContext, IMqttActionListener callback)
          Connects to an MQTT server using the default options.
 IMqttToken disconnect()
          Disconnects from the server.
 IMqttToken disconnect(long quiesceTimeout)
          Disconnects from the server.
 IMqttToken disconnect(long quiesceTimeout, Object userContext, IMqttActionListener callback)
          Disconnects from the server.
 IMqttToken disconnect(Object userContext, IMqttActionListener callback)
          Disconnects from the server.
 String getClientId()
          Returns the client ID used by this client.
 IMqttDeliveryToken[] getPendingDeliveryTokens()
          Returns the delivery tokens for any outstanding publish operations.
 String getServerURI()
          Returns the address of the server used by this client.
 boolean isConnected()
          Determines if this client is currently connected to the server.
 IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained)
          Publishes a message to a topic on the server.
 IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained, Object userContext, IMqttActionListener callback)
          Publishes a message to a topic on the server.
 IMqttDeliveryToken publish(String topic, MqttMessage message)
          Publishes a message to a topic on the server.
 IMqttDeliveryToken publish(String topic, MqttMessage message, Object userContext, IMqttActionListener callback)
          Publishes a message to a topic on the server.
 void setCallback(MqttCallback callback)
          Sets a callback listener to use for events that happen asynchronously.
 IMqttToken subscribe(String[] topicFilters, int[] qos)
          Subscribe to multiple topics, each of which may include wildcards.
 IMqttToken subscribe(String[] topicFilters, int[] qos, Object userContext, IMqttActionListener callback)
          Subscribes to multiple topics, each of which may include wildcards.
 IMqttToken subscribe(String topicFilter, int qos)
          Subscribe to a topic, which may include wildcards.
 IMqttToken subscribe(String topicFilter, int qos, Object userContext, IMqttActionListener callback)
          Subscribe to a topic, which may include wildcards.
 IMqttToken unsubscribe(String topicFilter)
          Requests the server unsubscribe the client from a topic.
 IMqttToken unsubscribe(String[] topicFilters)
          Requests the server unsubscribe the client from one or more topics.
 IMqttToken unsubscribe(String[] topicFilters, Object userContext, IMqttActionListener callback)
          Requests the server unsubscribe the client from one or more topics.
 IMqttToken unsubscribe(String topicFilter, Object userContext, IMqttActionListener callback)
          Requests the server unsubscribe the client from a topics.
 

Method Detail

connect

IMqttToken connect()
                   throws MqttException,
                          MqttSecurityException
Connects to an MQTT server using the default options.

The default options are specified in MqttConnectOptions class.

Returns:
token used to track and wait for the connect to complete. The token will be passed to the callback methods if a callback is set.
Throws:
MqttSecurityException - for security related problems
MqttException - for non security related problems
See Also:
connect(MqttConnectOptions, Object, IMqttActionListener)

connect

IMqttToken connect(MqttConnectOptions options)
                   throws MqttException,
                          MqttSecurityException
Connects to an MQTT server using the provided connect options.

The connection will be established using the options specified in the MqttConnectOptions parameter.

Parameters:
options - a set of connection parameters that override the defaults.
Returns:
token used to track and wait for the connect to complete. The token will be passed to any callback that has been set.
Throws:
MqttSecurityException - for security related problems
MqttException - for non security related problems
See Also:
connect(MqttConnectOptions, Object, IMqttActionListener)

connect

IMqttToken connect(Object userContext,
                   IMqttActionListener callback)
                   throws MqttException,
                          MqttSecurityException
Connects to an MQTT server using the default options.

The default options are specified in MqttConnectOptions class.

Parameters:
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when the connect completes. Use null if not required.
Returns:
token used to track and wait for the connect to complete. The token will be passed to any callback that has been set.
Throws:
MqttSecurityException - for security related problems
MqttException - for non security related problems
See Also:
connect(MqttConnectOptions, Object, IMqttActionListener)

connect

IMqttToken connect(MqttConnectOptions options,
                   Object userContext,
                   IMqttActionListener callback)
                   throws MqttException,
                          MqttSecurityException
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.

The method returns control before the connect completes. Completion can be tracked by:

Parameters:
options - a set of connection parameters that override the defaults.
userContext - optional object for used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when the connect completes. Use null if not required.
Returns:
token used to track and wait for the connect to complete. The token will be passed to any callback that has been set.
Throws:
MqttSecurityException - for security related problems
MqttException - for non security related problems including communication errors

disconnect

IMqttToken disconnect()
                      throws MqttException
Disconnects from the server.

An attempt is made to quiesce the client allowing outstanding work to complete before disconnecting. It will wait for a maximum of 30 seconds for work to quiesce before disconnecting. This method must not be called from inside MqttCallback methods.

Returns:
token used to track and wait for disconnect to complete. The token will be passed to any callback that has been set.
Throws:
MqttException - for problems encountered while disconnecting
See Also:
disconnect(long, Object, IMqttActionListener)

disconnect

IMqttToken disconnect(long quiesceTimeout)
                      throws MqttException
Disconnects from the server.

An attempt is made to quiesce the client allowing outstanding work to complete before disconnecting. It will wait for a maximum of the specified quiesce time for work to complete before disconnecting. This method must not be called from inside MqttCallback methods.

Parameters:
quiesceTimeout - the amount of time in milliseconds to allow for existing work to finish before disconnecting. A value of zero or less means the client will not quiesce.
Returns:
token used to track and wait for disconnect to complete. The token will be passed to the callback methods if a callback is set.
Throws:
MqttException - for problems encountered while disconnecting
See Also:
disconnect(long, Object, IMqttActionListener)

disconnect

IMqttToken disconnect(Object userContext,
                      IMqttActionListener callback)
                      throws MqttException
Disconnects from the server.

An attempt is made to quiesce the client allowing outstanding work to complete before disconnecting. It will wait for a maximum of 30 seconds for work to quiesce before disconnecting. This method must not be called from inside MqttCallback methods.

Parameters:
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when the disconnect completes. Use null if not required.
Returns:
token used to track and wait for the disconnect to complete. The token will be passed to any callback that has been set.
Throws:
MqttException - for problems encountered while disconnecting
See Also:
disconnect(long, Object, IMqttActionListener)

disconnect

IMqttToken disconnect(long quiesceTimeout,
                      Object userContext,
                      IMqttActionListener callback)
                      throws MqttException
Disconnects from the server.

The client will wait for MqttCallback methods to complete. It will then wait for up to the quiesce timeout to allow for work which has already been initiated to complete. For instance when a QoS 2 message has started flowing to the server but the QoS 2 flow has not completed.It prevents new messages being accepted and does not send any messages that have been accepted but not yet started delivery across the network to the server. When work has completed or after the quiesce timeout, the client will disconnect from the server. If the cleanSession flag was set to false and is set to false the next time a connection is made QoS 1 and 2 messages that were not previously delivered will be delivered.

This method must not be called from inside MqttCallback methods.

The method returns control before the disconnect completes. Completion can be tracked by:

Parameters:
quiesceTimeout - the amount of time in milliseconds to allow for existing work to finish before disconnecting. A value of zero or less means the client will not quiesce.
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when the disconnect completes. Use null if not required.
Returns:
token used to track and wait for the connect to complete. The token will be passed to any callback that has been set.
Throws:
MqttException - for problems encountered while disconnecting

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.

The format of the returned String is the same as that used on the constructor.

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

publish

IMqttDeliveryToken publish(String topic,
                           byte[] payload,
                           int qos,
                           boolean retained)
                           throws MqttException,
                                  MqttPersistenceException
Publishes a message to a topic on the server.

A convenience method, which will create a new MqttMessage object with a byte array payload and the specified QoS, and then publish it.

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.
Returns:
token used to track and wait for the publish to complete. The token will be passed to any callback that has been set.
Throws:
MqttPersistenceException - when a problem occurs storing the message
IllegalArgumentException - if value of QoS is not 0, 1 or 2.
MqttException - for other errors encountered while publishing the message. For instance if too many messages are being processed.
See Also:
publish(String, MqttMessage, Object, IMqttActionListener), MqttMessage.setQos(int), MqttMessage.setRetained(boolean)

publish

IMqttDeliveryToken publish(String topic,
                           byte[] payload,
                           int qos,
                           boolean retained,
                           Object userContext,
                           IMqttActionListener callback)
                           throws MqttException,
                                  MqttPersistenceException
Publishes a message to a topic on the server.

A convenience method, which will create a new MqttMessage object with a byte array payload and the specified QoS, and then publish it.

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.
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when message delivery hsa completed to the requested quality of service
Returns:
token used to track and wait for the publish to complete. The token will be passed to any callback that has been set.
Throws:
MqttPersistenceException - when a problem occurs storing the message
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, Object, IMqttActionListener), MqttMessage.setQos(int), MqttMessage.setRetained(boolean)

publish

IMqttDeliveryToken publish(String topic,
                           MqttMessage message)
                           throws MqttException,
                                  MqttPersistenceException
Publishes a message to a topic on the server. Takes an MqttMessage message and delivers it to the server at the requested quality of service.

Parameters:
topic - to deliver the message to, for example "finance/stock/ibm".
message - to deliver to the server
Returns:
token used to track and wait for the publish to complete. The token will be passed to any callback that has been set.
Throws:
MqttPersistenceException - when a problem occurs storing the message
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, Object, IMqttActionListener)

publish

IMqttDeliveryToken publish(String topic,
                           MqttMessage message,
                           Object userContext,
                           IMqttActionListener callback)
                           throws MqttException,
                                  MqttPersistenceException
Publishes a message to a topic on the server.

Once this method has returned cleanly, the message has been accepted for publication by the client and will be delivered on a background thread. In the event the connection fails or the client stops. Messages will be delivered to the requested quality of service once the connection is re-established to the server on condition that:

When building an application, the design of the topic tree should take into account the following principles of topic name syntax and semantics:

The following principles apply to the construction and content of a topic tree:

The method returns control before the publish completes. Completion can be tracked by:

Parameters:
topic - to deliver the message to, for example "finance/stock/ibm".
message - to deliver to the server
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when message delivery has completed to the requested quality of service
Returns:
token used to track and wait for the publish to complete. The token will be passed to callback methods if set.
Throws:
MqttPersistenceException - when a problem occurs storing the message
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:
MqttMessage

subscribe

IMqttToken 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.
Returns:
token used to track and wait for the subscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error registering the subscription.
See Also:
subscribe(String[], int[], Object, IMqttActionListener)

subscribe

IMqttToken subscribe(String topicFilter,
                     int qos,
                     Object userContext,
                     IMqttActionListener callback)
                     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.
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when subscribe has completed
Returns:
token used to track and wait for the subscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error registering the subscription.
See Also:
subscribe(String[], int[], Object, IMqttActionListener)

subscribe

IMqttToken subscribe(String[] topicFilters,
                     int[] qos)
                     throws MqttException
Subscribe to multiple topics, each of which may include wildcards.

Provides an optimized way to subscribe to multiple topics compared to subscribing to each one individually.

Parameters:
topicFilters - one or more topics 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.
Returns:
token used to track and wait for the subscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error registering the subscription.
See Also:
subscribe(String[], int[], Object, IMqttActionListener)

subscribe

IMqttToken subscribe(String[] topicFilters,
                     int[] qos,
                     Object userContext,
                     IMqttActionListener callback)
                     throws MqttException
Subscribes to multiple topics, each of which may include wildcards.

Provides an optimized way to subscribe to multiple topics compared to subscribing to each one individually.

The setCallback(MqttCallback) method should be called before this method, otherwise any received messages will be discarded.

If (@link MqttConnectOptions#setCleanSession(boolean)} was set to true when when connecting to the server then the subscription remains in place until either:


unsubscribe

IMqttToken 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 an earlier subscribe.
Returns:
token used to track and wait for the unsubscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error unregistering the subscription.
See Also:
unsubscribe(String[], Object, IMqttActionListener)

unsubscribe

IMqttToken unsubscribe(String[] topicFilters)
                       throws MqttException
Requests the server unsubscribe the client from one or more topics.

Parameters:
topicFilters - one or more topics to unsubscribe from. Each topicFilter must match one specified on an earlier subscribe. *
Returns:
token used to track and wait for the unsubscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error unregistering the subscription.
See Also:
unsubscribe(String[], Object, IMqttActionListener)

unsubscribe

IMqttToken unsubscribe(String topicFilter,
                       Object userContext,
                       IMqttActionListener callback)
                       throws MqttException
Requests the server unsubscribe the client from a topics.

Parameters:
topicFilter - the topic to unsubscribe from. It must match a topicFilter specified on an earlier subscribe.
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when unsubscribe has completed
Returns:
token used to track and wait for the unsubscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error unregistering the subscription.
See Also:
unsubscribe(String[], Object, IMqttActionListener)

unsubscribe

IMqttToken unsubscribe(String[] topicFilters,
                       Object userContext,
                       IMqttActionListener callback)
                       throws MqttException
Requests the server unsubscribe the client from one or more topics.

Unsubcribing is the opposite of subscribing. When the server receives the unsubscribe request it looks to see if it can find a matching subscription for the client and then removes it. After this point the server will send no more messages to the client for this subscription.

The topic(s) specified on the unsubscribe must match the topic(s) specified in the original subscribe request for the unsubscribe to succeed

The method returns control before the unsubscribe completes. Completion can be tracked by:

Parameters:
topicFilters - one or more topics to unsubscribe from. Each topicFilter must match one specified on an earlier subscribe.
userContext - optional object used to pass context to the callback. Use null if not required.
callback - optional listener that will be notified when unsubscribe has completed
Returns:
token used to track and wait for the unsubscribe to complete. The token will be passed to callback methods if set.
Throws:
MqttException - if there was an error unregistering the subscription.

setCallback

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

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

Other events that track the progress of an individual operation such as connect and subscribe can be tracked using the MqttToken returned from each non-blocking method or using setting a IMqttActionListener on the non-blocking method.

Parameters:
callback - which will be invoked for certain asynchronous events
See Also:
MqttCallback

getPendingDeliveryTokens

IMqttDeliveryToken[] getPendingDeliveryTokens()
Returns the delivery tokens for any outstanding publish operations.

If a client has been restarted and there are messages that were in the process of being delivered when the client stopped this method returns a token for each in-flight message enabling the delivery to be tracked Alternately the MqttCallback.deliveryComplete(IMqttDeliveryToken) callback can be used to track the delivery of outstanding messages.

If a client connects with cleanSession true then there will be no delivery tokens as the cleanSession option deletes all earlier state. For state to be remembered the client must connect with cleanSession set to false

Returns:
zero or more delivery tokens

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.

Throws:
MqttException - if the client is not disconnected.


Copyright © 2013. All Rights Reserved.