Camel Components

Azure Event Hubs

Since Camel 3.5

Both producer and consumer are supported

The Azure Event Hubs component provides the capability to produce and consume events with Azure Event Hubs using the AMQP protocol. Azure EventHubs is a highly scalable publish-subscribe service that can ingest millions of events per second and stream them to multiple consumers.

Prerequisites

You must have a valid Microsoft Azure Event Hubs account. More information is available at the Azure Documentation Portal.

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-eventhubs</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI Format

azure-eventhubs://[namespace/eventHubName][?options]

When providing a connectionString, the namespace and eventHubName options are not required as they are already included in the connectionString

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, *.yaml files, 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 Event Hubs component supports the following options which are listed below.

Name Description Default Type

amqpRetryOptions (common)

The retry policy (com.azure.core.amqp.AmqpRetryOptions) of the Event Hubs clients that the producer and the consumer create. When not set, the SDK default applies: up to 3 retries, with an exponential delay from 0.8 seconds up to 1 minute, and a timeout of 1 minute per try. Not applied to a producerAsyncClient that you provide.

AmqpRetryOptions

amqpTransportType (common)

The transport of the connection to Event Hubs: AMQP (AMQP over TCP, port 5671) or AMQP_WEB_SOCKETS (AMQP over web sockets, port 443, for networks that block port 5671). Not applied to a producerAsyncClient that you provide.

Enum values:

  • Amqp

  • AmqpWebSockets

AMQP

AmqpTransportType

configuration (common)

The component configurations.

EventHubsConfiguration

blobAccessKey (consumer)

The access key of the Azure Storage account blobAccountName, for the default BlobCheckpointStore. Not used when checkpointStore or blobStorageSharedKeyCredential is set.

String

blobAccountName (consumer)

The name of the Azure Storage account of the default BlobCheckpointStore, used as https://ACCOUNT.blob.core.windows.net with blobAccessKey. Not used when checkpointStore or blobStorageSharedKeyCredential is set.

String

blobContainerName (consumer)

The name of the blob container in which the default BlobCheckpointStore stores the partition ownership and the checkpoints. The container must exist. Required when checkpointStore is not set.

String

blobStorageSharedKeyCredential (consumer)

The com.azure.storage.common.StorageSharedKeyCredential (account name and access key) of the Azure Storage account of the default BlobCheckpointStore, instead of blobAccountName and blobAccessKey.

StorageSharedKeyCredential

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

checkpointBatchSize (consumer)

The number of events of a partition whose exchanges completed successfully, after which the consumer updates the checkpoint of the partition in the checkpoint store, without waiting for checkpointBatchTimeout. A checkpoint never passes an event whose exchange is still in flight; an event whose exchange failed is not received again, and the next checkpoint passes it.

500

int

checkpointBatchTimeout (consumer)

The time in milliseconds after which the consumer updates the checkpoint of a partition for the events completed since the last checkpoint, when checkpointBatchSize is not reached first. The time starts with the first event completed after the last checkpoint.

5000

int

checkpointStore (consumer)

The com.azure.messaging.eventhubs.CheckpointStore in which the EventProcessorClient of the consumer stores the partition ownership and the checkpoints of the consumer group: the consumers of the group share the partitions through it, and a consumer that takes a partition resumes after its last checkpoint. When not set, a BlobCheckpointStore in Azure Blob Storage is created from blobContainerName and blobAccountName with blobAccessKey, or blobStorageSharedKeyCredential.

BlobCheckpointStore

CheckpointStore

consumerGroupName (consumer)

The consumer group that the consumer reads the events in. Each consumer group has its own checkpoints and partition ownership, so the consumers of one group share the partitions. $Default is the consumer group that every event hub has.

$Default

String

eventPosition (consumer)

The position to start reading a partition from when the checkpoint store has no checkpoint for it, as a map from the partition id to a com.azure.messaging.eventhubs.models.EventPosition. A partition with no checkpoint and no entry starts at EventPosition.latest(): only the events enqueued after the consumer starts reading are received.

Map

