Search code examples
javaazureservicebus

How do I close a ServiceBusAdministrationAsyncClient in Java when I no longer need it?


For the sender and receiver clients, I can call the .close() method once I no longer need them. I would like to do the same with an administration client but it doesn't have a .close() method.

Here is my code:

 //Create queue if it doesn't exist
 try{
     ServiceBusAdministrationAsyncClient adminClient = serviceBusConnectionManager.createServiceBusAdministrationAsyncClient();
     if(!adminClient.getQueueExists(queueName).block()) {
         adminClient.createQueue(queueName).block();
     }
     adminClient.close();
 } catch (Exception e) {
     // Handle the exception for failure to create the queue
 }

Of course this doesn't compile because ServiceBusAdministrationAsyncClient.close() doesn't exist.

ServiceBusConnectionManager.createServiceBusAdministrationAsyncClient() is a custom method that creates the admin client using new ServiceBusAdministrationClientBuilder().buildAsyncClient().

My pom.xml contains the following dependency:

 <dependency>
     <groupId>com.azure</groupId>
     <artifactId>azure-messaging-servicebus</artifactId>
     <version>7.13.3</version>
 </dependency>

What am I missing here? Is there simply no way to close the admin client? Is there some other way I can release the resources once the try/catch block finishes executing?


Solution

  • There is no need to close the administration client.

    Senders and receivers use a persistent AMQP connection to communicate with Service Bus that will continue to consume resources in your application and on the service until closed or an idle timeout occurs. As a result, it is strongly recommended that your application close them when no longer needed.

    The administration clients communicates with Service Bus via HTTP, which allows resources to be managed in the scope of a request/response cycle. As a result, your application does not need to provide a hint to understand when it no longer needs the client - it can be inferred by whether or not active calls are being made.