EventHubConnectionStringProperties Class

  • java.lang.Object
    • com.azure.messaging.eventhubs.models.EventHubConnectionStringProperties

public final class EventHubConnectionStringProperties

A utility class that parses a connection string into sections. An Event Hubs connection string is a set of key-value pairs separated by semicolon. A typical example is "Endpoint=sb://foo.EventHub.windows.net/;SharedAccessKeyName=someKeyName;SharedAccessKey=someKeyValue".

A connection may have the following sections:

  • Endpoint, which is mandatory. The fully qualifed namespace of the Event Hubs namespace. It will look similar to "{your-namespace}.servicebus.windows.net"
  • SharedAccessKeyName and SharedAccessKey, optional, used to authenticate the access to the Event Hubs namespace or Event Hub instance.
  • EntityPath, optional, the name of the Event Hub instance.
  • SharedAccessSignature, optional, an alternative way to authenticate the access to an Event Hub instance.

When you have an Event Hubs connection string, you can use connectionString(String connectionString) to build a client. If you'd like to use a TokenCredential to access an Event Hub, you can use this utility class to get the fully qualified namespace and entity path from the connection string and then use credential(String fullyQualifiedNamespace, String eventHubName, TokenCredential credential).

Sample: Construct a producer using Event Hub specific connection string

The code snippet below shows how to create a sync producer using a connection string that is scoped to a specific Event Hub. This can be found in the Azure Portal by navigating to the Event Hubs namespace, selecting an Event Hub, then choosing "Shared access policies" in the "Settings" panel. The visual difference between an Event Hub specific connection string and an Event Hubs namespace connection string is the "EntityPath" section. Additionally, this type of connection string is scoped to that specific Event Hub instance.

String connectionString = "Endpoint=sb://demo-hub.servicebus.windows.net/;SharedAccessKeyName=TestAccessKey;"
     + "SharedAccessKey=TestAccessKeyValue;EntityPath=MyEventHub";

 EventHubConnectionStringProperties properties = EventHubConnectionStringProperties.parse(connectionString);
 AzureNamedKeyCredential credential = new AzureNamedKeyCredential(properties.getSharedAccessKeyName(),
     properties.getSharedAccessKey());

 EventHubProducerClient producer = new EventHubClientBuilder()
     .credential(properties.getFullyQualifiedNamespace(), properties.getEntityPath(), credential)
     .buildProducerClient();

Sample: Construct a producer using Event Hubs namespace connection string

The code snippet below shows how to create a sync producer using an Event Hubs namespace connection string. This can be found in the Azure Portal by navigating to the Event Hubs namespace then choosing "Shared access policies" in the "Settings" panel. The visual difference between an Event Hubs namespace connection string and an Event Hub specific namespace connection string is the absence of the "EntityPath" section. Namespace connection strings have access to all Event Hub instances in that namespace.

String connectionString = "Endpoint=sb://demo-hub.servicebus.windows.net/;"
     + "SharedAccessKeyName=NamespaceAccessKey;SharedAccessKey=NamespaceAccessKeyValue";

 String eventHubName = "my-event-hub";

 EventHubConnectionStringProperties properties = EventHubConnectionStringProperties.parse(connectionString);
 AzureNamedKeyCredential credential = new AzureNamedKeyCredential(properties.getSharedAccessKeyName(),
     properties.getSharedAccessKey());

 EventHubProducerClient producer = new EventHubClientBuilder()
     .credential(properties.getFullyQualifiedNamespace(), eventHubName, credential)
     .buildProducerClient();

Sample: Construct a producer using a shared access signature (SAS)

The code snippet below shows how to create a sync producer using a shared access signature (SAS). Shared access signatures allow for granular control over access to an Event Hub. Authenticate access to Event Hubs resources using shared access signatures (SAS) contains information about how to configure and generate signatures.

// "sr" is the URI of the resource being accessed.
 // "se" is the expiration date of the signature.
 // "skn" is name of the authorization policy used to create the SAS
 String connectionString = "Endpoint={endpoint};EntityPath={entityPath};SharedAccessSignature="
     + "SharedAccessSignature sr={fullyQualifiedNamespace}&sig={signature}&se={expiry}&skn={policyName}";

 EventHubConnectionStringProperties properties = EventHubConnectionStringProperties.parse(connectionString);
 AzureSasCredential credential = new AzureSasCredential(connectionString);

 EventHubConsumerClient consumer = new EventHubClientBuilder()
     .credential(properties.getFullyQualifiedNamespace(), properties.getEntityPath(), credential)
     .buildConsumerClient();

Method Summary

Modifier and Type Method and Description
String getEndpoint()

Gets the "Endpoint" value of the connection string.

String getEntityPath()

Gets the "EntityPath" value of the connection string.

String getFullyQualifiedNamespace()

Gets the fully qualified namespace, or hostname, from the connection string "Endpoint" section.

String getSharedAccessKey()

Gets the "SharedAccessSignature" section of the connection string.

String getSharedAccessKeyName()

Gets the "SharedAccessKeyName" section of the connection string.

static EventHubConnectionStringProperties parse(String connectionString)

Parse a Event Hub connection string into an instance of this class.

Methods inherited from java.lang.Object

Method Details

getEndpoint

public String getEndpoint()

Gets the "Endpoint" value of the connection string.

Returns:

The endpoint.

getEntityPath

public String getEntityPath()

Gets the "EntityPath" value of the connection string.

Returns:

The entity path, or null if the connection string doesn't have an "EntityPath".

getFullyQualifiedNamespace

public String getFullyQualifiedNamespace()

Gets the fully qualified namespace, or hostname, from the connection string "Endpoint" section.

Returns:

The fully qualified namespace.

getSharedAccessKey

public String getSharedAccessKey()

Gets the "SharedAccessSignature" section of the connection string.

Returns:

The shared access key value, or null if the connection string doesn't have a "SharedAccessSignature".

getSharedAccessKeyName

public String getSharedAccessKeyName()

Gets the "SharedAccessKeyName" section of the connection string.

Returns:

The shared access key name, or null if the connection string doesn't have a "SharedAccessKeyName".

parse

public static EventHubConnectionStringProperties parse(String connectionString)

Parse a Event Hub connection string into an instance of this class.

Parameters:

connectionString - The connection string to be parsed.

Returns:

An instance of this class.

Applies to