prefetchCount (consumer)

The number of events that the Event Hubs client receives and queues locally for each partition, ahead of their processing, from 1 to 8000. Note: the consumer does not pass this option to its EventProcessorClient, which uses its own default of 500.

500

int

lazyStartProducer (producer)

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

partitionId (producer)

The id of the partition that the events are sent to. The CamelAzureEventHubsPartitionId header overrides it. When neither a partition id nor a partition key is set, Event Hubs chooses the partition. Sending fails when a partition key and a partition id are both set.

String

partitionKey (producer)

The partition key of the events: Event Hubs hashes it to choose the partition, so the events with the same key go to the same partition. The CamelAzureEventHubsPartitionKey header overrides it. Use it to keep related events together; use partitionId to send to one partition. Sending fails when a partition key and a partition id are both set.

String

producerAsyncClient (producer)

Autowired The com.azure.messaging.eventhubs.EventHubProducerAsyncClient that the producer sends the events with, instead of a client that it creates. The producer does not close it, and the credential, transport and retry options do not apply to it. Not used by the consumer.

EventHubProducerAsyncClient

autowiredEnabled (advanced)

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

headerFilterStrategy (filter)

To use a custom org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel message.

HeaderFilterStrategy

connectionString (security)

The connection string of the Event Hubs namespace or of the event hub, with the endpoint, the shared access key name and the key. It must contain EntityPath=NAME with the name of the event hub: add it to a connection string copied from the namespace. Used when credentialType is CONNECTION_STRING; it replaces the namespace and the event hub name of the URI path, sharedAccessName and sharedAccessKey.

String

credentialType (security)

How to authenticate: CONNECTION_STRING with connectionString, or with sharedAccessName and sharedAccessKey; TOKEN_CREDENTIAL with the tokenCredential option; AZURE_IDENTITY with a DefaultAzureCredential, the tokenCredential option when it is set, otherwise a new one. TOKEN_CREDENTIAL and AZURE_IDENTITY need the namespace and the event hub name of the URI path. When not set, it is TOKEN_CREDENTIAL (or AZURE_IDENTITY for a DefaultAzureCredential) when tokenCredential is set, else CONNECTION_STRING.

Enum values:

  • AZURE_IDENTITY

  • CONNECTION_STRING

  • TOKEN_CREDENTIAL

CONNECTION_STRING

CredentialType

sharedAccessKey (security)

The primary or secondary key of the shared access policy named by sharedAccessName.

String

sharedAccessName (security)

The name of the shared access policy (SAS key name) of the namespace or the event hub. With sharedAccessKey, the namespace and the event hub name of the URI path, it builds the connection string when credentialType is CONNECTION_STRING and connectionString is not set.

String

tokenCredential (security)

Autowired The com.azure.core.credential.TokenCredential to authenticate with Microsoft Entra ID, for credentialType TOKEN_CREDENTIAL or AZURE_IDENTITY, with the namespace and the event hub name of the URI path. A DefaultAzureCredential built with a managed identity client id or a tenant id is used as it is.

TokenCredential

Endpoint Options

The Azure Event Hubs endpoint is configured using URI syntax:

azure-eventhubs:namespace/eventHubName

With the following path and query parameters:

Path Parameters

Name Description Default Type

namespace (common)

The Event Hubs namespace, the first part of its host name NAMESPACE.servicebus.windows.net. The URI path is ignored when connectionString or producerAsyncClient is set and tokenCredential is not.

String

eventHubName (common)

The name of the event hub in the namespace. The URI path is ignored when connectionString or producerAsyncClient is set and tokenCredential is not.

String

Query Parameters

Name Description Default Type

amqpRetryOptions (common)

The retry policy (com.azure.core.amqp.AmqpRetryOptions) of the Event Hubs clients that the producer and the consumer create. When not set, the SDK default applies: up to 3 retries, with an exponential delay from 0.8 seconds up to 1 minute, and a timeout of 1 minute per try. Not applied to a producerAsyncClient that you provide.

AmqpRetryOptions

amqpTransportType (common)

