5353import java .util .UUID ;
5454import java .util .concurrent .atomic .AtomicBoolean ;
5555import java .util .concurrent .atomic .AtomicInteger ;
56+ import java .util .function .Supplier ;
5657import java .util .regex .Pattern ;
5758
5859import static com .azure .messaging .eventhubs .implementation .ClientConstants .CONNECTION_ID_KEY ;
@@ -221,7 +222,7 @@ public EventHubClientBuilder() {
221222 }
222223
223224 /**
224- * Sets the credential information given a connection string to the Event Hub instance.
225+ * Sets the credential information given a connection string to the Event Hub instance or the Event Hubs namespace .
225226 *
226227 * <p>
227228 * If the connection string is copied from the Event Hubs namespace, it will likely not contain the name to the
@@ -234,20 +235,34 @@ public EventHubClientBuilder() {
234235 * from that Event Hub will result in a connection string that contains the name.
235236 * </p>
236237 *
237- * @param connectionString The connection string to use for connecting to the Event Hub instance. It is expected
238- * that the Event Hub name and the shared access key properties are contained in this connection string.
238+ * @param connectionString The connection string to use for connecting to the Event Hub instance or Event Hubs
239+ * instance. It is expected that the Event Hub name and the shared access key properties are contained in this
240+ * connection string.
239241 *
240242 * @return The updated {@link EventHubClientBuilder} object.
241- * @throws IllegalArgumentException if {@code connectionString} is null or empty. Or, the {@code
242- * connectionString} does not contain the "EntityPath" key, which is the name of the Event Hub instance.
243+ * @throws IllegalArgumentException if {@code connectionString} is null or empty. If {@code fullyQualifiedNamespace}
244+ * in the connection string is null.
245+ * @throws NullPointerException if a credential could not be extracted
243246 * @throws AzureException If the shared access signature token credential could not be created using the
244247 * connection string.
245248 */
246249 @ Override
247250 public EventHubClientBuilder connectionString (String connectionString ) {
248- ConnectionStringProperties properties = new ConnectionStringProperties (connectionString );
249- TokenCredential tokenCredential = getTokenCredential (properties );
250- return credential (properties .getEndpoint ().getHost (), properties .getEntityPath (), tokenCredential );
251+ final ConnectionStringProperties properties = new ConnectionStringProperties (connectionString );
252+
253+ this .fullyQualifiedNamespace = Objects .requireNonNull (properties .getEndpoint ().getHost (),
254+ "'fullyQualifiedNamespace' cannot be null." );
255+ this .credentials = getTokenCredential (properties );
256+
257+ if (CoreUtils .isNullOrEmpty (fullyQualifiedNamespace )) {
258+ throw LOGGER .logExceptionAsError (new IllegalArgumentException ("'host' cannot be an empty string." ));
259+ }
260+
261+ if (!CoreUtils .isNullOrEmpty (properties .getEntityPath ())) {
262+ this .eventHubName = properties .getEntityPath ();
263+ }
264+
265+ return this ;
251266 }
252267
253268 private TokenCredential getTokenCredential (ConnectionStringProperties properties ) {
@@ -405,13 +420,6 @@ public EventHubClientBuilder eventHubName(String eventHubName) {
405420 return this ;
406421 }
407422
408- private String getEventHubName () {
409- if (CoreUtils .isNullOrEmpty (eventHubName )) {
410- throw LOGGER .logExceptionAsError (new IllegalArgumentException ("'eventHubName' cannot be an empty string." ));
411- }
412- return eventHubName ;
413- }
414-
415423 /**
416424 * Toggles the builder to use the same connection for producers or consumers that are built from this instance. By
417425 * default, a new connection is constructed and used created for each Event Hub consumer or producer created.
@@ -714,7 +722,7 @@ EventHubClientBuilder verifyMode(SslDomain.VerifyMode verifyMode) {
714722 * @throws IllegalArgumentException If shared connection is not used and the credentials have not been set using
715723 * either {@link #connectionString(String)} or {@link #credential(String, String, TokenCredential)}. Also, if
716724 * {@link #consumerGroup(String)} have not been set. And if a proxy is specified but the transport type is not
717- * {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}.
725+ * {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}. Or, if the {@code eventHubName} has not been set.
718726 */
719727 public EventHubConsumerAsyncClient buildAsyncConsumerClient () {
720728 if (CoreUtils .isNullOrEmpty (consumerGroup )) {
@@ -733,7 +741,7 @@ public EventHubConsumerAsyncClient buildAsyncConsumerClient() {
733741 * @throws IllegalArgumentException If shared connection is not used and the credentials have not been set using
734742 * either {@link #connectionString(String)} or {@link #credential(String, String, TokenCredential)}. Also, if
735743 * {@link #consumerGroup(String)} have not been set. And if a proxy is specified but the transport type is not
736- * {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}.
744+ * {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}. Or, if the {@code eventHubName} has not been set.
737745 */
738746 public EventHubConsumerClient buildConsumerClient () {
739747 return buildClient ().createConsumer (consumerGroup , prefetchCount );
@@ -747,6 +755,7 @@ public EventHubConsumerClient buildConsumerClient() {
747755 * @throws IllegalArgumentException If shared connection is not used and the credentials have not been set using
748756 * either {@link #connectionString(String)} or {@link #credential(String, String, TokenCredential)}. Or, if a
749757 * proxy is specified but the transport type is not {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}.
758+ * Or, if the {@code eventHubName} has not been set.
750759 */
751760 public EventHubProducerAsyncClient buildAsyncProducerClient () {
752761 return buildAsyncClient ().createProducer ();
@@ -760,6 +769,7 @@ public EventHubProducerAsyncClient buildAsyncProducerClient() {
760769 * @throws IllegalArgumentException If shared connection is not used and the credentials have not been set using
761770 * either {@link #connectionString(String)} or {@link #credential(String, String, TokenCredential)}. Or, if a
762771 * proxy is specified but the transport type is not {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}.
772+ * Or, if the {@code eventHubName} has not been set.
763773 */
764774 public EventHubProducerClient buildProducerClient () {
765775 return buildClient ().createProducer ();
@@ -786,7 +796,8 @@ public EventHubProducerClient buildProducerClient() {
786796 * @return A new {@link EventHubAsyncClient} instance with all the configured options.
787797 * @throws IllegalArgumentException if the credentials have not been set using either {@link
788798 * #connectionString(String)} or {@link #credential(String, String, TokenCredential)}. Or, if a proxy is
789- * specified but the transport type is not {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}.
799+ * specified but the transport type is not {@link AmqpTransportType#AMQP_WEB_SOCKETS web sockets}. Or, if the
800+ * {@code eventHubName} has not been set.
790801 */
791802 EventHubAsyncClient buildAsyncClient () {
792803 if (retryOptions == null ) {
@@ -898,6 +909,13 @@ Meter createMeter() {
898909
899910 private EventHubConnectionProcessor buildConnectionProcessor (MessageSerializer messageSerializer , Meter meter ) {
900911 final ConnectionOptions connectionOptions = getConnectionOptions ();
912+ final Supplier <String > getEventHubName = () -> {
913+ if (CoreUtils .isNullOrEmpty (eventHubName )) {
914+ throw LOGGER .logExceptionAsError (new IllegalArgumentException ("'eventHubName' cannot be an empty string." ));
915+ }
916+ return eventHubName ;
917+ };
918+
901919 final Flux <EventHubAmqpConnection > connectionFlux = Flux .create (sink -> {
902920 sink .onRequest (request -> {
903921
@@ -921,15 +939,15 @@ private EventHubConnectionProcessor buildConnectionProcessor(MessageSerializer m
921939 final ReactorHandlerProvider handlerProvider = new ReactorHandlerProvider (provider , meter );
922940
923941 final EventHubAmqpConnection connection = new EventHubReactorAmqpConnection (connectionId ,
924- connectionOptions , getEventHubName (), provider , handlerProvider , tokenManagerProvider ,
942+ connectionOptions , getEventHubName . get (), provider , handlerProvider , tokenManagerProvider ,
925943 messageSerializer );
926944
927945 sink .next (connection );
928946 });
929947 });
930948
931949 return connectionFlux .subscribeWith (new EventHubConnectionProcessor (
932- connectionOptions .getFullyQualifiedNamespace (), getEventHubName (), connectionOptions .getRetry ()));
950+ connectionOptions .getFullyQualifiedNamespace (), getEventHubName . get (), connectionOptions .getRetry ()));
933951 }
934952
935953 private ConnectionOptions getConnectionOptions () {
0 commit comments