Class AwsIotConnection
- java.lang.Object
-
- com.amazonaws.services.iot.client.core.AwsIotConnection
-
- All Implemented Interfaces:
AwsIotConnectionCallback
- Direct Known Subclasses:
AwsIotMqttConnection
public abstract class AwsIotConnection extends Object implements AwsIotConnectionCallback
This class provides an abstract layer for the library to communicate with the AWS IoT service without having to directly interact with the actual MQTT implementation. The abstraction layer also provides connection retry logic as well as offline message queuing.
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractAwsIotClientclientThe client the connection is associated with.protected AWSIotConnectionStatusconnectionStatusThe connection status.
-
Constructor Summary
Constructors Constructor Description AwsIotConnection(AbstractAwsIotClient client)Instantiates a new connection object.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidcloseConnection(AwsIotMessageCallback callback)Abstract method which is called to terminate an underneath connection.voidconnect(AwsIotMessageCallback callback)The actual connect method exposed by this class.voiddisconnect(AwsIotMessageCallback callback)The actual disconnect method exposed by this class.AbstractAwsIotClientgetClient()The client the connection is associated with.AwsIotMessageCallbackgetConnectCallback()The callback functions for the connect request.AWSIotConnectionStatusgetConnectionStatus()The connection status.ConcurrentLinkedQueue<AWSIotMessage>getPublishQueue()The offline publish queue holding messages while the connection is being established.Future<?>getRetryTask()The future object holding the retry task.intgetRetryTimes()The retry times.ConcurrentLinkedQueue<AWSIotMessage>getSubscribeQueue()The offline subscribe request queue holding messages while the connection is being established.ConcurrentLinkedQueue<AWSIotMessage>getUnsubscribeQueue()The offline unsubscribe request queue holding messages while the connection is being established.booleanisUserDisconnect()Flag to indicate user disconnect is in progress.voidonConnectionClosed()On connection closed.voidonConnectionFailure()On connection failure.voidonConnectionSuccess()On connection success.protected abstract voidopenConnection(AwsIotMessageCallback callback)Abstract method which is called to establish an underneath connection.voidpublish(AWSIotMessage message)The actual publish method exposed by this class.protected abstract voidpublishMessage(AWSIotMessage message)Abstract method which is called to publish a message.voidsetConnectionStatus(AWSIotConnectionStatus connectionStatus)The connection status.voidsubscribe(AWSIotMessage message)The actual subscribe method exposed by this class.protected abstract voidsubscribeTopic(AWSIotMessage message)Abstract method which is called to subscribe to a topic.voidunsubscribe(AWSIotMessage message)The actual unsubscribe method exposed by this class.protected abstract voidunsubscribeTopic(AWSIotMessage message)Abstract method which is called to unsubscribe to a topic.voidupdateCredentials(String awsAccessKeyId, String awsSecretAccessKey, String sessionToken)Updates credentials for the connection, which will be used for new connections.
-
-
-
Field Detail
-
client
protected AbstractAwsIotClient client
The client the connection is associated with.
-
connectionStatus
protected AWSIotConnectionStatus connectionStatus
The connection status. the new connection status
-
-
Constructor Detail
-
AwsIotConnection
public AwsIotConnection(AbstractAwsIotClient client)
Instantiates a new connection object.- Parameters:
client- the client
-
-
Method Detail
-
openConnection
protected abstract void openConnection(AwsIotMessageCallback callback) throws AWSIotException
Abstract method which is called to establish an underneath connection.- Parameters:
callback- connection callback functions- Throws:
AWSIotException- this exception is thrown when the request is failed to be sent
-
closeConnection
protected abstract void closeConnection(AwsIotMessageCallback callback) throws AWSIotException
Abstract method which is called to terminate an underneath connection.- Parameters:
callback- connection callback functions- Throws:
AWSIotException- this exception is thrown when the request is failed to be sent
-
publishMessage
protected abstract void publishMessage(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException
Abstract method which is called to publish a message.- Parameters:
message- the message to be published- Throws:
AWSIotException- this exception is thrown when there's an unrecoverable error happened while processing the requestAwsIotRetryableException- this exception is thrown when the request is failed to be sent, which will be queued and retried
-
subscribeTopic
protected abstract void subscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException
Abstract method which is called to subscribe to a topic.- Parameters:
message- the topic to be subscribed to- Throws:
AWSIotException- this exception is thrown when there's an unrecoverable error happened while processing the requestAwsIotRetryableException- this exception is thrown when the request is failed to be sent, which will be queued and retried
-
unsubscribeTopic
protected abstract void unsubscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException
Abstract method which is called to unsubscribe to a topic.- Parameters:
message- the topic to be unsubscribed to- Throws:
AWSIotException- this exception is thrown when there's an unrecoverable error happened while processing the requestAwsIotRetryableException- this exception is thrown when the request is failed to be sent, which will be queued and retried
-
publish
public void publish(AWSIotMessage message) throws AWSIotException
The actual publish method exposed by this class.- Parameters:
message- the message to be published- Throws:
AWSIotException- this exception is thrown when the underneath failed to process the request
-
updateCredentials
public void updateCredentials(String awsAccessKeyId, String awsSecretAccessKey, String sessionToken)
Updates credentials for the connection, which will be used for new connections.- Parameters:
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.
-
subscribe
public void subscribe(AWSIotMessage message) throws AWSIotException
The actual subscribe method exposed by this class.- Parameters:
message- the topic to be subscribed to- Throws:
AWSIotException- this exception is thrown when the underneath failed to process the request
-
unsubscribe
public void unsubscribe(AWSIotMessage message) throws AWSIotException
The actual unsubscribe method exposed by this class.- Parameters:
message- the topic to be unsubscribed to- Throws:
AWSIotException- this exception is thrown when the underneath failed to process the request
-
connect
public void connect(AwsIotMessageCallback callback) throws AWSIotException
The actual connect method exposed by this class.- Parameters:
callback- user callback functions- Throws:
AWSIotException- this exception is thrown when the underneath layer failed to process the request
-
disconnect
public void disconnect(AwsIotMessageCallback callback) throws AWSIotException
The actual disconnect method exposed by this class.- Parameters:
callback- user callback functions- Throws:
AWSIotException- this exception is thrown when the underneath layer failed to process the request
-
onConnectionSuccess
public void onConnectionSuccess()
Description copied from interface:AwsIotConnectionCallbackOn connection success.- Specified by:
onConnectionSuccessin interfaceAwsIotConnectionCallback
-
onConnectionFailure
public void onConnectionFailure()
Description copied from interface:AwsIotConnectionCallbackOn connection failure.- Specified by:
onConnectionFailurein interfaceAwsIotConnectionCallback
-
onConnectionClosed
public void onConnectionClosed()
Description copied from interface:AwsIotConnectionCallbackOn connection closed.- Specified by:
onConnectionClosedin interfaceAwsIotConnectionCallback
-
getClient
public AbstractAwsIotClient getClient()
The client the connection is associated with.- Returns:
- the current client
-
getConnectionStatus
public AWSIotConnectionStatus getConnectionStatus()
The connection status. the new connection status- Returns:
- the current connection status
-
setConnectionStatus
public void setConnectionStatus(AWSIotConnectionStatus connectionStatus)
The connection status.- Parameters:
connectionStatus- the new connection status
-
getRetryTask
public Future<?> getRetryTask()
The future object holding the retry task.- Returns:
- the current retry task
-
getRetryTimes
public int getRetryTimes()
The retry times.- Returns:
- the current retry times
-
getConnectCallback
public AwsIotMessageCallback getConnectCallback()
The callback functions for the connect request.- Returns:
- the current connect callback
-
isUserDisconnect
public boolean isUserDisconnect()
Flag to indicate user disconnect is in progress.- Returns:
- the current user disconnect flag
-
getPublishQueue
public ConcurrentLinkedQueue<AWSIotMessage> getPublishQueue()
The offline publish queue holding messages while the connection is being established.- Returns:
- the current offline publish queue
-
getSubscribeQueue
public ConcurrentLinkedQueue<AWSIotMessage> getSubscribeQueue()
The offline subscribe request queue holding messages while the connection is being established.- Returns:
- the current offline subscribe request queue
-
getUnsubscribeQueue
public ConcurrentLinkedQueue<AWSIotMessage> getUnsubscribeQueue()
The offline unsubscribe request queue holding messages while the connection is being established.- Returns:
- the current offline unsubscribe request queue
-
-