The transport of the connection to Event Hubs: AMQP (AMQP over TCP, port 5671) or AMQP_WEB_SOCKETS (AMQP over web sockets, port 443, for networks that block port 5671). Not applied to a producerAsyncClient that you provide.

Enum values:

  • Amqp

  • AmqpWebSockets

AMQP

AmqpTransportType

blobAccessKey (consumer)

The access key of the Azure Storage account blobAccountName, for the default BlobCheckpointStore. Not used when checkpointStore or blobStorageSharedKeyCredential is set.

String

blobAccountName (consumer)

The name of the Azure Storage account of the default BlobCheckpointStore, used as https://ACCOUNT.blob.core.windows.net with blobAccessKey. Not used when checkpointStore or blobStorageSharedKeyCredential is set.

String

blobContainerName (consumer)

The name of the blob container in which the default BlobCheckpointStore stores the partition ownership and the checkpoints. The container must exist. Required when checkpointStore is not set.

String

blobStorageSharedKeyCredential (consumer)

The com.azure.storage.common.StorageSharedKeyCredential (account name and access key) of the Azure Storage account of the default BlobCheckpointStore, instead of blobAccountName and blobAccessKey.

StorageSharedKeyCredential

checkpointBatchSize (consumer)

The number of events of a partition whose exchanges completed successfully, after which the consumer updates the checkpoint of the partition in the checkpoint store, without waiting for checkpointBatchTimeout. A checkpoint never passes an event whose exchange is still in flight; an event whose exchange failed is not received again, and the next checkpoint passes it.

500

int

checkpointBatchTimeout (consumer)

The time in milliseconds after which the consumer updates the checkpoint of a partition for the events completed since the last checkpoint, when checkpointBatchSize is not reached first. The time starts with the first event completed after the last checkpoint.

5000

int

checkpointStore (consumer)

The com.azure.messaging.eventhubs.CheckpointStore in which the EventProcessorClient of the consumer stores the partition ownership and the checkpoints of the consumer group: the consumers of the group share the partitions through it, and a consumer that takes a partition resumes after its last checkpoint. When not set, a BlobCheckpointStore in Azure Blob Storage is created from blobContainerName and blobAccountName with blobAccessKey, or blobStorageSharedKeyCredential.

BlobCheckpointStore

CheckpointStore

consumerGroupName (consumer)

The consumer group that the consumer reads the events in. Each consumer group has its own checkpoints and partition ownership, so the consumers of one group share the partitions. $Default is the consumer group that every event hub has.

$Default

String

eventPosition (consumer)

The position to start reading a partition from when the checkpoint store has no checkpoint for it, as a map from the partition id to a com.azure.messaging.eventhubs.models.EventPosition. A partition with no checkpoint and no entry starts at EventPosition.latest(): only the events enqueued after the consumer starts reading are received.

Map

prefetchCount (consumer)

The number of events that the Event Hubs client receives and queues locally for each partition, ahead of their processing, from 1 to 8000. Note: the consumer does not pass this option to its EventProcessorClient, which uses its own default of 500.

500

int

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

partitionId (producer)

The id of the partition that the events are sent to. The CamelAzureEventHubsPartitionId header overrides it. When neither a partition id nor a partition key is set, Event Hubs chooses the partition. Sending fails when a partition key and a partition id are both set.

String

partitionKey (producer)

The partition key of the events: Event Hubs hashes it to choose the partition, so the events with the same key go to the same partition. The CamelAzureEventHubsPartitionKey header overrides it. Use it to keep related events together; use partitionId to send to one partition. Sending fails when a partition key and a partition id are both set.

String

producerAsyncClient (producer)

Autowired The com.azure.messaging.eventhubs.EventHubProducerAsyncClient that the producer sends the events with, instead of a client that it creates. The producer does not close it, and the credential, transport and retry options do not apply to it. Not used by the consumer.

EventHubProducerAsyncClient

lazyStartProducer (producer (advanced))

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

connectionString (security)

