EventHubClientBuilder Class
- java.
lang. Object - com.
azure. messaging. eventhubs. EventHubClientBuilder
- com.
Implements
public class EventHubClientBuilder
implements TokenCredentialTrait<EventHubClientBuilder>, AzureNamedKeyCredentialTrait<EventHubClientBuilder>, ConnectionStringTrait<EventHubClientBuilder>, AzureSasCredentialTrait<EventHubClientBuilder>, AmqpTrait<EventHubClientBuilder>, ConfigurationTrait<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:
- connectionString(String connectionString) with a connection string to a specific Event Hub.
- connectionString(String connectionString, String eventHubName) with an Event Hub namespace connection string and the Event Hub name.
- credential(String fullyQualifiedNamespace, String eventHubName, TokenCredential credential) with the fully qualified namespace, Event Hub name, and a set of credentials authorized to use the Event Hub.
- credential(String fullyQualifiedNamespace, String eventHubName, AzureSasCredential credential) with the fully qualified namespace, Event Hub name, and a shared access signature for the Event Hub.
- credential(String fullyQualifiedNamespace, String eventHubName, AzureNamedKeyCredential credential) with the fully qualified namespace, Event Hub name, and a named key credential. The named key can be found in the Azure Portal by navigating to the Event Hub resource, selecting "Shared access policies" under the Settings section.
- credential(TokenCredential credential), credential(AzureSasCredential credential), and credential(AzureNamedKeyCredential credential) overloads can be used with its respective credentials. fullyQualifiedNamespace(String fullyQualifiedNamespace) and eventHubName(String eventHubName) must be set as well.
In addition, the consumerGroup(String consumerGroup) is required when creating EventHubConsumerAsyncClient or EventHubConsumerClient.
The credential used in the following samples is DefaultAzureCredential
for authentication. It is appropriate for most scenarios, including local development and production environments. Additionally, we recommend using managed identity for authentication in production environments. You can find more information on different ways of authenticating and their corresponding credential types in the Azure Identity documentation".
Sample: Construct a EventHubProducerAsyncClient
The following code sample demonstrates the creation of the asynchronous client EventHubProducerAsyncClient. The fullyQualifiedNamespace
is the Event Hubs Namespace's host name. It is listed under the "Essentials" panel after navigating to the Event Hubs Namespace via Azure Portal. The credential used is DefaultAzureCredential
because it combines commonly used credentials in deployment and development and chooses the credential to used based on its running environment.
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// "<<fully-qualified-namespace>>" will look similar to "{your-namespace}.servicebus.windows.net"
// "<<event-hub-name>>" will be the name of the Event Hub instance you created inside the Event Hubs namespace.
EventHubProducerAsyncClient producer = new EventHubClientBuilder()
.credential("<<fully-qualified-namespace>>", "<<event-hub-name>>",
credential)
.buildAsyncProducerClient();
Sample: Construct a EventHubConsumerAsyncClient
The following code sample demonstrates the creation of the asynchronous client EventHubConsumerAsyncClient. The fullyQualifiedNamespace
is the Event Hubs Namespace's host name. It is listed under the "Essentials" panel after navigating to the Event Hubs Namespace via Azure Portal. The consumerGroup
is found by navigating to the Event Hub instance, and selecting "Consumer groups" under the "Entities" panel. The consumerGroup(String consumerGroup) is required for creating consumer clients. The credential used is DefaultAzureCredential
because it combines commonly used credentials in deployment and development and chooses the credential to used based on its running environment.
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// "<<fully-qualified-namespace>>" will look similar to "{your-namespace}.servicebus.windows.net"
// "<<event-hub-name>>" will be the name of the Event Hub instance you created inside the Event Hubs namespace.
EventHubConsumerAsyncClient consumer = new EventHubClientBuilder()
.credential("<<fully-qualified-namespace>>", "<<event-hub-name>>",
credential)
.consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
.buildAsyncConsumerClient();
Sample: Creating a client using web sockets and custom retry options
By default, the AMQP port 5671 is used, but clients can use web sockets, port 443. Customers can replace the default retry options with their own policy. The retry options are used when recovering from transient failures in the underlying AMQP connection and performing any operations that require a response from the service.
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
AmqpRetryOptions customRetryOptions = new AmqpRetryOptions()
.setMaxRetries(5)
.setMode(AmqpRetryMode.FIXED)
.setTryTimeout(Duration.ofSeconds(60));
// "<<fully-qualified-namespace>>" will look similar to "{your-namespace}.servicebus.windows.net"
// "<<event-hub-name>>" will be the name of the Event Hub instance you created inside the Event Hubs namespace.
EventHubProducerClient producer = new EventHubClientBuilder()
.credential("<<fully-qualified-namespace>>", "<<event-hub-name>>",
credential)
.transportType(AmqpTransportType.AMQP_WEB_SOCKETS)
.buildProducerClient();
Sample: 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(). This underlying connection is closed when all clients created from this builder instance are disposed.
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// "<<fully-qualified-namespace>>" will look similar to "{your-namespace}.servicebus.windows.net"
// "<<event-hub-name>>" will be the name of the Event Hub instance you created inside the Event Hubs namespace.
EventHubClientBuilder builder = new EventHubClientBuilder()
.credential("<<fully-qualified-namespace>>", "<<event-hub-name>>",
credential)
.shareConnection();
// Both the producer and consumer created share the same underlying connection.
EventHubProducerAsyncClient producer = builder.buildAsyncProducerClient();
EventHubConsumerAsyncClient consumer = builder
.consumerGroup("my-consumer-group")
.buildAsyncConsumerClient();
Field Summary
Modifier and Type | Field and Description |
---|---|
static final String |
DEFAULT_CONSUMER_GROUP_NAME
The name of the default consumer group in the Event Hubs service. |
Constructor Summary
Constructor | Description |
---|---|
EventHubClientBuilder() |
Creates a new instance with the default transport AMQP and a non-shared connection. |
Method Summary
Modifier and Type | Method and Description |
---|---|
Event |
buildAsyncConsumerClient()
Creates a new EventHubConsumerAsyncClient based on the options set on this builder. |
Event |
buildAsyncProducerClient()
Creates a new EventHubProducerAsyncClient based on options set on this builder. |
Event |
buildConsumerClient()
Creates a new EventHubConsumerClient based on the options set on this builder. |
Event |
buildProducerClient()
Creates a new EventHubProducerClient based on options set on this builder. |
Event |
clientOptions(ClientOptions clientOptions)
Sets the client options. |
Event |
configuration(Configuration configuration)
Sets the configuration store that is used during construction of the service client. |
Event |
connectionString(String connectionString)
Sets the credential information given a connection string to the Event Hub instance or the Event Hubs namespace. |
Event |
connectionString(String connectionString, String eventHubName)
Sets the credential information given a connection string to the Event Hubs namespace and name to a specific Event Hub instance. |
Event |
consumerGroup(String consumerGroup)
Sets the name of the consumer group this consumer is associated with. |
Event |
credential(AzureNamedKeyCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it. |
Event |
credential(AzureSasCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it. |
Event |
credential(TokenCredential credential)
Sets the TokenCredential used to authorize requests sent to the service. |
Event |
credential(String fullyQualifiedNamespace, String eventHubName, AzureNamedKeyCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it. |
Event |
credential(String fullyQualifiedNamespace, String eventHubName, AzureSasCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it. |
Event |
credential(String fullyQualifiedNamespace, String eventHubName, TokenCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it. |
Event |
customEndpointAddress(String customEndpointAddress)
Sets a custom endpoint address when connecting to the Event Hubs service. |
Event |
eventHubName(String eventHubName)
Sets the name of the Event Hub to connect the client to. |
Event |
fullyQualifiedNamespace(String fullyQualifiedNamespace)
Sets the fully qualified name for the Event Hubs namespace. |
Event |
prefetchCount(int prefetchCount)
Sets the count used by the receiver to control the number of events per partition the Event Hub consumer will actively receive and queue locally without regard to whether a receive operation is currently active. |
Event |
proxyOptions(ProxyOptions proxyOptions)
Sets the proxy configuration to use for EventHubAsyncClient. |
Event |
retry(AmqpRetryOptions retryOptions)
Deprecated
Replaced by retryOptions(AmqpRetryOptions retryOptions).
Sets the retry policy for EventHubAsyncClient. |
Event |
retryOptions(AmqpRetryOptions retryOptions)
Sets the retry policy for EventHubAsyncClient. |
Event |
shareConnection()
Toggles the builder to use the same connection for producers or consumers that are built from this instance. |
Event |
transportType(AmqpTransportType transport)
Sets the transport type by which all the communication with Azure Event Hubs occurs. |
Methods inherited from java.lang.Object
Field Details
DEFAULT_CONSUMER_GROUP_NAME
public static final String DEFAULT_CONSUMER_GROUP_NAME
The name of the default consumer group in the Event Hubs service.
Constructor Details
EventHubClientBuilder
public EventHubClientBuilder()
Creates a new instance with the default transport AMQP and a non-shared connection. A non-shared connection means that a dedicated AMQP connection is created for every Event Hub consumer or producer created using the builder.
Method Details
buildAsyncConsumerClient
public EventHubConsumerAsyncClient buildAsyncConsumerClient()
Creates a new EventHubConsumerAsyncClient based on the options set on this builder. Every time buildAsyncConsumer()
is invoked, a new instance of EventHubConsumerAsyncClient is created.
Returns:
buildAsyncProducerClient
public EventHubProducerAsyncClient buildAsyncProducerClient()
Creates a new EventHubProducerAsyncClient based on options set on this builder. Every time buildAsyncProducer()
is invoked, a new instance of EventHubProducerAsyncClient is created.
Returns:
buildConsumerClient
public EventHubConsumerClient buildConsumerClient()
Creates a new EventHubConsumerClient based on the options set on this builder. Every time buildConsumer()
is invoked, a new instance of EventHubConsumerClient is created.
Returns:
buildProducerClient
public EventHubProducerClient buildProducerClient()
Creates a new EventHubProducerClient based on options set on this builder. Every time buildAsyncProducer()
is invoked, a new instance of EventHubProducerClient is created.
Returns:
clientOptions
public EventHubClientBuilder clientOptions(ClientOptions clientOptions)
Sets the client options.
Parameters:
Returns:
configuration
public EventHubClientBuilder configuration(Configuration configuration)
Sets the configuration store that is used during construction of the service client.
If not specified, the default configuration store is used to configure the EventHubAsyncClient. Use NONE to bypass using configuration settings during construction.
Parameters:
Returns:
connectionString
public EventHubClientBuilder connectionString(String connectionString)
Sets the credential information given a connection string to the Event Hub instance or the Event Hubs namespace.
If the connection string is copied from the Event Hubs namespace, it will likely not contain the name to the desired Event Hub, which is needed. In this case, the name can be added manually by adding "EntityPath=EVENT_HUB_NAME" to the end of the connection string. For example, "EntityPath=telemetry-hub".
If you have defined a shared access policy directly on the Event Hub itself, then copying the connection string from that Event Hub will result in a connection string that contains the name.
Parameters:
Returns:
connectionString
public EventHubClientBuilder connectionString(String connectionString, String eventHubName)
Sets the credential information given a connection string to the Event Hubs namespace and name to a specific Event Hub instance.
Parameters:
Returns:
consumerGroup
public EventHubClientBuilder consumerGroup(String consumerGroup)
Sets the name of the consumer group this consumer is associated with. Events are read in the context of this group. The name of the consumer group that is created by default is DEFAULT_CONSUMER_GROUP_NAME.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(AzureNamedKeyCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(AzureSasCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(TokenCredential credential)
Sets the TokenCredential used to authorize requests sent to the service. Refer to the Azure SDK for Java identity and authentication documentation for more details on proper usage of the TokenCredential type.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, AzureNamedKeyCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, AzureSasCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.
Parameters:
Returns:
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, TokenCredential credential)
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.
Parameters:
Returns:
customEndpointAddress
public EventHubClientBuilder customEndpointAddress(String customEndpointAddress)
Sets a custom endpoint address when connecting to the Event Hubs service. This can be useful when your network does not allow connecting to the standard Azure Event Hubs endpoint address, but does allow connecting through an intermediary. For example: https://my.custom.endpoint.com:55300.
If no port is specified, the default port for the transportType(AmqpTransportType transport) is used.
Parameters:
Returns:
eventHubName
public EventHubClientBuilder eventHubName(String eventHubName)
Sets the name of the Event Hub to connect the client to.
Parameters:
Returns:
fullyQualifiedNamespace
public EventHubClientBuilder fullyQualifiedNamespace(String fullyQualifiedNamespace)
Sets the fully qualified name for the Event Hubs namespace.
Parameters:
Returns:
prefetchCount
public EventHubClientBuilder prefetchCount(int prefetchCount)
Sets the count used by the receiver to control the number of events per partition the Event Hub consumer will actively receive and queue locally without regard to whether a receive operation is currently active.
Parameters:
Returns:
proxyOptions
public EventHubClientBuilder proxyOptions(ProxyOptions proxyOptions)
Sets the proxy configuration to use for EventHubAsyncClient. When a proxy is configured, AMQP_WEB_SOCKETS must be used for the transport type.
Parameters:
Returns:
retry
@Deprecated
public EventHubClientBuilder retry(AmqpRetryOptions retryOptions)
Deprecated
Sets the retry policy for EventHubAsyncClient. If not specified, the default retry options are used.
Parameters:
Returns:
retryOptions
public EventHubClientBuilder retryOptions(AmqpRetryOptions retryOptions)
Sets the retry policy for EventHubAsyncClient. If not specified, the default retry options are used.
Parameters:
Returns:
shareConnection
public EventHubClientBuilder shareConnection()
Toggles the builder to use the same connection for producers or consumers that are built from this instance. By default, a new connection is constructed and used created for each Event Hub consumer or producer created.
Returns:
transportType
public EventHubClientBuilder transportType(AmqpTransportType transport)
Sets the transport type by which all the communication with Azure Event Hubs occurs. Default value is AMQP.
Parameters:
Returns:
Applies to
Azure SDK for Java