MessagingClient Class
- java.
lang. Object - com.
microsoft. azure. sdk. iot. service. messaging. MessagingClient
- com.
public class MessagingClient
A client for sending cloud to device and cloud to module messages. For more details on what cloud to device messages are, see this document.
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 Summary
Method Summary
Modifier and Type | Method and Description |
---|---|
synchronized void |
close()
Close this client and release all network resources tied to it. Once closed, this client can be re-opened by calling open(). If this client is already closed, this function will do nothing. |
synchronized void |
close(int timeoutMilliseconds)
Close this client and release all network resources tied to it. Once closed, this client can be re-opened by calling open(). If this client is already closed, this function will do nothing. |
boolean |
isOpen()
Returns true if this client is currently open and false otherwise. This client may lose connectivity due to network issues, so this value may be false even if you have not closed the client yourself. Monitoring the optional errorProcessor that can be set in MessagingClientOptions will provide the context on when connection loss events occur, and why they occurred. |
synchronized void |
open()
Open this client so that it can begin sending cloud to device and/or cloud to module messages. Once opened, you should call 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. |
synchronized void |
open(int timeoutMilliseconds)
Open this client so that it can begin sending cloud to device and/or cloud to module messages. Once opened, this client should call 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. |
void |
send(String deviceId, Message message)
Send a cloud to device message to the device with the provided device id. 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. |
void |
send(String deviceId, Message message, int timeoutMilliseconds)
Send a cloud to device message to the device with the provided device id. 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. |
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. 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. |
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. 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. |
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. 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. |
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. 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. |
Constructor Details
MessagingClient
public MessagingClient(String hostName, AzureSasCredential azureSasCredential, IotHubServiceClientProtocol protocol)
Create a MessagingClient instance with an instance of AzureSasCredential.
Parameters:
MessagingClient
public MessagingClient(String hostName, AzureSasCredential azureSasCredential, IotHubServiceClientProtocol protocol, MessagingClientOptions options)
Create a MessagingClient instance with an instance of AzureSasCredential.
Parameters:
MessagingClient
public MessagingClient(String connectionString, IotHubServiceClientProtocol protocol)
Construct a MessagingClient from the specified connection string
Parameters:
MessagingClient
public MessagingClient(String connectionString, IotHubServiceClientProtocol protocol, MessagingClientOptions options)
Construct a MessagingClient from the specified connection string
Parameters:
MessagingClient
public MessagingClient(String hostName, 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.
Parameters:
MessagingClient
public MessagingClient(String hostName, 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.
Parameters:
Method Details
close
public synchronized void close()
Close this client and release all network resources tied to it. Once closed, this client can be re-opened by calling open(). If this client is already closed, this function will do nothing.
Throws:
close
public synchronized void close(int timeoutMilliseconds)
Close this client and release all network resources tied to it. Once closed, this client can be re-opened by calling open(). If this client is already closed, this function will do nothing.
Parameters:
Throws:
isOpen
public boolean isOpen()
Returns true if this client is currently open and false otherwise. This client may lose connectivity due to network issues, so this value may be false even if you have not closed the client yourself. Monitoring the optional errorProcessor that can be set in MessagingClientOptions will provide the context on when connection loss events occur, and why they occurred.
Returns:
open
public synchronized void open()
Open this client so that it can begin sending cloud to device and/or cloud to module messages. Once opened, you should call 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.
Throws:
open
public synchronized void open(int timeoutMilliseconds)
Open this client so that it can begin sending cloud to device and/or cloud to module messages. Once opened, this client should call 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.
Parameters:
Throws:
send
public void send(String deviceId, Message message)
Send a cloud to device message to the device with the provided device id.
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.
Parameters:
Throws:
send
public void send(String deviceId, Message message, int timeoutMilliseconds)
Send a cloud to device message to the device with the provided device id.
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.
Parameters:
Throws:
send
public 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.
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.
Parameters:
Throws:
send
public 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.
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.
Parameters:
Throws:
sendAsync
public void sendAsync(String deviceId, Message message, Consumer
Asynchronously send a cloud to device message to the device with the provided device Id.
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.
Parameters:
Throws:
sendAsync
public void sendAsync(String deviceId, String moduleId, Message message, Consumer
Asynchronously send a cloud to device message to the module with the provided module id on the device with the provided device Id.
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.
Parameters:
Throws:
Applies to
Azure SDK for Java