The connection string of the Event Hubs namespace or of the event hub, with the endpoint, the shared access key name and the key. It must contain EntityPath=NAME with the name of the event hub: add it to a connection string copied from the namespace. Used when credentialType is CONNECTION_STRING; it replaces the namespace and the event hub name of the URI path, sharedAccessName and sharedAccessKey.

String

credentialType (security)

How to authenticate: CONNECTION_STRING with connectionString, or with sharedAccessName and sharedAccessKey; TOKEN_CREDENTIAL with the tokenCredential option; AZURE_IDENTITY with a DefaultAzureCredential, the tokenCredential option when it is set, otherwise a new one. TOKEN_CREDENTIAL and AZURE_IDENTITY need the namespace and the event hub name of the URI path. When not set, it is TOKEN_CREDENTIAL (or AZURE_IDENTITY for a DefaultAzureCredential) when tokenCredential is set, else CONNECTION_STRING.

Enum values:

  • AZURE_IDENTITY

  • CONNECTION_STRING

  • TOKEN_CREDENTIAL

CONNECTION_STRING

CredentialType

sharedAccessKey (security)

The primary or secondary key of the shared access policy named by sharedAccessName.

String

sharedAccessName (security)

The name of the shared access policy (SAS key name) of the namespace or the event hub. With sharedAccessKey, the namespace and the event hub name of the URI path, it builds the connection string when credentialType is CONNECTION_STRING and connectionString is not set.

String

tokenCredential (security)

Autowired The com.azure.core.credential.TokenCredential to authenticate with Microsoft Entra ID, for credentialType TOKEN_CREDENTIAL or AZURE_IDENTITY, with the namespace and the event hub name of the URI path. A DefaultAzureCredential built with a managed identity client id or a tenant id is used as it is.

TokenCredential

Message Headers

The Azure Event Hubs component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelAzureEventHubsPartitionKey (common)

Constant: PARTITION_KEY

(producer) The partition key of the events, which Event Hubs hashes to choose the partition; it overrides the partitionKey option. A header that still holds the partition key of the event received by an azure-eventhubs consumer is not taken as set by the route. (consumer) The partition key that the event was published with, if it has one.

String

CamelAzureEventHubsPartitionId (common)

Constant: PARTITION_ID

(producer) The id of the partition to send the events to; it overrides the partitionId option. A header that still holds the partition id of the event received by an azure-eventhubs consumer is not taken as set by the route. (consumer) The id of the partition that the event was received from.

String

CamelAzureEventHubsOffset (consumer)

Constant: OFFSET

The offset of the event in its partition, from the deprecated EventData.getOffset(): a Long, or null when the offset is not a number.

Integer

CamelAzureEventHubsEnqueuedTime (consumer)

Constant: ENQUEUED_TIME

The instant (in UTC) at which the event was enqueued in its partition.

Instant

CamelAzureEventHubsSequenceNumber (consumer)

Constant: SEQUENCE_NUMBER

The sequence number that Event Hubs assigned to the event when it was enqueued, unique within its partition.

Long

CamelAzureEventHubsMetadata (consumer)

Constant: METADATA

The application properties of the event (EventData.getProperties()), as one map. The azure-eventhubs producer sends the message headers as these properties.

Map

CamelMessageTimestamp (consumer)

Constant: MESSAGE_TIMESTAMP

The time at which the event was enqueued, in milliseconds since the epoch, with second precision (the last three digits are always 0).

long

CamelAzureEventHubsCheckpointUpdatedBy (consumer)

Constant: CHECKPOINT_UPDATED_BY

Why the checkpoint of the partition was updated: size when the completion of this exchange reached checkpointBatchSize, timeout when checkpointBatchTimeout had passed. Set when the exchange completes, after the route, so only completion callbacks see it; not set when neither applies.

String

Message Body

The message body at each side of the Azure Event Hubs component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

bytes (byte[])

The body of the event, one message per event.

Producer input

text, bytes, stream, file or list

What converts to a byte array: text, bytes, a stream or a file, sent as one event; a Map, a number or a POJO fails: marshal it first. A List is sent as one event per element, in one batch, and an element that is an Exchange or a Message is sent with its headers.

