public final class MessagingClient extends Object
This client relies on a persistent amqp/amqp_ws connection to IoT Hub that may break due to network instability.
While optional to monitor, users are highly encouraged to utilize the errorProcessorHandler defined in the
MessagingClientOptions
when constructing this client in order to monitor the connection state and to re-open
the connection when needed. See the messaging client sample in this repo for best practices for monitoring and handling
disconnection events.
Constructor and Description |
---|
MessagingClient(String hostName,
com.azure.core.credential.AzureSasCredential azureSasCredential,
IotHubServiceClientProtocol protocol)
Create a
MessagingClient instance with an instance of AzureSasCredential . |
MessagingClient(String hostName,
com.azure.core.credential.AzureSasCredential azureSasCredential,
IotHubServiceClientProtocol protocol,
MessagingClientOptions options)
Create a
MessagingClient instance with an instance of AzureSasCredential . |
MessagingClient(String connectionString,
IotHubServiceClientProtocol protocol)
Construct a MessagingClient from the specified connection string
|
MessagingClient(String connectionString,
IotHubServiceClientProtocol protocol,
MessagingClientOptions options)
Construct a MessagingClient from the specified connection string
|
MessagingClient(String hostName,
com.azure.core.credential.TokenCredential credential,
IotHubServiceClientProtocol protocol)
Create a
MessagingClient instance with a custom TokenCredential to allow for finer grain control
of authentication tokens used in the underlying connection. |
MessagingClient(String hostName,
com.azure.core.credential.TokenCredential credential,
IotHubServiceClientProtocol protocol,
MessagingClientOptions options)
Create a
MessagingClient instance with a custom TokenCredential to allow for finer grain control
of authentication tokens used in the underlying connection. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this client and release all network resources tied to it.
|
void |
close(int timeoutMilliseconds)
Close this client and release all network resources tied to it.
|
boolean |
isOpen()
Returns true if this client is currently open and false otherwise.
|
void |
open()
Open this client so that it can begin sending cloud to device and/or cloud to module messages.
|
void |
open(int timeoutMilliseconds)
Open this client so that it can begin sending cloud to device and/or cloud to module messages.
|
void |
send(String deviceId,
Message message)
Send a cloud to device message to the device with the provided device id.
|
void |
send(String deviceId,
Message message,
int timeoutMilliseconds)
Send a cloud to device message to the device with the provided device id.
|
void |
send(String deviceId,
String moduleId,
Message message)
Send a cloud to device message to the module with the provided module id on the device with the provided device Id.
|
void |
send(String deviceId,
String moduleId,
Message message,
int timeoutMilliseconds)
Send a cloud to device message to the module with the provided module id on the device with the provided device Id.
|
void |
sendAsync(String deviceId,
Message message,
Consumer<SendResult> onMessageSentCallback,
Object context)
Asynchronously send a cloud to device message to the device with the provided device Id.
|
void |
sendAsync(String deviceId,
String moduleId,
Message message,
Consumer<SendResult> onMessageSentCallback,
Object context)
Asynchronously send a cloud to device message to the module with the provided module id on the device with the provided device Id.
|
public MessagingClient(String connectionString, IotHubServiceClientProtocol protocol)
connectionString
- The connection string for the IotHubprotocol
- The protocol that the client will communicate to IoT Hub over.public MessagingClient(String connectionString, IotHubServiceClientProtocol protocol, MessagingClientOptions options)
connectionString
- The connection string for the IotHubprotocol
- The protocol that the client will communicate to IoT Hub over.options
- The connection options to use when connecting to the service. May not be null.public MessagingClient(String hostName, com.azure.core.credential.TokenCredential credential, IotHubServiceClientProtocol protocol)
MessagingClient
instance with a custom TokenCredential
to allow for finer grain control
of authentication tokens used in the underlying connection.hostName
- The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")credential
- The custom TokenCredential
that will provide authentication tokens to
this library when they are needed. The provided tokens must be Json Web Tokens.protocol
- The protocol that the client will communicate to IoT Hub over.public MessagingClient(String hostName, com.azure.core.credential.TokenCredential credential, IotHubServiceClientProtocol protocol, MessagingClientOptions options)
MessagingClient
instance with a custom TokenCredential
to allow for finer grain control
of authentication tokens used in the underlying connection.hostName
- The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")credential
- The custom TokenCredential
that will provide authentication tokens to
this library when they are needed. The provided tokens must be Json Web Tokens.protocol
- The protocol that the client will communicate to IoT Hub over.options
- The connection options to use when connecting to the service. May not be null.public MessagingClient(String hostName, com.azure.core.credential.AzureSasCredential azureSasCredential, IotHubServiceClientProtocol protocol)
MessagingClient
instance with an instance of AzureSasCredential
.hostName
- The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")azureSasCredential
- The SAS token provider that will be used for authentication.protocol
- The protocol that the client will communicate to IoT Hub over.public MessagingClient(String hostName, com.azure.core.credential.AzureSasCredential azureSasCredential, IotHubServiceClientProtocol protocol, MessagingClientOptions options)
MessagingClient
instance with an instance of AzureSasCredential
.hostName
- The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")azureSasCredential
- The SAS token provider that will be used for authentication.protocol
- The protocol that the client will communicate to IoT Hub over.options
- The connection options to use when connecting to the service. May not be null.public void open() throws IotHubException, IOException, InterruptedException, TimeoutException
close()
once no more messages will be sent in order to free up network resources. If this
client is already open, then this function will do nothing.IotHubException
- If any IoT Hub level exceptions occur such as an IotHubUnauthorizedException
.IOException
- If any network level exceptions occur such as the connection timing out.InterruptedException
- If this thread is interrupted while waiting for the connection to the service to open.TimeoutException
- If the connection is not established before the default timeout.public void open(int timeoutMilliseconds) throws IotHubException, IOException, InterruptedException, TimeoutException
close()
once no more messages will be sent in order to free up network resources. If this
client is already open, then this function will do nothing.timeoutMilliseconds
- the maximum number of milliseconds to wait for the underlying amqp connection to open.
If this value is 0, it will have an infinite timeout.IotHubException
- If any IoT Hub level exceptions occur such as an IotHubUnauthorizedException
.IOException
- If any network level exceptions occur such as the connection timing out.InterruptedException
- If this thread is interrupted while waiting for the connection to the service to open.TimeoutException
- If the connection is not established before the provided timeout.public void close() throws InterruptedException
open()
. If this client is already closed, this function will do nothing.InterruptedException
- if this function is interrupted while waiting for the connection to close down all
network resources.public void close(int timeoutMilliseconds) throws InterruptedException
open()
. If this client is already closed, this function will do nothing.timeoutMilliseconds
- the maximum number of milliseconds to wait for the underlying amqp connection to close.
If this value is 0, it will have an infinite timeout. If the provided timeout has passed and the connection has
not closed gracefully, then the connection will be forcefully closed and no exception will be thrown.InterruptedException
- if this function is interrupted while waiting for the connection to close down all
network resources.public void send(String deviceId, Message message) throws IotHubException, InterruptedException, TimeoutException, IllegalStateException
This method is a blocking call that will wait for the sent message to be acknowledged by the service before returning.
This is provided for simplicity and for applications that aren't concerned with throughput. For applications that
need to provided higher throughput of sent cloud to device messages, users should use sendAsync(String, Message, Consumer, Object)
as demonstrated in the messaging client performance sample in this repo.
deviceId
- the Id of the device to send the cloud to device message to.message
- the message to send to the device.IotHubException
- If any IoT Hub level exception is thrown. For instance, if the provided message exceeds
the IoT Hub message size limit, IotHubMessageTooLargeException
will be thrown.InterruptedException
- If this function is interrupted while waiting for the cloud to device message to be acknowledged
by the service.TimeoutException
- If the sent message isn't acknowledged by the service within the default timeout.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public void send(String deviceId, Message message, int timeoutMilliseconds) throws IotHubException, InterruptedException, TimeoutException, IllegalStateException
This method is a blocking call that will wait for the sent message to be acknowledged by the service before returning.
This is provided for simplicity and for applications that aren't concerned with throughput. For applications that
need to provided higher throughput of sent cloud to device messages, users should use sendAsync(String, Message, Consumer, Object)
as demonstrated in the messaging client performance sample in this repo.
deviceId
- the Id of the device to send the cloud to device message to.message
- the message to send to the device.timeoutMilliseconds
- the maximum number of milliseconds to wait for the message to be sent before timing out and throwing an IotHubException
.IotHubException
- If any IoT Hub level exception is thrown. For instance, if the provided message exceeds
the IoT Hub message size limit, IotHubMessageTooLargeException
will be thrown.InterruptedException
- If this function is interrupted while waiting for the cloud to device message to be acknowledged
by the service.TimeoutException
- If the sent message isn't acknowledged by the service within the provided timeout.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public void send(String deviceId, String moduleId, Message message) throws IotHubException, InterruptedException, TimeoutException, IllegalStateException
This method is a blocking call that will wait for the sent message to be acknowledged by the service before returning.
This is provided for simplicity and for applications that aren't concerned with throughput. For applications that
need to provided higher throughput of sent cloud to device messages, users should use sendAsync(String, String, Message, Consumer, Object)
as demonstrated in the messaging client performance sample in this repo.
deviceId
- the Id of the device that contains the module that the message is being sent to.moduleId
- the Id of the module to send the cloud to device message to.message
- the message to send to the device.IotHubException
- If any IoT Hub level exception is thrown. For instance, if the provided message exceeds
the IoT Hub message size limit, IotHubMessageTooLargeException
will be thrown.InterruptedException
- If this function is interrupted while waiting for the cloud to device message to be acknowledged
by the service.TimeoutException
- If the sent message isn't acknowledged by the service within the default timeout.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public void send(String deviceId, String moduleId, Message message, int timeoutMilliseconds) throws IotHubException, InterruptedException, TimeoutException, IllegalStateException
This method is a blocking call that will wait for the sent message to be acknowledged by the service before returning.
This is provided for simplicity and for applications that aren't concerned with throughput. For applications that
need to provided higher throughput of sent cloud to device messages, users should use sendAsync(String, String, Message, Consumer, Object)
as demonstrated in the messaging client performance sample in this repo.
deviceId
- the Id of the device that contains the module that the message is being sent to.moduleId
- the Id of the module to send the cloud to device message to.message
- the message to send to the device.timeoutMilliseconds
- the maximum number of milliseconds to wait for the message to be sent before timing out and throwing an IotHubException
.IotHubException
- If any IoT Hub level exception is thrown. For instance, if the provided message exceeds
the IoT Hub message size limit, IotHubMessageTooLargeException
will be thrown.InterruptedException
- If this function is interrupted while waiting for the cloud to device message to be acknowledged
by the service.TimeoutException
- If the sent message isn't acknowledged by the service within the provided timeout.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public void sendAsync(String deviceId, Message message, Consumer<SendResult> onMessageSentCallback, Object context) throws IllegalStateException
Unlike the synchronous version of this function, this function does not throw any exceptions. Instead, any exception
encountered while sending the message will be provided in the SendResult
provided in the onMessageSentCallback.
To see an example of how this looks, see the messaging client performance sample in this repo.
deviceId
- the Id of the device to send the cloud to device message to.message
- the message to send to the device.onMessageSentCallback
- the callback that will be executed when the message has either successfully been
sent, or has failed to send. May be null if you don't care if the sent message is acknowledged by the service.context
- user defined context that will be provided in the onMessageSentCallback callback when it executes. May be null.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public void sendAsync(String deviceId, String moduleId, Message message, Consumer<SendResult> onMessageSentCallback, Object context) throws IllegalStateException
Unlike the synchronous version of this function, this function does not throw any exceptions. Instead, any exception
encountered while sending the message will be provided in the SendResult
provided in the onMessageSentCallback.
To see an example of how this looks, see the messaging client performance sample in this repo.
deviceId
- the Id of the device that contains the module that the message is being sent to.moduleId
- the Id of the module to send the cloud to device message to.message
- the message to send to the device.onMessageSentCallback
- the callback that will be executed when the message has either successfully been
sent, or has failed to send. May be null if you don't care if the sent message is acknowledged by the service.context
- user defined context that will be provided in the onMessageSentCallback callback when it executes. May be null.IllegalStateException
- if the client has not been opened yet, or is closed for any other reason such as connectivity loss.public boolean isOpen()
MessagingClientOptions
will provide the context on when connection loss events occur, and
why they occurred.Copyright © 2023. All rights reserved.