Class EventHubClientBuilder

java.lang.Object
com.azure.messaging.eventhubs.EventHubClientBuilder
All Implemented Interfaces:
AmqpTrait<EventHubClientBuilder>, AzureNamedKeyCredentialTrait<EventHubClientBuilder>, AzureSasCredentialTrait<EventHubClientBuilder>, ConfigurationTrait<EventHubClientBuilder>, ConnectionStringTrait<EventHubClientBuilder>, TokenCredentialTrait<EventHubClientBuilder>

This class provides a fluent builder API to aid the instantiation of EventHubProducerAsyncClient, EventHubProducerClient, EventHubConsumerAsyncClient, and EventHubConsumerClient. Calling any of the .build*Client() methods will create an instance of the respective client.

Credentials are required to perform operations against Azure Event Hubs. They can be set by using one of the following methods:

In addition, the consumer group is required when creating EventHubConsumerAsyncClient or EventHubConsumerClient.

Creating an asynchronous EventHubProducerAsyncClient using Event Hubs namespace connection string

In the sample, the namespace connection string is used to create an asynchronous Event Hub producer. Notice that "EntityPath" is not a component in the connection string.

 // The required parameter is a way to authenticate with Event Hubs using credentials.
 // The connectionString provides a way to authenticate with Event Hub.
 EventHubProducerAsyncClient producer = new EventHubClientBuilder()
     .connectionString(
         "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}",
         "event-hub-name")
     .buildAsyncProducerClient();
 

Creating a synchronous EventHubConsumerClient using an Event Hub instance connection string

In the sample, the namespace connection string is used to create a synchronous Event Hub consumer. Notice that "EntityPath" is in the connection string.

 // The required parameters are `consumerGroup`, and a way to authenticate with Event Hubs using credentials.
 EventHubConsumerClient consumer = new EventHubClientBuilder()
     .connectionString("Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};"
         + "SharedAccessKey={key};Entity-Path={hub-name}")
     .consumerGroup("$DEFAULT")
     .buildConsumerClient();
 

Creating producers and consumers that share the same connection

By default, a dedicated connection is created for each producer and consumer created from the builder. If users wish to use the same underlying connection, they can toggle shareConnection().

 // Toggling `shareConnection` instructs the builder to use the same underlying connection
 // for each consumer or producer created using the same builder instance.
 EventHubClientBuilder builder = new EventHubClientBuilder()
     .connectionString("event-hubs-instance-connection-string")
     .shareConnection();

 // Both the producer and consumer created share the same underlying connection.
 EventHubProducerAsyncClient producer = builder.buildAsyncProducerClient();
 EventHubConsumerAsyncClient consumer = builder
     .consumerGroup("my-consumer-group")
     .buildAsyncConsumerClient();
 
See Also: