public class AWSIotMqttManager
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
AWSIotMqttManager.ClientId
Wrapper class for Mqtt client id.
|
static class |
AWSIotMqttManager.Endpoint
Wrapper class for Mqtt broker endpoint.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.Integer |
DEFAULT_AUTO_RECONNECT_ATTEMPTS
Default value for number of auto reconnect attempts before giving up.
|
static java.lang.Boolean |
DEFAULT_AUTO_RECONNECT_ENABLED
Default value for reconnect enabled.
|
static java.lang.Integer |
DEFAULT_KEEP_ALIVE_SECONDS
Default value for MQTT keep alive.
|
static java.lang.Integer |
DEFAULT_MAX_RECONNECT_RETRY_TIME_SECONDS
Default value for maximum delay in exponential backoff reconnect algorithm.
|
static java.lang.Integer |
DEFAULT_MIN_RECONNECT_RETRY_TIME_SECONDS
Default value for starting delay in exponential backoff reconnect algorithm.
|
static java.lang.Integer |
DEFAULT_OFFLINE_PUBLISH_QUEUE_BOUND
Default value for offline publish queue bound.
|
static java.lang.Boolean |
DEFAULT_OFFLINE_PUBLISH_QUEUE_ENABLED
Default value for offline publish queue enabled.
|
Constructor and Description |
---|
AWSIotMqttManager(java.lang.String mqttClientId,
Region region,
java.lang.String accountEndpointPrefix)
Constructs a new AWSIotMqttManager.
|
AWSIotMqttManager(java.lang.String mqttClientId,
java.lang.String endpoint)
Constructs a new AWSIotMqttManager.
|
Modifier and Type | Method and Description |
---|---|
void |
addUserMetaData(java.util.Map<java.lang.String,java.lang.String> userMetaDataMap)
Deprecated.
Since 2.13.2 this method will be removed in the next minor version.
Please use updateUserMetaData method instead.
Sets the userMetaData map.
|
void |
connect(AWSCredentialsProvider credentialsProvider,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using AWS credentials.
|
void |
connect(java.security.KeyStore keyStore,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using certificate and private key in keystore on port 8883.
|
void |
connect(java.lang.String username,
java.lang.String password,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using username and password as credentials.
|
void |
connect(java.lang.String tokenKeyName,
java.lang.String token,
java.lang.String tokenSignature,
java.lang.String customAuthorizer,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using custom authorization tokens.
|
void |
connectUsingALPN(java.security.KeyStore keyStore,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using certificate and private key in keystore on port 443.
|
void |
connectWithProxy(java.security.KeyStore keyStore,
java.lang.String proxyHost,
int proxyPort,
AWSIotMqttClientStatusCallback statusCallback)
Initializes the MQTT session and connects to the specified MQTT server
using certificate and private key in keystore on port 8883 via the proxy specified by a
host and port combination.
|
boolean |
disconnect()
Disconnect from a mqtt client (close current MQTT session).
|
static AWSIotMqttManager |
from(Region region,
AWSIotMqttManager.ClientId clientId,
AWSIotMqttManager.Endpoint endpoint)
Factory method to instantiate a
AWSIotMqttManager from Region , AWSIotMqttManager.ClientId and
AWSIotMqttManager.Endpoint . |
boolean |
fullPublishQueueKeepsOldestMessages()
Keep the oldest messages when publish queue is full?
|
java.lang.String |
getAccountEndpointPrefix()
Return the customer specific endpoint prefix.
|
int |
getConnectionStabilityTime()
Gets the connection established time.
|
java.lang.Long |
getDrainingInterval()
Get the "draining interval" (the time between publish messages are sent from the offline queue when reconnected).
|
int |
getKeepAlive()
Get the MQTT keep alive time.
|
int |
getMaxAutoReconnectAttempts()
Get the current setting of maximum reconnects attempted automatically before quitting.
|
int |
getMaxReconnectRetryTime()
Gets the current maximum value for time to wait between autoreconnect attempts.
|
int |
getMinReconnectRetryTime()
Gets the current starting value for time to wait between autoreconnect attempts.
|
AWSIotMqttLastWillAndTestament |
getMqttLastWillAndTestament()
Get the currently configured Last Will and Testament.
|
java.lang.Integer |
getOfflinePublishQueueBound()
Get the current value of the offline message queue bound.
|
int |
getReconnectTimeout()
Deprecated.
|
boolean |
getSessionPresent() |
boolean |
isAutoReconnect()
Is auto-reconnect enabled?
|
boolean |
isMetricsEnabled() |
boolean |
isOfflinePublishQueueEnabled()
Is the publish queue while offline feature enabled?
|
void |
publishData(byte[] data,
java.lang.String topic,
AWSIotMqttQos qos)
Publish data to an MQTT topic.
|
void |
publishData(byte[] data,
java.lang.String topic,
AWSIotMqttQos qos,
AWSIotMqttMessageDeliveryCallback callback,
java.lang.Object userData)
Publish data to an MQTT topic.
|
void |
publishString(java.lang.String str,
java.lang.String topic,
AWSIotMqttQos qos)
Send a message to an MQTT topic.
|
void |
publishString(java.lang.String str,
java.lang.String topic,
AWSIotMqttQos qos,
AWSIotMqttMessageDeliveryCallback cb,
java.lang.Object userData)
Send a message to an MQTT topic.
|
void |
resetReconnect()
Reset the backoff logic to the initial values.
|
void |
setAutoReconnect(boolean enabled)
Enable / disable the auto-reconnect feature.
|
void |
setAutoResubscribe(boolean enabled)
Enable/Disable auto resubscribe feature.
|
void |
setCleanSession(boolean cleanSession)
Set to true if the connection should be established with a clean session, false otherwise.
|
void |
setConnectionStabilityTime(int time)
Sets the connection stability time.
|
void |
setCredentialsProvider(AWSCredentialsProvider credentialsProvider)
Set the AWS credentials provider to be used in SigV4 MQTT connections.
|
void |
setDrainingInterval(java.lang.Long interval)
Set the "draining interval" (the time between publish messages are sent from the offline queue when reconnected).
|
void |
setFullQueueToKeepNewestMessages()
Set the queue behavior on a full queue to keep newest messages.
|
void |
setFullQueueToKeepOldestMessages()
Set the queue behavior on a full queue to keep oldest messages.
|
void |
setKeepAlive(int keepAlive)
Sets the MQTT keep alive time used by the underlying MQTT client to
determine connection status.
|
void |
setMaxAutoReconnectAttempts(int attempts)
Set the maximum reconnects attempted automatically before quitting.
|
void |
setMaxAutoReconnectAttepts(int attempts)
Deprecated.
Please use
setMaxAutoReconnectAttempts(int) instead. |
void |
setMetricsIsEnabled(boolean enabled)
Turning on/off metrics collection.
|
void |
setMqttLastWillAndTestament(AWSIotMqttLastWillAndTestament lwt)
Set the client last will and testament.
|
void |
setOfflinePublishQueueBound(java.lang.Integer bound)
Set the bound for the number of messages queued while offline.
|
void |
setOfflinePublishQueueEnabled(boolean enabled)
Enable or disable offline publish queueing.
|
void |
setReconnectRetryLimits(int minTimeout,
int maxTimeout)
Sets the times to wait between autoreconnect attempts.
|
void |
setReconnectTimeout(int timeout)
Deprecated.
|
void |
subscribeToTopic(java.lang.String topic,
AWSIotMqttQos qos,
AWSIotMqttNewMessageCallback callback)
Subscribes to an MQTT topic.
|
void |
subscribeToTopic(java.lang.String topic,
AWSIotMqttQos qos,
AWSIotMqttSubscriptionStatusCallback subscriptionStatusCallback,
AWSIotMqttNewMessageCallback callback)
Subscribes to an MQTT topic
|
void |
unsubscribeTopic(java.lang.String topic)
Unsubscribe from an MQTT topic.
|
void |
updateUserMetaData(java.util.Map<java.lang.String,java.lang.String> userMetaDataMap)
This is an internal method.
|
public static final java.lang.Integer DEFAULT_MIN_RECONNECT_RETRY_TIME_SECONDS
public static final java.lang.Integer DEFAULT_MAX_RECONNECT_RETRY_TIME_SECONDS
public static final java.lang.Boolean DEFAULT_AUTO_RECONNECT_ENABLED
public static final java.lang.Integer DEFAULT_AUTO_RECONNECT_ATTEMPTS
public static final java.lang.Integer DEFAULT_KEEP_ALIVE_SECONDS
public static final java.lang.Boolean DEFAULT_OFFLINE_PUBLISH_QUEUE_ENABLED
public static final java.lang.Integer DEFAULT_OFFLINE_PUBLISH_QUEUE_BOUND
public AWSIotMqttManager(java.lang.String mqttClientId, java.lang.String endpoint)
mqttClientId
- MQTT client ID to use with this client.endpoint
- AWS IoT endpoint.
Expected endpoint formats:
XXXXXXX.iot.[region].amazonaws.com or
XXXXXXX-ats.iot.[region].amazonaws.com or
XXXXXXX.ats.iot.cn-north-1.amazonaws.com.cnpublic AWSIotMqttManager(java.lang.String mqttClientId, Region region, java.lang.String accountEndpointPrefix)
mqttClientId
- MQTT client ID to use with this client.region
- The AWS region to use when creating endpoint.accountEndpointPrefix
- Customer specific endpoint prefix XXXXXXX in
XXXXXXX.iot.[region].amazonaws.com or
XXXXXXX-ats.iot.[region].amazonaws.com or
XXXXXXX.ats.iot.cn-north-1.amazonaws.com.cnpublic void setMetricsIsEnabled(boolean enabled)
enabled
- indicates whether metrics collection is enabled or disabled.public boolean isMetricsEnabled()
@Deprecated public void addUserMetaData(java.util.Map<java.lang.String,java.lang.String> userMetaDataMap)
userMetaDataMap
- userMetaData mappublic void updateUserMetaData(java.util.Map<java.lang.String,java.lang.String> userMetaDataMap)
userMetaDataMap
- userMetaData mappublic java.lang.String getAccountEndpointPrefix()
public boolean isAutoReconnect()
public void setAutoReconnect(boolean enabled)
enabled
- true if enabled, false if disabled.@Deprecated public int getReconnectTimeout()
@Deprecated public void setReconnectTimeout(int timeout)
timeout
- timeout value in seconds.public void setReconnectRetryLimits(int minTimeout, int maxTimeout)
minTimeout
- minimum timeout value in seconds.maxTimeout
- maximum timeout value in seconds.public int getMinReconnectRetryTime()
public int getMaxReconnectRetryTime()
public int getMaxAutoReconnectAttempts()
@Deprecated public void setMaxAutoReconnectAttepts(int attempts)
setMaxAutoReconnectAttempts(int)
instead.attempts
- number of reconnects attempted automatically. Retry forever =
-1.public void setMaxAutoReconnectAttempts(int attempts)
attempts
- number of reconnects attempted automatically. Retry
forever = -1.public void setConnectionStabilityTime(int time)
time
- time to wait to declare a connection to be stable (in seconds).public int getConnectionStabilityTime()
public boolean isOfflinePublishQueueEnabled()
public void setOfflinePublishQueueEnabled(boolean enabled)
enabled
- boolean queueing feature is enabled.public java.lang.Integer getOfflinePublishQueueBound()
public void setOfflinePublishQueueBound(java.lang.Integer bound)
bound
- max number of messages to queue while offline. Negative or 0
values ignored.public java.lang.Long getDrainingInterval()
public void setDrainingInterval(java.lang.Long interval)
interval
- milliseconds between offline queue publishes.public boolean fullPublishQueueKeepsOldestMessages()
public void setFullQueueToKeepOldestMessages()
public void setFullQueueToKeepNewestMessages()
public int getKeepAlive()
public void setKeepAlive(int keepAlive)
keepAlive
- the MQTT keep alive time set by the user (in seconds). A
value of 0 disables keep alive.public AWSIotMqttLastWillAndTestament getMqttLastWillAndTestament()
public void setMqttLastWillAndTestament(AWSIotMqttLastWillAndTestament lwt)
lwt
- the desired last will and testament.public void setCredentialsProvider(AWSCredentialsProvider credentialsProvider)
credentialsProvider
- AWS credentials provider used to create the MQTT connection.public void setAutoResubscribe(boolean enabled)
enabled
- Indicate whether auto resubscribe feature is enabled.public void setCleanSession(boolean cleanSession)
cleanSession
- flag to establish a clean sessionpublic static AWSIotMqttManager from(Region region, AWSIotMqttManager.ClientId clientId, AWSIotMqttManager.Endpoint endpoint)
AWSIotMqttManager
from Region
, AWSIotMqttManager.ClientId
and
AWSIotMqttManager.Endpoint
. Use this constructor to connect to AWS IoT using your custom
domain. Other overloads of this constructor support only AWS IoT endpoints which are expected to be
in specific formats. This constructor can however be used for both custom domains as well as AWS IoT managed domains.region
- The AWS region to use when creating endpoint.clientId
- MQTT client ID to use with this client.endpoint
- Customer specific endpoint from AWS IoT or fully qualified domain name of
your custom domain.public void connectUsingALPN(java.security.KeyStore keyStore, AWSIotMqttClientStatusCallback statusCallback)
keyStore
- A keystore containing an keystore with a certificate and
private key. Use IotKeystoreHelper to get keystore.statusCallback
- When new MQTT session status is received the
function of callback will be called with new connection
status.public void connectWithProxy(java.security.KeyStore keyStore, java.lang.String proxyHost, int proxyPort, AWSIotMqttClientStatusCallback statusCallback)
AWSIotKeystoreHelper
to setup the
certificate and key aliases as expected by the underlying socket helper library.keyStore
- A keystore containing an keystore with a certificate and
private key. Use IotKeystoreHelper to get keystore.proxyHost
- hostname of the proxyproxyPort
- proxy port numberstatusCallback
- When new MQTT session status is received the
function of callback will be called with new connection
status.public void connect(java.security.KeyStore keyStore, AWSIotMqttClientStatusCallback statusCallback)
AWSIotKeystoreHelper
to setup the certificate and key aliases as
expected by the underlying socket helper library.keyStore
- A keystore containing an keystore with a certificate and
private key. Use IotKeystoreHelper to get keystore.statusCallback
- When new MQTT session status is received the
function of callback will be called with new connection
status.public void connect(AWSCredentialsProvider credentialsProvider, AWSIotMqttClientStatusCallback statusCallback)
credentialsProvider
- AWS credentialsProvider used to create a WebSocket connection to AWS IoT.statusCallback
- When new MQTT session status is received the function of callback will
be called with new connection status.public void connect(java.lang.String tokenKeyName, java.lang.String token, java.lang.String tokenSignature, java.lang.String customAuthorizer, AWSIotMqttClientStatusCallback statusCallback)
tokenKeyName
- This specifies the key name that your device chooses, which indicates the
token in the custom authorization HTTP request header. It is a string;
the minimum length is 1 character, and the maximum length is 128 characters.token
- This specifies the custom authorization token to authorize the request to the AWS
IoT gateway. It is a string; the minimum length is 1 character, and the maximum
length is 1,024 characters.tokenSignature
- This is the token signature generated by using the private key with the
SHA256withRSA algorithm. tokenSignature can be null if signing is disabled
in domain configuration of the domain that is used to connect to IoT.customAuthorizer
- Name of the AWS IoT custom authorizer.statusCallback
- When a new MQTT session status is received, one of the methods inside this
callback will be invoked with new connection status.public void connect(java.lang.String username, java.lang.String password, AWSIotMqttClientStatusCallback statusCallback)
username
- Username to authenticate client to the broker.password
- Password to authenticate client to the broker.statusCallback
- When a new MQTT session status is received, one of the methods inside this
callback will be invoked with new connection status.public boolean disconnect()
public void resetReconnect()
public void subscribeToTopic(java.lang.String topic, AWSIotMqttQos qos, AWSIotMqttNewMessageCallback callback)
topic
- The topic to which to subscribe.qos
- Quality of Service Level of the subscription.callback
- Callback to be called when new message is received on
this topic for this subscription.public void subscribeToTopic(java.lang.String topic, AWSIotMqttQos qos, AWSIotMqttSubscriptionStatusCallback subscriptionStatusCallback, AWSIotMqttNewMessageCallback callback)
topic
- The topic to which to subscribe.qos
- Quality of Service Level of the subscription.subscriptionStatusCallback
- Callback that will be notified when the subscribe has completed.
Any exception encountered during the subscribe operation is reported on the callback
if avalialble, else AmazonClientException is thrown by this method.callback
- Callback to be called when new message is received on this
topic for this subscription.public void unsubscribeTopic(java.lang.String topic)
topic
- topic from which to unsubscribe.public void publishString(java.lang.String str, java.lang.String topic, AWSIotMqttQos qos)
str
- The message payload to be sent (as a String).topic
- The topic on which to publish.qos
- The quality of service requested for this message.public void publishString(java.lang.String str, java.lang.String topic, AWSIotMqttQos qos, AWSIotMqttMessageDeliveryCallback cb, java.lang.Object userData)
str
- The message payload to be sent (as a String).topic
- The topic on which to publish.qos
- The quality of service requested for this message.cb
- Callback for message status.userData
- User defined data which will be passed back to the user when the
callback is invoked.public void publishData(byte[] data, java.lang.String topic, AWSIotMqttQos qos)
data
- The message payload to be sent as a byte array.topic
- The topic on which to publish.qos
- The quality of service requested for this message.public void publishData(byte[] data, java.lang.String topic, AWSIotMqttQos qos, AWSIotMqttMessageDeliveryCallback callback, java.lang.Object userData)
data
- The message payload to be sent as a byte array.topic
- The topic on which to publish.qos
- The quality of service requested for this message.callback
- Callback for message status.userData
- User defined data which will be passed back to the user when the
callback is invoked.public boolean getSessionPresent()
Copyright © 2018 Amazon Web Services, Inc. All Rights Reserved.