如何在不再需要时关闭Java中的ServiceBusAdministrationAsyncClient?

huangapple go评论64阅读模式
英文:

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

问题

I understand your request to only translate the code portion. Here it is:

 // 如果队列不存在,则创建队列
 try{
	 ServiceBusAdministrationAsyncClient adminClient = serviceBusConnectionManager.createServiceBusAdministrationAsyncClient();
	 if(!adminClient.getQueueExists(queueName).block()) {
		 adminClient.createQueue(queueName).block();
	 }
	 adminClient.close();
 } catch (Exception e) {
	 // 处理创建队列失败的异常
 }

Please let me know if you need any further assistance with this code.

英文:

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?

答案1

得分: 0

以下是翻译好的部分:

不需要关闭管理客户端。

发送方和接收方使用持久的AMQP连接与Service Bus通信,该连接将继续消耗应用程序和服务上的资源,直到关闭或发生空闲超时。因此,强烈建议在不再需要时关闭应用程序。

管理客户端通过HTTP与Service Bus通信,这允许在请求/响应周期的范围内管理资源。因此,您的应用程序不需要提供提示来确定何时不再需要客户端 - 可以通过是否正在进行活动调用来推断。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月13日 21:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定