Class EventHubClientBuilder
- All Implemented Interfaces:
com.azure.core.amqp.client.traits.AmqpTrait<EventHubClientBuilder>,com.azure.core.client.traits.AzureNamedKeyCredentialTrait<EventHubClientBuilder>,com.azure.core.client.traits.AzureSasCredentialTrait<EventHubClientBuilder>,com.azure.core.client.traits.ConfigurationTrait<EventHubClientBuilder>,com.azure.core.client.traits.ConnectionStringTrait<EventHubClientBuilder>,com.azure.core.client.traits.TokenCredentialTrait<EventHubClientBuilder>
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)with a connection string to a specific Event Hub.connectionString(String, String)with an Event Hub namespace connection string and the Event Hub name.credential(String, String, TokenCredential)with the fully qualified namespace, Event Hub name, and a set of credentials authorized to use the Event Hub.credential(String, String, AzureSasCredential)with the fully qualified namespace, Event Hub name, and a shared access signature for the Event Hub.credential(String, String, AzureNamedKeyCredential)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(AzureSasCredential), andcredential(AzureNamedKeyCredential)overloads can be used with its respective credentials.fullyQualifiedNamespace(String)andeventHubName(String)must be set as well.
In addition, the consumerGroup(String) 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) 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();
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of the default consumer group in the Event Hubs service. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance with the default transportAmqpTransportType.AMQPand a non-shared connection. -
Method Summary
Modifier and TypeMethodDescriptionCreates a newEventHubConsumerAsyncClientbased on the options set on this builder.Creates a newEventHubProducerAsyncClientbased on options set on this builder.Creates a newEventHubConsumerClientbased on the options set on this builder.Creates a newEventHubProducerClientbased on options set on this builder.clientOptions(com.azure.core.util.ClientOptions clientOptions) Sets the client options.configuration(com.azure.core.util.Configuration configuration) Sets the configuration store that is used during construction of the service client.connectionString(String connectionString) Sets the credential information given a connection string to the Event Hub instance or the Event Hubs namespace.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.consumerGroup(String consumerGroup) Sets the name of the consumer group this consumer is associated with.credential(com.azure.core.credential.AzureNamedKeyCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.credential(com.azure.core.credential.AzureSasCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.credential(com.azure.core.credential.TokenCredential credential) Sets theTokenCredentialused to authorize requests sent to the service.credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.AzureNamedKeyCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.AzureSasCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.TokenCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.customEndpointAddress(String customEndpointAddress) Sets a custom endpoint address when connecting to the Event Hubs service.eventHubName(String eventHubName) Sets the name of the Event Hub to connect the client to.fullyQualifiedNamespace(String fullyQualifiedNamespace) Sets the fully qualified name for the Event Hubs namespace.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.proxyOptions(com.azure.core.amqp.ProxyOptions proxyOptions) Sets the proxy configuration to use forEventHubAsyncClient.retry(com.azure.core.amqp.AmqpRetryOptions retryOptions) Deprecated.retryOptions(com.azure.core.amqp.AmqpRetryOptions retryOptions) Sets the retry policy forEventHubAsyncClient.Toggles the builder to use the same connection for producers or consumers that are built from this instance.transportType(com.azure.core.amqp.AmqpTransportType transport) Sets the transport type by which all the communication with Azure Event Hubs occurs.
-
Field Details
-
DEFAULT_CONSUMER_GROUP_NAME
The name of the default consumer group in the Event Hubs service.- See Also:
-
-
Constructor Details
-
EventHubClientBuilder
public EventHubClientBuilder()Creates a new instance with the default transportAmqpTransportType.AMQPand 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
-
clientOptions
Sets the client options.- Specified by:
clientOptionsin interfacecom.azure.core.amqp.client.traits.AmqpTrait<EventHubClientBuilder>- Parameters:
clientOptions- The client options.- Returns:
- The updated
EventHubClientBuilderobject.
-
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.
- Specified by:
connectionStringin interfacecom.azure.core.client.traits.ConnectionStringTrait<EventHubClientBuilder>- Parameters:
connectionString- The connection string to use for connecting to the Event Hub instance or Event Hubs instance. It is expected that the Event Hub name and the shared access key properties are contained in this connection string.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- ifconnectionStringis null or empty. IffullyQualifiedNamespacein the connection string is null.NullPointerException- if a credential could not be extractedcom.azure.core.exception.AzureException- If the shared access signature token credential could not be created using the connection string.
-
connectionString
Sets the credential information given a connection string to the Event Hubs namespace and name to a specific Event Hub instance.- Parameters:
connectionString- The connection string to use for connecting to the Event Hubs namespace; it is expected that the shared access key properties are contained in this connection string, but not the Event Hub name.eventHubName- The name of the Event Hub to connect the client to.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
NullPointerException- ifconnectionStringoreventHubNameis null.IllegalArgumentException- ifconnectionStringoreventHubNameis an empty string. Or, if theconnectionStringcontains the Event Hub name.com.azure.core.exception.AzureException- If the shared access signature token credential could not be created using the connection string.
-
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. UseConfiguration.NONEto bypass using configuration settings during construction.- Specified by:
configurationin interfacecom.azure.core.client.traits.ConfigurationTrait<EventHubClientBuilder>- Parameters:
configuration- The configuration store used to configure theEventHubAsyncClient.- Returns:
- The updated
EventHubClientBuilderobject.
-
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
transport typeis used.- Parameters:
customEndpointAddress- The custom endpoint address.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- ifcustomEndpointAddresscannot be parsed into a validURL.
-
fullyQualifiedNamespace
Sets the fully qualified name for the Event Hubs namespace.- Parameters:
fullyQualifiedNamespace- The fully qualified name for the Event Hubs namespace. This is likely to be similar to "{your-namespace}.servicebus.windows.net".- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- iffullyQualifiedNamespaceis an empty string.NullPointerException- iffullyQualifiedNamespaceis null.
-
eventHubName
Sets the name of the Event Hub to connect the client to.- Parameters:
eventHubName- The name of the Event Hub to connect the client to.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- ifeventHubNameis an empty string.NullPointerException- ifeventHubNameis null.
-
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.TokenCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.- Parameters:
fullyQualifiedNamespace- The fully qualified name for the Event Hubs namespace. This is likely to be similar to "{your-namespace}.servicebus.windows.net".eventHubName- The name of the Event Hub to connect the client to.credential- The token credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- iffullyQualifiedNamespaceoreventHubNameis an empty string.NullPointerException- iffullyQualifiedNamespace,eventHubName,credentialsis null.
-
credential
Sets theTokenCredentialused 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 theTokenCredentialtype.- Specified by:
credentialin interfacecom.azure.core.client.traits.TokenCredentialTrait<EventHubClientBuilder>- Parameters:
credential- The token credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
NullPointerException- ifcredentialsis null.
-
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.AzureNamedKeyCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.- Parameters:
fullyQualifiedNamespace- The fully qualified name for the Event Hubs namespace. This is likely to be similar to "{your-namespace}.servicebus.windows.net".eventHubName- The name of the Event Hub to connect the client to.credential- The shared access name and key credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- iffullyQualifiedNamespaceoreventHubNameis an empty string.NullPointerException- iffullyQualifiedNamespace,eventHubName,credentialsis null.
-
credential
public EventHubClientBuilder credential(com.azure.core.credential.AzureNamedKeyCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.- Specified by:
credentialin interfacecom.azure.core.client.traits.AzureNamedKeyCredentialTrait<EventHubClientBuilder>- Parameters:
credential- The shared access name and key credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
NullPointerException- ifcredentialsis null.
-
credential
public EventHubClientBuilder credential(String fullyQualifiedNamespace, String eventHubName, com.azure.core.credential.AzureSasCredential credential) Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.- Parameters:
fullyQualifiedNamespace- The fully qualified name for the Event Hubs namespace. This is likely to be similar to "{your-namespace}.servicebus.windows.net".eventHubName- The name of the Event Hub to connect the client to.credential- The shared access signature credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- iffullyQualifiedNamespaceoreventHubNameis an empty string.NullPointerException- iffullyQualifiedNamespace,eventHubName,credentialsis null.
-
credential
Sets the credential information for which Event Hub instance to connect to, and how to authorize against it.- Specified by:
credentialin interfacecom.azure.core.client.traits.AzureSasCredentialTrait<EventHubClientBuilder>- Parameters:
credential- The shared access signature credential to use for authorization. Access controls may be specified by the Event Hubs namespace or the requested Event Hub, depending on Azure configuration.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
NullPointerException- ifcredentialsis null.
-
proxyOptions
Sets the proxy configuration to use forEventHubAsyncClient. When a proxy is configured,AmqpTransportType.AMQP_WEB_SOCKETSmust be used for the transport type.- Specified by:
proxyOptionsin interfacecom.azure.core.amqp.client.traits.AmqpTrait<EventHubClientBuilder>- Parameters:
proxyOptions- The proxy configuration to use.- Returns:
- The updated
EventHubClientBuilderobject.
-
transportType
Sets the transport type by which all the communication with Azure Event Hubs occurs. Default value isAmqpTransportType.AMQP.- Specified by:
transportTypein interfacecom.azure.core.amqp.client.traits.AmqpTrait<EventHubClientBuilder>- Parameters:
transport- The transport type to use.- Returns:
- The updated
EventHubClientBuilderobject.
-
retry
Deprecated.Replaced byretryOptions(AmqpRetryOptions).Sets the retry policy forEventHubAsyncClient. If not specified, the default retry options are used.- Parameters:
retryOptions- The retry policy to use.- Returns:
- The updated
EventHubClientBuilderobject.
-
retryOptions
Sets the retry policy forEventHubAsyncClient. If not specified, the default retry options are used.- Specified by:
retryOptionsin interfacecom.azure.core.amqp.client.traits.AmqpTrait<EventHubClientBuilder>- Parameters:
retryOptions- The retry policy to use.- Returns:
- The updated
EventHubClientBuilderobject.
-
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".- Parameters:
consumerGroup- 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".- Returns:
- The updated
EventHubClientBuilderobject.
-
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:
prefetchCount- The amount of events per partition to queue locally. Defaults to 500 events per partition.- Returns:
- The updated
EventHubClientBuilderobject. - Throws:
IllegalArgumentException- ifprefetchCountis less than1or greater than8000.
-
buildAsyncConsumerClient
Creates a newEventHubConsumerAsyncClientbased on the options set on this builder. Every timebuildAsyncConsumer()is invoked, a new instance ofEventHubConsumerAsyncClientis created.- Returns:
- A new
EventHubConsumerAsyncClientwith the configured options. - Throws:
IllegalArgumentException- If shared connection is not used and the credentials have not been set using eitherconnectionString(String)orcredential(String, String, TokenCredential). Also, ifconsumerGroup(String)have not been set. And if a proxy is specified but the transport type is notweb sockets. Or, if theeventHubNamehas not been set.
-
buildConsumerClient
Creates a newEventHubConsumerClientbased on the options set on this builder. Every timebuildConsumer()is invoked, a new instance ofEventHubConsumerClientis created.- Returns:
- A new
EventHubConsumerClientwith the configured options. - Throws:
IllegalArgumentException- If shared connection is not used and the credentials have not been set using eitherconnectionString(String)orcredential(String, String, TokenCredential). Also, ifconsumerGroup(String)have not been set. And if a proxy is specified but the transport type is notweb sockets. Or, if theeventHubNamehas not been set.
-
buildAsyncProducerClient
Creates a newEventHubProducerAsyncClientbased on options set on this builder. Every timebuildAsyncProducer()is invoked, a new instance ofEventHubProducerAsyncClientis created.- Returns:
- A new
EventHubProducerAsyncClientinstance with all the configured options. - Throws:
IllegalArgumentException- If shared connection is not used and the credentials have not been set using eitherconnectionString(String)orcredential(String, String, TokenCredential). Or, if a proxy is specified but the transport type is notweb sockets. Or, if theeventHubNamehas not been set.
-
buildProducerClient
Creates a newEventHubProducerClientbased on options set on this builder. Every timebuildAsyncProducer()is invoked, a new instance ofEventHubProducerClientis created.- Returns:
- A new
EventHubProducerClientinstance with all the configured options. - Throws:
IllegalArgumentException- If shared connection is not used and the credentials have not been set using eitherconnectionString(String)orcredential(String, String, TokenCredential). Or, if a proxy is specified but the transport type is notweb sockets. Or, if theeventHubNamehas not been set.
-
retryOptions(AmqpRetryOptions).