public class AWSIotMqttClient extends AbstractAwsIotClient
AWSIotDevice
, one can
easily access AWS IoT device shadows in the cloud, and keep them in sync with
the real devices.
There are two types of connections this SDK supports to connect to the AWS IoT service:
For MQTT over TLS, a KeyStore
containing a valid device certificate
and private key is required for instantiating the client. Password for
decrypting the private key in the KeyStore must also be provided.
For MQTT over WebSocket, AWS Signature Version 4 (SigV4) protocol is used for device authentication. For that, a valid AWS IAM access Id and access key pair is required for instantiating the client.
In both cases, AWS IoT IAM policies must be configured properly before the connection can be established with the AWS IoT Gateway. For more information about AWS IoT service, please refer to the AWS IoT developer guide.
To use the client directly, a typical flow would be like the below, and since methods in this class are thread-safe, publish and subscribe can be called from different threads.
AWSIotMqttClient client = new AWSIotMqttClient(...);
client.connect();
...
client.subscribe(topic, ...)
...
client.publish(message, ...)
When using this client in conjunction with AWSIotDevice
, one can
implement a device that is always synchronized with its AWS IoT shadow by
just providing getter and setter methods for the device attributes. The
library does all the heavy lifting by collecting device attributes using the
getter methods provided and reporting to the shadow periodically. It also
subscribes to device changes and updates the device by calling provided
setter methods whenever a change is received. All of these are handled by the
library with no extra code required from the user. AWSIotDevice
also
provides methods for accessing device shadows directly. Please refer to
AWSIotDevice
for more details. A typical flow would be like below.
AWSIotMqttClient client = new AWSIotMqttClient(...);
SomeDevice someDevice = new SomeDevice(thingName); // SomeDevice extends AWSIotDevice
client.attach(someDevice);
client.connect();
The library contains sample applications that demonstrate different ways of using this client library.
baseRetryDelay, cleanSession, clientEnableMetrics, clientEndpoint, clientId, connectionTimeout, connectionType, keepAliveInterval, maxConnectionRetries, maxOfflineQueueSize, maxRetryDelay, numOfClientThreads, port, serverAckTimeout, willMessage
Constructor and Description |
---|
AWSIotMqttClient(String clientEndpoint,
String clientId,
KeyStore keyStore,
String keyPassword)
Instantiates a new client using TLS 1.2 mutual authentication.
|
AWSIotMqttClient(String clientEndpoint,
String clientId,
SSLSocketFactory socketFactory)
Instantiates a new client using TLS 1.2 mutual authentication.
|
AWSIotMqttClient(String clientEndpoint,
String clientId,
SSLSocketFactory socketFactory,
int port)
Instantiates a new client using TLS 1.2 mutual authentication.
|
AWSIotMqttClient(String clientEndpoint,
String clientId,
String awsAccessKeyId,
String awsSecretAccessKey)
Instantiates a new client using Secure WebSocket and AWS SigV4
authentication.
|
AWSIotMqttClient(String clientEndpoint,
String clientId,
String awsAccessKeyId,
String awsSecretAccessKey,
String sessionToken)
Instantiates a new client using Secure WebSocket and AWS SigV4
authentication.
|
AWSIotMqttClient(String clientEndpoint,
String clientId,
String awsAccessKeyId,
String awsSecretAccessKey,
String sessionToken,
String region)
Instantiates a new client using Secure WebSocket and AWS SigV4
authentication.
|
Modifier and Type | Method and Description |
---|---|
void |
attach(AWSIotDevice device)
Attach a shadow device to the client.
|
void |
connect()
Connect the client to the server.
|
void |
connect(long timeout)
Connect the client to the server.
|
void |
connect(long timeout,
boolean blocking)
Connect the client to the server.
|
void |
detach(AWSIotDevice device)
Detach the given device from the client.
|
void |
disconnect()
Disconnect the client from the server.
|
void |
disconnect(long timeout)
Disconnect the client from the server.
|
void |
disconnect(long timeout,
boolean blocking)
Disconnect the client from the server.
|
int |
getBaseRetryDelay()
Gets the base retry delay in milliseconds currently configured.
|
AWSIotConnectionStatus |
getConnectionStatus()
Gets the connection status of the connection used by the client.
|
int |
getConnectionTimeout()
Gets the connection timeout in milliseconds currently configured.
|
int |
getKeepAliveInterval()
Gets the keep-alive interval for the MQTT connection in milliseconds
currently configured.
|
int |
getMaxConnectionRetries()
Gets the maximum number of connection retries currently configured.
|
int |
getMaxOfflineQueueSize()
Gets the maximum offline queue size current configured.
|
int |
getMaxRetryDelay()
Gets the maximum retry delay in milliseconds currently configured.
|
int |
getNumOfClientThreads()
Gets the number of client threads currently configured.
|
int |
getServerAckTimeout()
Gets the server acknowledge timeout in milliseconds currently configured.
|
AWSIotMessage |
getWillMessage()
Gets the Last Will and Testament message currently configured.
|
boolean |
isCleanSession()
Gets whether each connection is a clean session.
|
void |
onConnectionClosed()
This callback function is called when the connection used by the client
is permanently closed.
|
void |
onConnectionFailure()
This callback function is called when the connection used by the client
is temporarily lost.
|
void |
onConnectionSuccess()
This callback function is called when the connection used by the client
is successfully established.
|
void |
publish(AWSIotMessage message)
Publishes the payload to a given topic.
|
void |
publish(AWSIotMessage message,
long timeout)
Publishes the payload to a given topic.
|
void |
publish(String topic,
AWSIotQos qos,
byte[] payload)
Publishes the raw payload to a given topic.
|
void |
publish(String topic,
AWSIotQos qos,
byte[] payload,
long timeout)
Publishes the raw payload to a given topic.
|
void |
publish(String topic,
AWSIotQos qos,
String payload)
Publishes the payload to a given topic.
|
void |
publish(String topic,
AWSIotQos qos,
String payload,
long timeout)
Publishes the payload to a given topic.
|
void |
publish(String topic,
byte[] payload)
Publishes the raw payload to a given topic.
|
void |
publish(String topic,
byte[] payload,
long timeout)
Publishes the raw payload to a given topic.
|
void |
publish(String topic,
String payload)
Publishes the payload to a given topic.
|
void |
publish(String topic,
String payload,
long timeout)
Publishes the payload to a given topic.
|
void |
setBaseRetryDelay(int baseRetryDelay)
Sets a new value in milliseconds for the base retry delay.
|
void |
setCleanSession(boolean cleanSession)
Sets whether the client and server should establish a clean session on each connection.
|
void |
setConnectionTimeout(int connectionTimeout)
Sets a new value in milliseconds for the connection timeout.
|
void |
setKeepAliveInterval(int keepAliveInterval)
Sets a new value in milliseconds for the connection keep-alive interval.
|
void |
setMaxConnectionRetries(int maxConnectionRetries)
Sets a new value for the maximum connection retries.
|
void |
setMaxOfflineQueueSize(int maxOfflineQueueSize)
Sets a new value for the maximum offline queue size.
|
void |
setMaxRetryDelay(int maxRetryDelay)
Sets a new value in milliseconds for the maximum retry delay.
|
void |
setNumOfClientThreads(int numOfClientThreads)
Sets a new value for the number of client threads.
|
void |
setServerAckTimeout(int serverAckTimeout)
Sets a new value in milliseconds for the default server acknowledge
timeout.
|
void |
setWillMessage(AWSIotMessage willMessage)
Sets a new Last Will and Testament message.
|
void |
subscribe(AWSIotTopic topic)
Subscribes to a given topic.
|
void |
subscribe(AWSIotTopic topic,
boolean blocking)
Subscribes to a given topic.
|
void |
subscribe(AWSIotTopic topic,
long timeout)
Subscribes to a given topic.
|
void |
subscribe(AWSIotTopic topic,
long timeout,
boolean blocking)
Subscribes to a given topic.
|
void |
unsubscribe(AWSIotTopic topic)
Unsubscribes to a given topic.
|
void |
unsubscribe(AWSIotTopic topic,
long timeout)
Unsubscribes to a given topic.
|
void |
unsubscribe(String topic)
Unsubscribes to a given topic.
|
void |
unsubscribe(String topic,
long timeout)
Unsubscribes to a given topic.
|
void |
updateCredentials(String awsAccessKeyId,
String awsSecretAccessKey,
String sessionToken)
Updates credentials used for signing Secure WebSocket URLs.
|
dispatch, getClientEndpoint, getClientId, getConnection, getConnectionType, getDevices, getExecutionService, getPort, getSubscriptions, isClientEnableMetrics, scheduleRoutineTask, scheduleTask, scheduleTimeoutTask, setExecutionService, setPort, topicFilterMatch
public AWSIotMqttClient(String clientEndpoint, String clientId, KeyStore keyStore, String keyPassword)
KeyStore
argument. The key password protecting the private key in the
KeyStore
is also required.clientEndpoint
- the client endpoint in the form of <account-specific
prefix>.iot.<aws-region>.amazonaws.com
. The account-specific
prefix can be found on the AWS IoT console or by using the
describe-endpoint
command through the AWS command line
interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.keyStore
- the key store containing the client X.509 certificate and
private key. The KeyStore
object can be constructed
using X.509 certificate file and private key file created on
the AWS IoT console. For more details, please refer to the
README file of this SDK.keyPassword
- the key password protecting the private key in the
keyStore
argument.public AWSIotMqttClient(String clientEndpoint, String clientId, SSLSocketFactory socketFactory)
clientEndpoint
- the client endpoint in the form of <account-specific
prefix>.iot.<aws-region>.amazonaws.com
. The account-specific
prefix can be found on the AWS IoT console or by using the
describe-endpoint
command through the AWS command line
interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.socketFactory
- A socketFactory instantiated with a Keystore containing the client X.509
certificate and private key, and a Truststore containing trusted
Certificate Authorities(CAs).public AWSIotMqttClient(String clientEndpoint, String clientId, SSLSocketFactory socketFactory, int port)
clientEndpoint
- the client endpoint in the form of <account-specific
prefix>.iot.<aws-region>.amazonaws.com
. The account-specific
prefix can be found on the AWS IoT console or by using the
describe-endpoint
command through the AWS command line
interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.socketFactory
- A socketFactory instantiated with a Keystore containing the client X.509
certificate and private key, and a Truststore containing trusted
Certificate Authorities(CAs).port
- The socket port to use.public AWSIotMqttClient(String clientEndpoint, String clientId, String awsAccessKeyId, String awsSecretAccessKey)
clientEndpoint
- the client endpoint in the form of
<account-specific-prefix>.iot.<region>.amazonaws.com
. The account-specific prefix can be found on the AWS IoT
console or by using the describe-endpoint
command
through the AWS command line interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.awsAccessKeyId
- the AWS access key idawsSecretAccessKey
- the AWS secret access keypublic AWSIotMqttClient(String clientEndpoint, String clientId, String awsAccessKeyId, String awsSecretAccessKey, String sessionToken)
clientEndpoint
- the client endpoint in the form of
<account-specific-prefix>.iot.<region>.amazonaws.com
. The account-specific prefix can be found on the AWS IoT
console or by using the describe-endpoint
command
through the AWS command line interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.awsAccessKeyId
- the AWS access key idawsSecretAccessKey
- the AWS secret access keysessionToken
- Session token received along with the temporary credentials
from services like STS server, AssumeRole, or Amazon Cognito.public AWSIotMqttClient(String clientEndpoint, String clientId, String awsAccessKeyId, String awsSecretAccessKey, String sessionToken, String region)
clientEndpoint
- the client endpoint in the form of
<account-specific-prefix>.iot.<region>.amazonaws.com
. The account-specific prefix can be found on the AWS IoT
console or by using the describe-endpoint
command
through the AWS command line interface.clientId
- the client ID uniquely identify a MQTT connection. Two clients
with the same client ID are not allowed to be connected
concurrently to a same endpoint.awsAccessKeyId
- the AWS access key idawsSecretAccessKey
- the AWS secret access keysessionToken
- Session token received along with the temporary credentials
from services like STS server, AssumeRole, or Amazon Cognito.region
- the AWS regionpublic void updateCredentials(String awsAccessKeyId, String awsSecretAccessKey, String sessionToken)
updateCredentials
in class AbstractAwsIotClient
awsAccessKeyId
- the AWS access key idawsSecretAccessKey
- the AWS secret access keysessionToken
- Session token received along with the temporary credentials
from services like STS server, AssumeRole, or Amazon Cognito.public int getNumOfClientThreads()
getNumOfClientThreads
in class AbstractAwsIotClient
public void setNumOfClientThreads(int numOfClientThreads)
connect()
is called.setNumOfClientThreads
in class AbstractAwsIotClient
numOfClientThreads
- the new number of client threads. The default value is 1.public int getConnectionTimeout()
getConnectionTimeout
in class AbstractAwsIotClient
public void setConnectionTimeout(int connectionTimeout)
connect()
is called.setConnectionTimeout
in class AbstractAwsIotClient
connectionTimeout
- the new connection timeout. The default value is 30,000ms.public int getMaxConnectionRetries()
disconnect()
will not be retried. By default, it's 5 times.
Setting it to 0 will disable the connection retry function.getMaxConnectionRetries
in class AbstractAwsIotClient
public void setMaxConnectionRetries(int maxConnectionRetries)
connect()
is called. Setting it to 0 will disable the
connection retry function.setMaxConnectionRetries
in class AbstractAwsIotClient
maxConnectionRetries
- the new max connection retries. The default value is 5.public int getBaseRetryDelay()
delay = min(baseRetryDelay * pow(2, numRetries), maxRetryDelay)
.
By default, the base retry delay is 3,000ms.getBaseRetryDelay
in class AbstractAwsIotClient
public void setBaseRetryDelay(int baseRetryDelay)
connect()
is called.setBaseRetryDelay
in class AbstractAwsIotClient
baseRetryDelay
- the new base retry delay. The default value is 3,000ms.public int getMaxRetryDelay()
delay = min(baseRetryDelay * pow(2, numRetries), maxRetryDelay)
.
By default, the maximum retry delay is 30,000ms.getMaxRetryDelay
in class AbstractAwsIotClient
public void setMaxRetryDelay(int maxRetryDelay)
connect()
is called.setMaxRetryDelay
in class AbstractAwsIotClient
maxRetryDelay
- the new max retry delay. The default value is 30,000ms.public int getServerAckTimeout()
getServerAckTimeout
in class AbstractAwsIotClient
public void setServerAckTimeout(int serverAckTimeout)
connect()
is called.setServerAckTimeout
in class AbstractAwsIotClient
serverAckTimeout
- the new server acknowledge timeout. The default value is
3,000ms.public int getKeepAliveInterval()
getKeepAliveInterval
in class AbstractAwsIotClient
public void setKeepAliveInterval(int keepAliveInterval)
connect()
is called. Setting this
value to 0 will disable the keep-alive function.setKeepAliveInterval
in class AbstractAwsIotClient
keepAliveInterval
- the new keep alive interval. The default value is 30,000ms.public int getMaxOfflineQueueSize()
getMaxOfflineQueueSize
in class AbstractAwsIotClient
public void setMaxOfflineQueueSize(int maxOfflineQueueSize)
connect()
is called. Setting it to 0 will disable the
offline queues.setMaxOfflineQueueSize
in class AbstractAwsIotClient
maxOfflineQueueSize
- the new maximum offline queue size. The default value is 64.public AWSIotMessage getWillMessage()
disconnect()
.getWillMessage
in class AbstractAwsIotClient
public void setCleanSession(boolean cleanSession)
connect()
is called.setCleanSession
in class AbstractAwsIotClient
cleanSession
- If true, the server starts a clean session with the client on each connection.
If false, the server should persist the client's state between connections.public boolean isCleanSession()
setCleanSession(boolean)
.isCleanSession
in class AbstractAwsIotClient
public void setWillMessage(AWSIotMessage willMessage)
connect()
is called. By default, Last Will and Testament
message is not sent.setWillMessage
in class AbstractAwsIotClient
willMessage
- the new Last Will and Testament message message. The default
value is null
.public void connect() throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
connect
in class AbstractAwsIotClient
AWSIotException
- exception thrown if the connection operation failspublic void connect(long timeout) throws AWSIotException, AWSIotTimeoutException
connect
in class AbstractAwsIotClient
timeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void connect(long timeout, boolean blocking) throws AWSIotException, AWSIotTimeoutException
blocking
argument. For blocking
calls, the calling thread is blocked until the operation completed,
failed, or timed out; for non-blocking calls, the calling thread will not
be blocked while the connection is being established.connect
in class AbstractAwsIotClient
timeout
- the timeout in milliseconds that the calling thread will waitblocking
- whether the call should be blocking or non-blockingAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void disconnect() throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
disconnect
in class AbstractAwsIotClient
AWSIotException
- exception thrown if the operation failspublic void disconnect(long timeout) throws AWSIotException, AWSIotTimeoutException
disconnect
in class AbstractAwsIotClient
timeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void disconnect(long timeout, boolean blocking) throws AWSIotException, AWSIotTimeoutException
blocking
argument. For blocking
calls, the calling thread is blocked until the operation completed,
failed, or timed out; for non-blocking calls, the calling thread will not
be blocked while the connection is being terminated.disconnect
in class AbstractAwsIotClient
timeout
- the timeout in milliseconds that the calling thread will waitblocking
- whether the call should be blocking or non-blockingAWSIotException
- exception thrown if the operation failsAWSIotTimeoutException
- exception thrown if the operation times outpublic void publish(String topic, String payload) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
publish
in class AbstractAwsIotClient
topic
- the topic to be published topayload
- the payload to be publishedAWSIotException
- exception thrown if the publish operation failspublic void publish(String topic, String payload, long timeout) throws AWSIotException, AWSIotTimeoutException
publish
in class AbstractAwsIotClient
topic
- the topic to be published topayload
- the payload to be publishedtimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- the exception thrown if the publish operation failsAWSIotTimeoutException
- the exception thrown if the publish operation times outpublic void publish(String topic, AWSIotQos qos, String payload) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
publish
in class AbstractAwsIotClient
topic
- the topic to be published toqos
- the MQTT QoS used for publishingpayload
- the payload to be publishedAWSIotException
- the exception thrown if the publish operation failspublic void publish(String topic, AWSIotQos qos, String payload, long timeout) throws AWSIotException, AWSIotTimeoutException
publish
in class AbstractAwsIotClient
topic
- the topic to be published toqos
- the MQTT QoS used for publishingpayload
- the payload to be publishedtimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- the exception thrown if the publish operation failsAWSIotTimeoutException
- the exception thrown if the publish operation times outpublic void publish(String topic, byte[] payload) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
publish
in class AbstractAwsIotClient
topic
- the topic to be published topayload
- the payload to be publishedAWSIotException
- the exception thrown if the publish operation failspublic void publish(String topic, byte[] payload, long timeout) throws AWSIotException, AWSIotTimeoutException
publish
in class AbstractAwsIotClient
topic
- the topic to be published topayload
- the payload to be publishedtimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- the exception thrown if the publish operation failsAWSIotTimeoutException
- the exception thrown if the publish operation times outpublic void publish(String topic, AWSIotQos qos, byte[] payload) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
publish
in class AbstractAwsIotClient
topic
- the topic to be published toqos
- the MQTT QoS used for publishingpayload
- the payload to be publishedAWSIotException
- the exception thrown if the publish operation failspublic void publish(String topic, AWSIotQos qos, byte[] payload, long timeout) throws AWSIotException, AWSIotTimeoutException
publish
in class AbstractAwsIotClient
topic
- the topic to be published toqos
- the MQTT QoS used for publishingpayload
- the payload to be publishedtimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- the exception thrown if the publish operation failsAWSIotTimeoutException
- the exception thrown if the publish operation times outpublic void publish(AWSIotMessage message) throws AWSIotException
message
argument. This is a non-blocking call so it
immediately returns once the operation has been queued in the system. The
result of the operation will be notified through the callback functions,
namely AWSIotMessage.onSuccess()
and
AWSIotMessage.onFailure()
, one of which will be invoked after the
operation succeeded or failed respectively. The default implementation
for the callback functions in AWSIotMessage
does nothing. The
user could override one or more of these functions through subclassing.publish
in class AbstractAwsIotClient
message
- the message, including the topic, MQTT QoS, and payload, to be
publishedAWSIotException
- the exception thrown if the publish operation fails to be
queuedpublic void publish(AWSIotMessage message, long timeout) throws AWSIotException
message
argument. This is a non-blocking call so it
immediately returns once the operation has been queued in the system. The
result of the operation will be notified through the callback functions,
namely AWSIotMessage.onSuccess()
, AWSIotMessage.onFailure()
,
and AWSIotMessage.onTimeout()
, one of which will be invoked after
the operation succeeded, failed, or timed out respectively. The user
could override one or more of these functions through subclassing.publish
in class AbstractAwsIotClient
message
- the message, including the topic, MQTT QoS, and payload, to be
publishedtimeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- the exception thrown if the publish operation fails to be
queuedpublic void subscribe(AWSIotTopic topic, boolean blocking) throws AWSIotException
topic
argument. This call can be either blocking or non-blocking
specified by the blocking
argument. For blocking calls, the
calling thread is blocked until the subscribe operation completed or
failed; for non-blocking calls, the result of the operation will be
notified through the callback functions, namely
AWSIotMessage.onSuccess()
and AWSIotMessage.onFailure()
, one of
which will be invoked after the operation succeeded or failed
respectively. For both blocking and non-blocking calls, callback function
AWSIotTopic.onMessage(com.amazonaws.services.iot.client.AWSIotMessage)
is invoked when subscribed message arrives.
The default implementation for the callback functions in
AWSIotTopic
does nothing. The user could override one or more of
these functions through subclassing.
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
subscribe
in class AbstractAwsIotClient
topic
- the topic to subscribe toblocking
- whether the call should be blocking or non-blockingAWSIotException
- the exception thrown if the subscribe operation fails
(blocking) or fails to be queued (non-blocking)public void subscribe(AWSIotTopic topic, long timeout, boolean blocking) throws AWSIotException, AWSIotTimeoutException
topic
argument. This call can be either blocking or non-blocking
specified by the blocking
argument. For blocking call, the
calling thread is blocked until the subscribe operation completed,
failed, or timed out; for non-blocking call, the result of the operation
will be notified through the callback functions, namely
AWSIotMessage.onSuccess()
, AWSIotMessage.onFailure()
and
AWSIotMessage.onTimeout()
, one of which will be invoked after the
operation succeeded, failed, or timed out respectively. For both blocking
and non-blocking calls, callback function AWSIotTopic.onMessage(com.amazonaws.services.iot.client.AWSIotMessage)
is invoked when subscribed message arrives. The default implementation
for the callback functions in AWSIotTopic
does nothing. The user
could override one or more of these functions through subclassing.subscribe
in class AbstractAwsIotClient
topic
- the topic to subscribe totimeout
- the timeout in milliseconds for the operation to be considered
timed outblocking
- whether the call should be blocking or non-blockingAWSIotException
- the exception thrown if the subscribe operation fails
(blocking) or fails to be queued (non-blocking)AWSIotTimeoutException
- the exception thrown if the subscribe operation times out.
This exception is not thrown if the call is non-blocking;
AWSIotMessage.onTimeout()
will be invoked instead if
timeout happens.public void subscribe(AWSIotTopic topic) throws AWSIotException
topic
argument. This is a non-blocking call so it immediately
returns once is the operation has been queued in the system. The result
of the operation will be notified through the callback functions, namely
AWSIotMessage.onSuccess()
and AWSIotMessage.onFailure()
, one of
which will be invoked after the operation succeeded or failed
respectively. Another callback function, AWSIotTopic.onMessage(com.amazonaws.services.iot.client.AWSIotMessage)
,
is invoked when subscribed message arrives. The default implementation
for the callback functions in AWSIotTopic
does nothing. The user
could override one or more of these functions through sub-classing.subscribe
in class AbstractAwsIotClient
topic
- the topic to subscribe toAWSIotException
- the exception thrown if the subscribe operation fails to be
queuedpublic void subscribe(AWSIotTopic topic, long timeout) throws AWSIotException
topic
argument. This is a non-blocking call so it immediately
returns once is the operation has been queued in the system. The result
of the operation will be notified through the callback functions, namely
AWSIotMessage.onSuccess()
, AWSIotMessage.onFailure()
, and
AWSIotMessage.onTimeout()
, one of which will be invoked after the
operation succeeded, failed, or timed out respectively. Another callback
function, AWSIotTopic.onMessage(com.amazonaws.services.iot.client.AWSIotMessage)
, is invoked when subscribed
message arrives. The default implementation for the callback functions in
AWSIotTopic
does nothing. The user could override one or more of
these functions through sub-classing.subscribe
in class AbstractAwsIotClient
topic
- the topic to subscribe totimeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- the exception thrown if the subscribe operation fails to be
queuedpublic void unsubscribe(String topic) throws AWSIotException
Note: Blocking API call without specifying a timeout, in very rare cases, can block the calling thread indefinitely, if the server response is not received or lost. Use the alternative APIs with timeout for applications that expect responses within fixed duration.
unsubscribe
in class AbstractAwsIotClient
topic
- the topic to unsubscribe toAWSIotException
- the exception thrown if the unsubscribe operation failspublic void unsubscribe(String topic, long timeout) throws AWSIotException, AWSIotTimeoutException
unsubscribe
in class AbstractAwsIotClient
topic
- the topic to unsubscribe totimeout
- the timeout in milliseconds that the calling thread will waitAWSIotException
- the exception thrown if the unsubscribe operation failsAWSIotTimeoutException
- the exception thrown if the unsubscribe operation times outpublic void unsubscribe(AWSIotTopic topic) throws AWSIotException
AWSIotMessage.onSuccess()
and AWSIotMessage.onFailure()
,
one of which will be invoked after the operation succeeded or failed
respectively. The default implementation for the callback functions in
AWSIotTopic
does nothing. The user could override one or more of
these functions through subclassing.unsubscribe
in class AbstractAwsIotClient
topic
- the topic to unsubscribe toAWSIotException
- the exception thrown if the unsubscribe operation fails to be
queuedpublic void unsubscribe(AWSIotTopic topic, long timeout) throws AWSIotException
AWSIotMessage.onSuccess()
, AWSIotMessage.onFailure()
, and
AWSIotMessage.onTimeout()
, one of which will be invoked after the
operation succeeded, failed, or timed out respectively. The default
implementation for the callback functions in AWSIotTopic
does
nothing. The user could override one or more of these functions through
subclassing.unsubscribe
in class AbstractAwsIotClient
topic
- the topic to unsubscribe totimeout
- the timeout in milliseconds for the operation to be considered
timed outAWSIotException
- the exception thrown if the unsubscribe operation fails to be
queuedpublic void attach(AWSIotDevice device) throws AWSIotException
AWSIotDevice
.attach
in class AbstractAwsIotClient
device
- the device to be attached to the clientAWSIotException
- the exception thrown if the attach operation failspublic void detach(AWSIotDevice device) throws AWSIotException
detach
in class AbstractAwsIotClient
device
- the device to be detached from the clientAWSIotException
- the exception thrown if the detach operation failspublic AWSIotConnectionStatus getConnectionStatus()
getConnectionStatus
in class AbstractAwsIotClient
public void onConnectionSuccess()
onConnectionSuccess
in interface AwsIotConnectionCallback
onConnectionSuccess
in class AbstractAwsIotClient
public void onConnectionFailure()
onConnectionFailure
in interface AwsIotConnectionCallback
onConnectionFailure
in class AbstractAwsIotClient
public void onConnectionClosed()
onConnectionClosed
in interface AwsIotConnectionCallback
onConnectionClosed
in class AbstractAwsIotClient
Copyright © 2020. All rights reserved.