public interface IMqttToken
When using the asynchronous/non-blocking MQTT programming interface all methods/operations that take any time (and in particular those that involve any network operation) return control to the caller immediately. The operation then proceeds to run in the background so as not to block the invoking thread. An IMqttToken is used to track the state of the operation. An application can use the token to wait for an operation to complete. A token is passed to callbacks once the operation completes and provides context linking it to the original request. A token is associated with a single operation.
An action is in progress until either:
Modifier and Type | Method and Description |
---|---|
IMqttActionListener |
getActionCallback()
Return the async listener for this token.
|
IMqttAsyncClient |
getClient()
Returns the MQTT client that is responsible for processing the asynchronous
action
|
MqttException |
getException()
Returns an exception providing more detail if an operation failed.
|
int[] |
getGrantedQos()
Returns the granted QoS list from a suback
|
int |
getMessageId()
Returns the message ID of the message that is associated with the token.
|
MqttWireMessage |
getResponse()
Returns the response wire message
|
boolean |
getSessionPresent()
Returns the session present flag from a connack
|
java.lang.String[] |
getTopics()
Returns the topic string(s) for the action being tracked by this
token.
|
java.lang.Object |
getUserContext()
Retrieve the context associated with an action.
|
boolean |
isComplete()
Returns whether or not the action has finished.
|
void |
setActionCallback(IMqttActionListener listener)
Register a listener to be notified when an action completes.
|
void |
setUserContext(java.lang.Object userContext)
Store some context associated with an action.
|
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.
|
void waitForCompletion() throws MqttException
MqttException
- if there was a problem with the action associated with the token.waitForCompletion(long)
void waitForCompletion(long timeout) throws MqttException
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.
timeout
- the maximum amount of time to wait for, in milliseconds.MqttException
- if there was a problem with the action associated with the token.boolean isComplete()
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.
MqttException getException()
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
void setActionCallback(IMqttActionListener listener)
Once a listener is registered it will be invoked when the action the token is associated with either succeeds or fails.
listener
- to be invoked once the action completesIMqttActionListener getActionCallback()
IMqttAsyncClient getClient()
java.lang.String[] getTopics()
void setUserContext(java.lang.Object userContext)
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
userContext
- to associate with an actionjava.lang.Object getUserContext()
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.
int getMessageId()
int[] getGrantedQos()
boolean getSessionPresent()
MqttWireMessage getResponse()