public final class MqttTransport extends Object implements IotHubTransport
An MQTT transport. Contains functionality for adding messages and sending batched messages to an IoT Hub. Buffers unsent messages until they are received by an IoT Hub. A transport is bound at construction to the following parameters: IoT Hub name, device ID, device key, and message valid seconds.
The transport can also poll an IoT Hub for messages and invoke a user-defined message callback if a message and callback is found.
| Modifier and Type | Field and Description |
|---|---|
protected Queue<IotHubCallbackPacket> |
callbackList
Messages whose callbacks that are waiting to be invoked.
|
protected DeviceClientConfig |
config |
protected Object |
handleMessageLock |
protected MqttIotHubConnection |
mqttIotHubConnection
The MQTT connection.
|
protected Object |
sendMessagesLock
The MQTT connection lock.
|
protected State |
state |
protected Queue<IotHubOutboundPacket> |
waitingList
Messages waiting to be sent.
|
| Constructor and Description |
|---|
MqttTransport(DeviceClientConfig config)
Constructs an instance from the given
DeviceClientConfig
object. |
| Modifier and Type | Method and Description |
|---|---|
void |
addMessage(Message message,
IotHubEventCallback callback,
Object callbackContext)
Adds a message to the transport queue.
|
void |
close()
Closes all resources used to communicate with an IoT Hub.
|
void |
handleMessage()
Invokes the message callback every time a message arrives from the server.
|
void |
invokeCallbacks()
Invokes the callbacks for all completed requests.
|
boolean |
isEmpty()
Returns
true if the transport has no more messages to handle,
and false otherwise. |
void |
open()
Establishes a communication channel with an IoT Hub.
|
void |
sendMessages()
Sends all messages on the transport queue, one at a time.
|
protected final Object sendMessagesLock
protected final Object handleMessageLock
protected State state
protected MqttIotHubConnection mqttIotHubConnection
protected final Queue<IotHubOutboundPacket> waitingList
protected final Queue<IotHubCallbackPacket> callbackList
protected final DeviceClientConfig config
public MqttTransport(DeviceClientConfig config)
DeviceClientConfig
object.config - configuration parameters for the connection.public void open()
throws IOException
open in interface IotHubTransportIOException - if a communication channel cannot be established.public void close()
close() is
called, the transport is no longer usable. If the transport is already
closed, the function shall do nothing.close in interface IotHubTransportclose in interface Closeableclose in interface AutoCloseablepublic void addMessage(Message message, IotHubEventCallback callback, Object callbackContext) throws IllegalStateException
addMessage in interface IotHubTransportmessage - the message to be sent.callback - the callback to be invoked when a response for the
message is received.callbackContext - the context to be passed in when the callback is
invoked.IllegalStateException - if the transport has not been opened or is closedpublic void sendMessages()
throws IllegalStateException
Sends all messages on the transport queue, one at a time. If a previous send attempt had failed, the function will attempt to resend the messages in the previous attempt.
If one has not already been created, the function will initialize an MQTT connection with the IoT Hub specified in the configuration.sendMessages in interface IotHubTransportIllegalStateException - if the transport has not been opened or is closed.public void invokeCallbacks()
throws IllegalStateException
invokeCallbacks in interface IotHubTransportIllegalStateException - if the transport has not been opened or is closed.public void handleMessage()
throws IllegalStateException
Invokes the message callback every time a message arrives from the server. responds to the IoT Hub on how the processed message should be handled by the IoT Hub.
If no message callback is set, the function will do nothing.handleMessage in interface IotHubTransportIllegalStateException - if the transport has not been opened or is closed.public boolean isEmpty()
true if the transport has no more messages to handle,
and false otherwise.isEmpty in interface IotHubTransporttrue if the transport has no more messages to handle,
and false otherwise.Copyright © 2017. All rights reserved.