Azure IoT Hub
Since Camel 4.24
Only producer is supported
The Azure IoT Hub component provides the capability to send device-to-cloud telemetry and to update device-twin reported properties on Azure IoT Hub, using the Azure IoT device SDK. Device connections are pooled and reused per connection string: a message only gets a pooled connection that was opened with the same connection string, including its key, so connection strings of the same device with different keys get their own connections.
Prerequisites
You must have a valid Microsoft Azure IoT Hub and a per-device connection string. More information is available at the Azure IoT Hub Documentation.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-azure-iothub</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Azure IoT Hub component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The configuration that each endpoint copies as its defaults, before the endpoint options are applied. | IoTHubConfiguration | ||
Content encoding applied to telemetry messages. Set it to an empty value to send telemetry without a content encoding, for example for a binary body such as Avro or Protobuf. | UTF-8 | String | |
The content type of the telemetry message, which IoT Hub message routing needs to query the body. The contentEncoding option sets the content encoding. | application/json | String | |
What the producer sends. TELEMETRY sends the body as a device-to-cloud message, TWIN updates the reported properties of the device twin, and BOTH does both. The CamelAzureIoTHubDeliveryMode header replaces this option. Enum values:
| TELEMETRY | IoTHubDeliveryMode | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The protocol of the device client. MQTT_WS and AMQPS_WS are MQTT and AMQPS over WebSocket on port 443. The SDK supports device twin operations only over MQTT, MQTT_WS, AMQPS and AMQPS_WS. Enum values:
| MQTT | IotHubClientProtocol | |
How long the producer waits for IoT Hub to acknowledge a telemetry message, and for each device twin request: getting the twin, updating its reported properties, and the subscription to desired properties when a device client is created. Opening the device client uses the retry policy of the Azure IoT SDK and is not limited by this timeout. | 10s | Duration | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
How often the component checks the pool for device clients that are unused for longer than deviceClientIdleTimeout, and closes them. A value of 0 or less uses 30 seconds. | 30s | Duration | |
How long a pooled device client can be unused before the component closes it. A client is unused when no producer sends with it. A value of 0 or less uses 5 minutes. | 5m | Duration | |
The connection string of the device, with its HostName, DeviceId and SharedAccessKey. The producer opens one device client per hub, device and module of the connection string, and reuses it; an HTTPS endpoint uses a client of its own. The CamelAzureIoTHubConnectionString header replaces this option. | String |
Endpoint Options
The Azure IoT Hub endpoint is configured using URI syntax:
azure-iothub:deviceId
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Content encoding applied to telemetry messages. Set it to an empty value to send telemetry without a content encoding, for example for a binary body such as Avro or Protobuf. | UTF-8 | String | |
The content type of the telemetry message, which IoT Hub message routing needs to query the body. The contentEncoding option sets the content encoding. | application/json | String | |
What the producer sends. TELEMETRY sends the body as a device-to-cloud message, TWIN updates the reported properties of the device twin, and BOTH does both. The CamelAzureIoTHubDeliveryMode header replaces this option. Enum values:
| TELEMETRY | IoTHubDeliveryMode | |
The protocol of the device client. MQTT_WS and AMQPS_WS are MQTT and AMQPS over WebSocket on port 443. The SDK supports device twin operations only over MQTT, MQTT_WS, AMQPS and AMQPS_WS. Enum values:
| MQTT | IotHubClientProtocol | |
How long the producer waits for IoT Hub to acknowledge a telemetry message, and for each device twin request: getting the twin, updating its reported properties, and the subscription to desired properties when a device client is created. Opening the device client uses the retry policy of the Azure IoT SDK and is not limited by this timeout. | 10s | Duration | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The connection string of the device, with its HostName, DeviceId and SharedAccessKey. The producer opens one device client per hub, device and module of the connection string, and reuses it; an HTTPS endpoint uses a client of its own. The CamelAzureIoTHubConnectionString header replaces this option. | String |
Message Headers
The Azure IoT Hub component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelAzureIoTHubDeviceId (producer) Constant: | The ID of the device that sends the message. It replaces the deviceId of the endpoint, unless it is empty. | String | |
CamelAzureIoTHubConnectionString (producer) Constant: | The connection string of the device. It replaces the connectionString option, unless it is empty. | String | |
CamelAzureIoTHubDeliveryMode (producer) Constant: | What the producer sends for this message. It replaces the deliveryMode option. Enum values:
| IoTHubDeliveryMode | |
CamelAzureIoTHubTwinProperties (producer) Constant: | The reported properties to set on the device twin, as a java.util.Map with String keys. TWIN uses it instead of a Map body, and BOTH requires it. The producer sends only the properties whose value differs from the current reported properties. | Map |
Usage
The producer sends the message body to Azure IoT Hub. The delivery mode selects how the body is interpreted:
-
TELEMETRY- the body is sent as a device-to-cloud message, using the payload as abyte[]/String-compatible value. -
TWIN- the body (ajava.util.Map) is applied as device-twin reported properties using a delta update (only changed properties are sent). -
BOTH- sends the message body as telemetry and applies twin reported properties from theCamelAzureIoTHubTwinPropertiesheader.
TWIN and BOTH update the device twin, which the Azure IoT SDK supports only over MQTT, MQTT_WS, AMQPS and AMQPS_WS. With protocol=HTTPS only TELEMETRY works: a producer with deliveryMode TWIN or BOTH fails to start, and a message whose CamelAzureIoTHubDeliveryMode header asks for a twin update fails. An HTTPS endpoint and an MQTT or AMQPS endpoint of the same device each open their own device client.
Any payload decoding (for example Protobuf or CloudEvent to Map) must be done by a preceding processor in the route; this component is payload-agnostic.
Scope and non-goals
This component deliberately covers only the data plane - the per-message act of connecting as an already-provisioned device and sending telemetry or twin updates. It is credential- and payload-agnostic: it expects a ready-to-use per-device connection string (supplied on the endpoint or via the CamelAzureIoTHubConnectionString header) and a payload already in the shape it expects.
| Passing the IoT Hub connection string in a message header is sensitive because it may travel through tracing, logging, or downstream endpoints. Use it only when needed and avoid persisting it in a message body or route metadata. |
The following control plane and application-specific concerns are intentionally out of scope and must be handled by the application or dedicated tooling before a message reaches this component:
-
Device provisioning via the Azure Device Provisioning Service (DPS) - enrollment groups, id scopes, attestation and the registration handshake.
-
Device registry lifecycle - creating, disabling or deleting device identities in IoT Hub.
-
Credential resolution and storage - deriving or retrieving SAS keys, integrating with a secret store such as Azure Key Vault, caching and refresh policies.
-
Payload decoding - converting application formats (for example Protobuf or CloudEvents) into the
byte[]/Stringtelemetry body or theMapof twin reported properties.
This mirrors the other Azure Camel components (for example azure-eventhubs and azure-servicebus), which likewise accept a connection string or token credential and leave resource provisioning to the application. Keeping provisioning out of the component makes it reusable across tenants, avoids coupling a hot data path to a slow one-time control path, and keeps environment-specific security details out of the component.
Example
Send telemetry to a fixed device:
from("direct:telemetry")
.to("azure-iothub:myDevice?deliveryMode=TELEMETRY&connectionString=RAW(HostName=...;DeviceId=myDevice;SharedAccessKey=...)"); Route messages to different devices dynamically using headers:
from("direct:dynamic")
.setHeader("CamelAzureIoTHubDeviceId", simple("${header.deviceId}"))
.setHeader("CamelAzureIoTHubConnectionString", simple("${header.deviceConnectionString}"))
.to("azure-iothub:placeholder?deliveryMode=TWIN"); | The connection string is secret and should not be logged or propagated downstream unless it is strictly required. Prefer configuring it on the endpoint when possible. |