public class MultiplexingClient
extends java.lang.Object
A given AMQPS connection requires a TLS connection, so multiplexing may be worthwhile if you want to limit the number of TLS connections needed to connect multiple device clients to IoT hub.
A given multiplexing client also has a fixed amount of worker threads regardless of how many device clients are being multiplexed. Comparatively, every non-multiplexed device client instance has its own set of worker threads. Multiplexing may be worthwhile if you want fewer worker threads.
Only AMQPS and AMQPS_WS support multiplexing, and only symmetric key authenticated devices can be multiplexed.
ModuleClient instances cannot be multiplexed.
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_MULTIPLEX_DEVICE_COUNT_AMQPS
The maximum number of devices that can be multiplexed together on a single multiplexed AMQPS connection
|
static int |
MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS
The maximum number of devices that can be multiplexed together on a single multiplexed AMQPS_WS connection
|
| Constructor and Description |
|---|
MultiplexingClient(java.lang.String hostName,
IotHubClientProtocol protocol)
Instantiate a new MultiplexingClient that will establish a multiplexed connection through a proxy.
|
MultiplexingClient(java.lang.String hostName,
IotHubClientProtocol protocol,
MultiplexingClientOptions options)
Instantiate a new MultiplexingClient that will establish a multiplexed connection through a proxy.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close this multiplexing client.
|
int |
getRegisteredDeviceCount()
Get the number of currently registered devices on this multiplexing client.
|
boolean |
isDeviceRegistered(java.lang.String deviceId)
Returns if a device client for the provided device Id is already registered to this multiplexing client.
|
void |
open(boolean withRetry)
Opens this multiplexing client.
|
void |
registerDeviceClient(DeviceClient deviceClient)
Register a device client to this multiplexing client.
|
void |
registerDeviceClient(DeviceClient deviceClient,
long timeoutMilliseconds)
Register a device client to this multiplexing client.
|
void |
registerDeviceClients(java.lang.Iterable<DeviceClient> deviceClients)
Register multiple device clients to this multiplexing client.
|
void |
registerDeviceClients(java.lang.Iterable<DeviceClient> deviceClients,
long timeoutMilliseconds)
Register multiple device clients to this multiplexing client.
|
void |
setConnectionStatusChangeCallback(IotHubConnectionStatusChangeCallback callback,
java.lang.Object callbackContext)
Registers a callback to be executed when the connection status of the multiplexed connection as a whole changes.
|
void |
setRetryPolicy(RetryPolicy retryPolicy)
Sets the given retry policy for the multiplexing client level connection management.
|
void |
unregisterDeviceClient(DeviceClient deviceClient)
Unregister a device client from this multiplexing client.
|
void |
unregisterDeviceClient(DeviceClient deviceClient,
long timeoutMilliseconds)
Unregister a device client from this multiplexing client.
|
void |
unregisterDeviceClients(java.lang.Iterable<DeviceClient> deviceClients)
Unregister multiple device clients from this multiplexing client.
|
void |
unregisterDeviceClients(java.lang.Iterable<DeviceClient> deviceClients,
long timeoutMilliseconds)
Unregister multiple device clients from this multiplexing client.
|
public static final int MAX_MULTIPLEX_DEVICE_COUNT_AMQPS
public static final int MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS
public MultiplexingClient(java.lang.String hostName,
IotHubClientProtocol protocol)
hostName - The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")protocol - The transport protocol that this client will build the multiplexed connection on. Must be either
IotHubClientProtocol.AMQPS or IotHubClientProtocol.AMQPS_WS.public MultiplexingClient(java.lang.String hostName,
IotHubClientProtocol protocol,
MultiplexingClientOptions options)
hostName - The hostname of your IoT Hub instance (For instance, "your-iot-hub.azure-devices.net")protocol - The transport protocol that this client will build the multiplexed connection on. Must be
IotHubClientProtocol.AMQPS_WS since using IotHubClientProtocol.AMQPS does not support proxies.options - The optional parameters to configure this client to use.public void open(boolean withRetry)
throws IotHubClientException
This call behaves synchronously, so if it returns without throwing, then all registered device clients were successfully opened.
If this client is already open, then this method will do nothing.
withRetry - if true, this open call will apply the current retry policy to allow for the open call to be
retried if it fails.IotHubClientException - If any IO or authentication errors occur while opening the multiplexed connection.MultiplexingClientRegistrationException - If one or many of the registered devices failed to authenticate.
Any devices not found in the map of registration exceptions provided by
MultiplexingClientRegistrationException.getRegistrationExceptions() have registered successfully.
Even when this is thrown, the AMQPS/AMQPS_WS connection is still open, and other clients may be registered to it.public void close()
If this client is already closed, then this method will do nothing.
Once closed, this client can be re-opened. It will preserve all previously registered device clients.
public void registerDeviceClient(DeviceClient deviceClient) throws java.lang.InterruptedException, IotHubClientException
Users should use registerDeviceClients(Iterable) for registering multiple devices as it has some
performance improvements over repeatedly calling this method for individual device registrations. This method blocks on each registration, whereas
registerDeviceClients(Iterable) blocks on all of the registrations after starting them all asynchronously.
A device client can be unregistered using unregisterDeviceClient(DeviceClient), unregisterDeviceClient(DeviceClient, long),
unregisterDeviceClients(Iterable), or unregisterDeviceClients(Iterable, long). A device client will not be unregistered
automatically if it encounters a non-retryable exception, so users are responsible for unregistering a device client
when they no longer want it in this multiplexing client.
Up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS devices can be registered on a multiplexed AMQPS connection,
and up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS devices can be registered on a multiplexed AMQPS_WS connection.
If the multiplexing client is already open, then this device client will automatically
be opened, too. If the multiplexing client is not open yet, then this device client will not be opened until
open(boolean) is called.
If the multiplexed connection is already open, then this call will add this device client to the multiplexed connection, and then will block until the registration has been completed.
Any proxy settings set to the provided device clients will be overwritten by the proxy settings of this multiplexing client.
The registered device client must use the same transport protocol (AMQPS or AMQPS_WS) that this multiplexing client uses.
Each registered device client may have its own retry policy and its own SAS token expiry time, separate from every other registered device client.
The registered device client must use symmetric key based authentication.
The registered device client must belong to the same IoT hub as all previously registered device clients.
If the provided device client is already registered to this multiplexing client, then then this method will do nothing.
Any subscriptions (twin, methods, cloud to device messages) set on this device client from when it was previously registered to any multiplexing client will need to be set again as subscriptions and their callbacks are not preserved.
deviceClient - The device client to associate with this multiplexing client.java.lang.InterruptedException - If the thread gets interrupted while waiting for the registration to succeed. This
will never be thrown if the multiplexing client is not open yet.MultiplexingClientRegistrationException - If the device failed to register. Details for
this failure can be found nested within the map given by
MultiplexingClientRegistrationException.getRegistrationExceptions(). If this exception is
thrown, the device was not registered, and therefore it does not need to be unregistered.IotHubClientException - If this operation takes longer than the default timeout allows.public void registerDeviceClient(DeviceClient deviceClient, long timeoutMilliseconds) throws java.lang.InterruptedException, IotHubClientException
Users should use registerDeviceClients(Iterable) for registering multiple devices as it has some
performance improvements over repeatedly calling this method for individual device registrations. This method blocks on each registration, whereas
registerDeviceClients(Iterable) blocks on all of the registrations after starting them all asynchronously.
A device client can be unregistered using unregisterDeviceClient(DeviceClient), unregisterDeviceClient(DeviceClient, long),
unregisterDeviceClients(Iterable), or unregisterDeviceClients(Iterable, long). A device client will not be unregistered
automatically if it encounters a non-retryable exception, so users are responsible for unregistering a device client
when they no longer want it in this multiplexing client.
Up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS devices can be registered on a multiplexed AMQPS connection,
and up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS devices can be registered on a multiplexed AMQPS_WS connection.
If the multiplexing client is already open, then this device client will automatically
be opened, too. If the multiplexing client is not open yet, then this device client will not be opened until
open(boolean) is called.
If the multiplexed connection is already open, then this call will add this device client to the multiplexed connection, and then will block until the registration has been completed.
Any proxy settings set to the provided device clients will be overwritten by the proxy settings of this multiplexing client.
The registered device client must use the same transport protocol (AMQPS or AMQPS_WS) that this multiplexing client uses.
Each registered device client may have its own retry policy and its own SAS token expiry time, separate from every other registered device client.
The registered device client must use symmetric key based authentication.
The registered device client must belong to the same IoT hub as all previously registered device clients.
If the provided device client is already registered to this multiplexing client, then then this method will do nothing.
Any subscriptions (twin, methods, cloud to device messages) set on this device client from when it was previously registered to any multiplexing client will need to be set again as subscriptions and their callbacks are not preserved.
deviceClient - The device client to associate with this multiplexing client.timeoutMilliseconds - How long (in milliseconds) to let this operation wait for all registrations to complete.
If this threshold is passed, a IotHubClientException with status code
IotHubStatusCode.DEVICE_OPERATION_TIMED_OUT is thrown.java.lang.InterruptedException - If the thread gets interrupted while waiting for the registration to succeed. This
will never be thrown if the multiplexing client is not open yet.MultiplexingClientRegistrationException - If the device failed to register. Details for
this failure can be found nested within the map given by
MultiplexingClientRegistrationException.getRegistrationExceptions(). If this exception is
thrown, the device was not registered, and therefore it does not need to be unregistered.IotHubClientException - If this operation takes longer than the provided timeout allows.public void registerDeviceClients(java.lang.Iterable<DeviceClient> deviceClients) throws java.lang.InterruptedException, IotHubClientException
Up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS devices can be registered on a multiplexed AMQPS connection,
and up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS devices can be registered on a multiplexed AMQPS_WS connection.
A device client can be unregistered using unregisterDeviceClient(DeviceClient), unregisterDeviceClient(DeviceClient, long),
unregisterDeviceClients(Iterable), or unregisterDeviceClients(Iterable, long). A device client will not be unregistered
automatically if it encounters a non-retryable exception, so users are responsible for unregistering a device client
when they no longer want it in this multiplexing client.
If the multiplexing client is already open, then these device clients will automatically
be opened, too. If the multiplexing client is not open yet, then these device clients will not be opened until
open(boolean) is called.
If the multiplexed connection is already open, then this call will asynchronously add each device client to the multiplexed connection, and then will block until all registrations have been completed.
Any proxy settings set to the provided device clients will be overwritten by the proxy settings of this multiplexing client.
The registered device clients must use the same transport protocol (AMQPS or AMQPS_WS) that this multiplexing client uses.
Each registered device client may have its own retry policy and its own SAS token expiry time, separate from every other registered device client.
The registered device clients must use symmetric key based authentication.
The registered device clients must belong to the same IoT hub as all previously registered device clients.
If any of these device clients are already registered to this multiplexing client, then then this method will not do anything to that particular device client. All other provided device clients will still be registered though.
Any subscriptions (twin, methods, cloud to device messages) set on these device clients from when it was previously registered to any multiplexing client will need to be set again as subscriptions and their callbacks are not preserved.
deviceClients - The device clients to associate with this multiplexing client.java.lang.InterruptedException - If the thread gets interrupted while waiting for the registration to succeed. This
will never be thrown if the multiplexing client is not open yet.MultiplexingClientRegistrationException - If one or more devices failed to register.
Details for each failure can be found in the map provided by
MultiplexingClientRegistrationException.getRegistrationExceptions(). Any devices not
found in the map of registration exceptions provided by this exception have registered successfully. Any devices
that are found in the map of registration exceptions provided by this exception were not registered, and therefore
do not need to be unregistered.IotHubClientException - If this operation takes longer than the default timeout allows.public void registerDeviceClients(java.lang.Iterable<DeviceClient> deviceClients, long timeoutMilliseconds) throws java.lang.InterruptedException, IotHubClientException
Up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS devices can be registered on a multiplexed AMQPS connection,
and up to MAX_MULTIPLEX_DEVICE_COUNT_AMQPS_WS devices can be registered on a multiplexed AMQPS_WS connection.
A device client can be unregistered using unregisterDeviceClient(DeviceClient), unregisterDeviceClient(DeviceClient, long),
unregisterDeviceClients(Iterable), or unregisterDeviceClients(Iterable, long). A device client will not be unregistered
automatically if it encounters a non-retryable exception, so users are responsible for unregistering a device client
when they no longer want it in this multiplexing client.
If the multiplexing client is already open, then these device clients will automatically
be opened, too. If the multiplexing client is not open yet, then these device clients will not be opened until
open(boolean) is called.
If the multiplexed connection is already open, then this call will asynchronously add each device client to the multiplexed connection, and then will block until all registrations have been completed.
Any proxy settings set to the provided device clients will be overwritten by the proxy settings of this multiplexing client.
The registered device clients must use the same transport protocol (AMQPS or AMQPS_WS) that this multiplexing client uses.
Each registered device client may have its own retry policy and its own SAS token expiry time, separate from every other registered device client.
The registered device clients must use symmetric key based authentication.
The registered device clients must belong to the same IoT hub as all previously registered device clients.
If any of these device clients are already registered to this multiplexing client, then then this method will not do anything to that particular device client. All other provided device clients will still be registered though.
Any subscriptions (twin, methods, cloud to device messages) set on these device clients from when it was previously registered to any multiplexing client will need to be set again as subscriptions and their callbacks are not preserved.
deviceClients - The device clients to associate with this multiplexing client.timeoutMilliseconds - How long (in milliseconds) to let this operation wait for all registrations to complete.
If this threshold is passed, a IotHubClientException with status code
IotHubStatusCode.DEVICE_OPERATION_TIMED_OUT is thrown.java.lang.InterruptedException - If the thread gets interrupted while waiting for the registration to succeed. This
will never be thrown if the multiplexing client is not open yet.MultiplexingClientRegistrationException - If one or more devices failed to register.
Details for each failure can be found in the map provided by
MultiplexingClientRegistrationException.getRegistrationExceptions(). Any devices not
found in the map of registration exceptions provided by this exception have registered successfully. Any devices
that are found in the map of registration exceptions provided by this exception were not registered, and therefore
do not need to be unregistered.IotHubClientException - If this operation takes longer than the provided timeout allows.public void unregisterDeviceClient(DeviceClient deviceClient) throws java.lang.InterruptedException, IotHubClientException
Users should use unregisterDeviceClients(Iterable) for unregistering multiple devices as it has some
performance improvements over repeatedly calling this method for individual device unregistrations. This method blocks on each unregistration, whereas
registerDeviceClients(Iterable) blocks on all of the unregistrations after starting them all asynchronously.
If the multiplexed connection is already open, then this call will close the AMQP device session associated with this device, but it will not close any other registered device sessions or the multiplexing client itself.
If the multiplexed connection is already open, and this call would unregister the last device client,
the multiplexed connection will remain open. The multiplexed connection can only be closed by calling
close()
Once a device client is unregistered, it may be re-registered to this or any other multiplexing client. It cannot be used in non-multiplexing scenarios.
Any subscriptions set on this device client for twin/methods/cloud to device messages will need to be set again after this device is re-registered.
deviceClient - The device client to unregister from this multiplexing client.java.lang.InterruptedException - If the thread gets interrupted while waiting for the unregistration to succeed.IotHubClientException - If the unregistration takes longer than the default timeout allows.public void unregisterDeviceClient(DeviceClient deviceClient, long timeoutMilliseconds) throws java.lang.InterruptedException, IotHubClientException
Users should use unregisterDeviceClients(Iterable) for unregistering multiple devices as it has some
performance improvements over repeatedly calling this method for individual device unregistrations. This method blocks on each unregistration, whereas
registerDeviceClients(Iterable) blocks on all of the unregistrations after starting them all asynchronously.
If the multiplexed connection is already open, then this call will close the AMQP device session associated with this device, but it will not close any other registered device sessions or the multiplexing client itself.
If the multiplexed connection is already open, and this call would unregister the last device client,
the multiplexed connection will remain open. The multiplexed connection can only be closed by calling
close()
Once a device client is unregistered, it may be re-registered to this or any other multiplexing client. It cannot be used in non-multiplexing scenarios.
Any subscriptions set on this device client for twin/methods/cloud to device messages will need to be set again after this device is re-registered.
deviceClient - The device client to unregister from this multiplexing client.timeoutMilliseconds - How long (in milliseconds) to let this operation wait for all registrations to complete.
If this threshold is passed, a IotHubClientException with status code
IotHubStatusCode.DEVICE_OPERATION_TIMED_OUT is thrown.java.lang.InterruptedException - If the thread gets interrupted while waiting for the unregistration to succeed.IotHubClientException - If the unregistration takes longer than the provided timeout allows.public void unregisterDeviceClients(java.lang.Iterable<DeviceClient> deviceClients) throws java.lang.InterruptedException, IotHubClientException
If the multiplexed connection is already open, then this call will close the AMQP device session associated with this device, but it will not close any other registered device sessions or the multiplexing client itself.
If the multiplexed connection is already open, and this call would unregister the last device clients,
the multiplexed connection will remain open. The multiplexed connection can only be closed by calling
close()
Once a device client is unregistered, it may be re-registered to this or any other multiplexing client. It cannot be used in non-multiplexing scenarios.
Any subscriptions set on these device clients for twin/methods/cloud to device messages will need to be set again after these devices are re-registered.
deviceClients - The device clients to unregister from this multiplexing client.java.lang.InterruptedException - If the thread gets interrupted while waiting for the unregistration to succeed.IotHubClientException - If the unregistration takes longer than the default timeout allows.public void unregisterDeviceClients(java.lang.Iterable<DeviceClient> deviceClients, long timeoutMilliseconds) throws java.lang.InterruptedException, IotHubClientException
If the multiplexed connection is already open, then this call will close the AMQP device session associated with this device, but it will not close any other registered device sessions or the multiplexing client itself.
If the multiplexed connection is already open, and this call would unregister the last device clients,
the multiplexed connection will remain open. The multiplexed connection can only be closed by calling
close()
Once a device client is unregistered, it may be re-registered to this or any other multiplexing client. It cannot be used in non-multiplexing scenarios.
Any subscriptions set on these device clients for twin/methods/cloud to device messages will need to be set again after these devices are re-registered.
deviceClients - The device clients to unregister from this multiplexing client.timeoutMilliseconds - How long (in milliseconds) to let this operation wait for all registrations to complete.
If this threshold is passed, a IotHubClientException with status code
IotHubStatusCode.DEVICE_OPERATION_TIMED_OUT is thrown.java.lang.InterruptedException - If the thread gets interrupted while waiting for the unregistration to succeed.IotHubClientException - If the unregistration takes longer than the provided timeout allows.public void setConnectionStatusChangeCallback(IotHubConnectionStatusChangeCallback callback, java.lang.Object callbackContext)
Note that this callback will not be fired for device specific connection status changes. In order to be notified
when a particular device's connection status changes, you will need to register a connection status change callback
on that device client instance using InternalClient.setConnectionStatusChangeCallback(IotHubConnectionStatusChangeCallback, Object).
Note that the thread used to deliver this callback should not be used to call open()/close() on the client that this callback belongs to. All open()/close() operations should be done on a separate thread
callback - The callback to be fired when the connection status of the multiplexed connection changes.
Can be null to unset this listener as long as the provided callbackContext is also null.callbackContext - a context to be passed to the callback. Can be null.public boolean isDeviceRegistered(java.lang.String deviceId)
deviceId - The Id of the device client to look for.public int getRegisteredDeviceCount()
public void setRetryPolicy(RetryPolicy retryPolicy)
retryPolicy - The policy that the multiplexing client will use when reconnecting.Copyright © 2023. All rights reserved.