Producer output

unchanged

Unchanged: a send gives no result, and no header is set.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Deterministic simulation tests

The consumer of the Azure Event Hubs component is tested with deterministic simulation tests: the real consumer runs under a seeded virtual-time scheduler against a fake of the external system, which injects faults while the tests check invariants. A failing seed can be replayed. These are the fake worlds of the component:

World What it checks Issues

EventHubsWorld

Fake: a mocked EventHubsClientFactory that delivers the events per partition, and a fake CheckpointStore

Tests: EventHubsCheckpointDstTest

Covers:

  • checkpointing by checkpointBatchSize and checkpointBatchTimeout

  • one or two partitions with bursts and quiet periods of events

  • an asynchronous downstream step

Faults:

  • the downstream step answers slowly

  • the downstream step fails

  • the checkpoint store answers slowly

  • the checkpoint store fails

Invariants:

  • every completed message is acknowledged when the partition goes quiet

  • a stored checkpoint never passes an event still in flight

  • the stored checkpoint of a partition never goes back

The worlds and their tests are in the test sources of the component. How to run them, replay a seed and write a world is described in Running the tests and Writing a fake world.

Usage

Authentication Information

There are three different Credential Types: AZURE_IDENTITY, TOKEN_CREDENTIAL and CONNECTION_STRING.

CONNECTION_STRING:

You can either:

  • Provide the connectionString option. Using this options means that you don’t need to specify additional options namespace, eventHubName, sharedAccessKey and sharedAccessName , as this data is already included within the connectionString. Learn more here on how to generate the connection string.

Or

  • Provide sharedAccessName and sharedAccessKey options for your Azure Event Hubs account. The sharedAccessKey can be generated through the Event Hubs Azure portal. The connection String will then be generated automatically for you by the azure-eventhubs component.

TOKEN_CREDENTIAL:

  • Bind an implementation of com.azure.core.credential.TokenCredential to the Camel Registry (see example below). See the documentation here about Azure-AD authentication.

AZURE_IDENTITY: - This will use an com.azure.identity.DefaultAzureCredentialBuilder().build() instance. This will follow the Default Azure Credential Chain. See the documentation here about Azure-AD authentication.

Client instance:

  • Provide a EventHubProducerAsyncClient instance which can be used for the producerAsyncClient option. However, this is only supported for azure-eventhubs producer, for the consumer, it is not possible to inject the client due to some design constraints in the EventProcessorClient.

Checkpoint Store Information

A checkpoint store, stores and retrieves partition ownership information and checkpoint details for each partition in a given consumer group of an event hub instance. Users are not meant to implement a CheckpointStore. Users are expected to choose existing implementations of this interface, instantiate it, and pass it to the component through the checkpointStore option.

When no CheckpointStore implementation is provided, the azure-eventhubs component will fall back to use BlobCheckpointStore to store the checkpoint information in the Azure Blob Storage account. If you chose to use the default BlobCheckpointStore, you will need to supply the following options:

  • blobAccountName: The Azure account name to be used for authentication with azure blob services.

  • blobAccessKey: The access key for the associated azure account name to be used for authentication with azure blob services.

  • blobContainerName: The name of the blob container that shall be used by the BlobCheckpointStore to store the checkpoint offsets.

Message body type

The azure-eventhubs producer expects the data in the message body to be of type byte[]. This allows the simple messages (E.g. String based ones) to be marshalled /unmarshalled with ease. The same is true for the azure-eventhubs consumer, it will set the encoded data as byte[] in the message body.

Automatic detection of EventHubProducerAsyncClient client in the Camel registry

The component is capable of detecting the presence of an EventHubProducerAsyncClient bean into the registry. If it’s the only instance of that type, it will be used as the client, and you won’t have to define it as uri parameter, like the example above. This may be really useful for smarter configuration of the endpoint.

Examples

Consumer Example

To consume events:

  • Java

  • XML

  • YAML

