英文:
What is the difference between ServiceBusSenderClient and ServiceBusSenderAsyncClient in Azure Service Bus?
问题
我想知道Azure Service Bus中的SenderClient和AsyncSenderClient之间有什么区别。以下是下面两个代码片段的区别:
代码片段 1:
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.connectionString("<<SERVICE BUS NAMESPACE CONNECTION STRING>>")
.sender()
.queueName("<<QUEUE NAME>>")
.buildClient();
代码片段 2:
ServiceBusSenderAsyncClient sender = new ServiceBusClientBuilder()
.connectionString("<<SERVICE BUS NAMESPACE CONNECTION STRING>>")
.sender()
.queueName("<<QUEUE NAME>>")
.buildAsyncClient();
英文:
I want to know the difference between SenderClient and AsynSenderClient in Azure Service Bus. What is the difference in the below 2 code snippets?
Snippet 1:
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.connectionString("<< CONNECTION STRING FOR THE SERVICE BUS NAMESPACE >>")
.sender()
.queueName("<< QUEUE NAME >>")
.buildClient();
Snippet 2:
ServiceBusSenderAsyncClient sender = new ServiceBusClientBuilder()
.connectionString("<< CONNECTION STRING FOR THE SERVICE BUS NAMESPACE >>")
.sender()
.queueName("<< QUEUE NAME >>")
.buildAsyncClient();
答案1
得分: 2
ServiceBusClientBuilder
用于创建ServiceBusSenderClient
实例。调用buildClient()
方法来构建客户端。生成的sender
变量的类型为ServiceBusSenderClient
,它提供了用于向由queue
指定的 Service Bus 队列发送消息的同步编程模型。- 当您使用
ServiceBusSenderClient
发送消息时,调用线程将被阻塞,直到消息被发送并从 Service Bus 服务接收到响应。
如果您使用 Maven 依赖,请按以下方式检查。
依赖项:
<dependencies>
<!-- Azure Messaging Service Bus -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-servicebus</artifactId>
<version>7.7.0</version>
</dependency>
<!-- Azure Identity -->
<dependency>
<groupId>com.azure.identity</groupId>
<artifactId>azure-identity</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Azure Core -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.17.0</version>
</dependency>
<!-- Azure Logging Dependencies (optional) -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-logger-slf4j</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
您可以在此处查看更多与您要求相关的内容。
ServiceBusSenderClient
以下是异步方法:
-
ServiceBusSenderAsyncClient
是一个异步客户端,提供了向 Service Bus 实体发送消息的非阻塞方法。 -
在 Java 中,
ServiceBusSenderAsyncClient
类继承自java.lang.Object
类,它是类层次结构的根。这意味着ServiceBusSenderAsyncClient
将继承由java.lang.Object
类提供的基本功能,如toString()
、hashCode()
和equals()
方法。
我尝试了一个示例应用程序,使用 ServiceBusSenderAsyncClient
类异步向 Service Bus 实体发送消息。请检查上面的依赖项。
import com.azure.messaging.servicebus.ServiceBusSenderAsyncClient;
import com.azure.messaging.servicebus.ServiceBusMessage;
public class ServiceBusSenderExample {
public static void main(String[] args) {
// 创建 ServiceBusSenderAsyncClient 的实例
ServiceBusSenderAsyncClient senderClient = new ServiceBusSenderAsyncClient("<connectionString>", "<queueOrTopicName>");
// 创建消息
ServiceBusMessage message = new ServiceBusMessage("Hello, Service Bus!");
// 异步发送消息
senderClient.sendMessage(message).subscribe(
messageId -> {
System.out.println("消息发送成功。MessageId:" + messageId);
},
error -> {
System.err.println("发送消息时发生错误:" + error);
},
() -> {
System.out.println("消息发送完成。");
}
);
}
}
- 如果您希望实现非阻塞行为并利用异步编程的好处,可以使用
ServiceBusSenderAsyncClient
。请查看链接获取更多信息。
英文:
ServiceBusClientBuilder
is used to create aServiceBusSenderClient
instance. ThebuildClient()
method is invoked to build the client. The resultingsender
variable is of typeServiceBusSenderClient
, which provides a synchronous programming model for sending messages to the Service Bus queue specified by thequeue
.- When you use
ServiceBusSenderClient
to send a message, the calling thread will be blocked until the message is sent and a response is received from the Service Bus service.
If you use maven dependency please check as given below.
Dependencies:
<dependencies>
<!-- Azure Messaging Service Bus -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-servicebus</artifactId>
<version>7.7.0</version>
</dependency>
<!-- Azure Identity -->
<dependency>
<groupId>com.azure.identity</groupId>
<artifactId>azure-identity</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Azure Core -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.17.0</version>
</dependency>
<!-- Azure Logging Dependencies (optional) -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-logger-slf4j</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
Here you can explore more content for your requirement.
ServiceBusSenderClient
Here is the asynchronous approach:
-
ServiceBusSenderAsyncClient
is an asynchronous client that provides non-blocking methods for sending messages to a Service Bus entity. -
In Java,
ServiceBusSenderAsyncClient
class extends thejava.lang.Object
class, which is the root of the class hierarchy. That meansServiceBusSenderAsyncClient
will inherits the basic functionalities provided by thejava.lang.Object
class, such as thetoString()
,hashCode()
, andequals()
methods.
I tried a sample application use the ServiceBusSenderAsyncClient
class to send a message to a Service Bus entity asynchronously. For the dependencies check above.
import com.azure.messaging.servicebus.ServiceBusSenderAsyncClient;
import com.azure.messaging.servicebus.ServiceBusMessage;
public class ServiceBusSenderExample {
public static void main(String[] args) {
// Create an instance of the ServiceBusSenderAsyncClient
ServiceBusSenderAsyncClient senderClient = new ServiceBusSenderAsyncClient("<connectionString>", "<queueOrTopicName>");
// Create a message
ServiceBusMessage message = new ServiceBusMessage("Hello, Service Bus!");
// Send the message asynchronously
senderClient.sendMessage(message).subscribe(
messageId -> {
System.out.println("Message sent successfully. MessageId: " + messageId);
},
error -> {
System.err.println("Error occurred while sending the message: " + error);
},
() -> {
System.out.println("Message sending complete.");
}
);
}
}
- Non-blocking behavior and want to leverage the benefits of asynchronous programming, you can use
ServiceBusSenderAsyncClient
check on the link for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论