from("azure-eventhubs:/camel/camelHub?sharedAccessName=SASaccountName&sharedAccessKey=SASaccessKey&blobAccountName=accountName&blobAccessKey=accessKey&blobContainerName=containerName")
    .to("file://queuedirectory");
<route>
  <from uri="azure-eventhubs:/camel/camelHub?sharedAccessName=SASaccountName&amp;sharedAccessKey=SASaccessKey&amp;blobAccountName=accountName&amp;blobAccessKey=accessKey&amp;blobContainerName=containerName"/>
  <to uri="file://queuedirectory"/>
</route>
- route:
    from:
      uri: azure-eventhubs:/camel/camelHub
      parameters:
        sharedAccessName: SASaccountName
        sharedAccessKey: SASaccessKey
        blobAccountName: accountName
        blobAccessKey: accessKey
        blobContainerName: containerName
      steps:
        - to:
            uri: file://queuedirectory

Producer Example

To produce events:

Java-only: producing events with a Processor and EventHubsConstants
from("direct:start")
    .process(exchange -> {
        exchange.getIn().setHeader(EventHubsConstants.PARTITION_ID, firstPartition);
        exchange.getIn().setBody("test event");
    })
    .to("azure-eventhubs:?connectionString=RAW({{connectionString}})");

The azure-eventhubs producer supports sending events as an Iterable (E.g. as a List). For example:

Java-only: producing batch events with an Iterable body
from("direct:start")
    .process(exchange -> {
        final List<String> messages = new LinkedList<>();
        messages.add("Test String Message 1");
        messages.add("Test String Message 2");

        exchange.getIn().setHeader(EventHubsConstants.PARTITION_ID, firstPartition);
        exchange.getIn().setBody(messages);
    })
    .to("azure-eventhubs:?connectionString=RAW({{connectionString}})");

The producer sends the events to the partition chosen, in this order, by:

  1. a CamelAzureEventHubsPartitionKey or CamelAzureEventHubsPartitionId header set by the route

  2. the partitionKey or partitionId endpoint option

  3. the partition key of the event received by an azure-eventhubs consumer earlier in the route, so that events with the same key stay together

Otherwise, Event Hubs picks the partition. The partition key and the partition id are looked up separately, so a partition id header set by the route together with a partitionKey endpoint option, or a partition key header together with a partitionId option, selects both, and the producer fails with Both partitionKey and partitionId are set.

The consumer sets the same two headers on every event it receives. While they still hold the received values, the producer does not take them as a partition chosen by the route, and it never reuses the partition id of a received event, because partitions differ from one Event Hub to another.

To keep the partition of the received event, for example when mirroring between Event Hubs that have the same partitions, remove the CamelAzureEventHubsReceivedPartitionId exchange property, or CamelAzureEventHubsReceivedPartitionKey for the partition key, before the producer, such as with .removeProperty(EventHubsConstants.RECEIVED_PARTITION_ID). The header then counts as chosen by the route.

Azure-AD Authentication example

The example below makes use of the Azure-AD authentication. See here about what environment variables you need to set for this to work:

Java-only: registering a TokenCredential bean and using Azure-AD authentication
@BindToRegistry("myTokenCredential")
public com.azure.core.credential.TokenCredential myTokenCredential() {
    return new com.azure.identity.DefaultAzureCredentialBuilder().build();
}

from("direct:start")
    .to("azure-eventhubs:namespace/eventHubName?tokenCredential=#myTokenCredential&credentialType=TOKEN_CREDENTIAL");

Important Development Notes

When developing on this component, in addition to the mocked unit tests, you will need to run the integration tests with every change you make or even client upgrade as the Azure client can break things even on minor versions upgrade. The integration tests run against a local Floci AZ container, which needs Docker. To run them, on this component directory, run the following maven command:

mvn verify

To run the integration tests against an event hub in Azure, run the following maven command instead:

mvn verify -DconnectionString=string -DblobAccountName=blob -DblobAccessKey=key

Whereby blobAccountName is your Azure account name and blobAccessKey is the access key being generated from Azure portal and connectionString is the eventHub connection string.