ServiceBusSenderClient 与 ServiceBusSenderAsyncClient 在 Azure Service Bus 中有什么区别?

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

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(&quot;&lt;&lt; CONNECTION STRING FOR THE SERVICE BUS NAMESPACE &gt;&gt;&quot;)
    .sender()
    .queueName(&quot;&lt;&lt; QUEUE NAME &gt;&gt;&quot;)
    .buildClient();

Snippet 2:

ServiceBusSenderAsyncClient sender = new ServiceBusClientBuilder()
    .connectionString(&quot;&lt;&lt; CONNECTION STRING FOR THE SERVICE BUS NAMESPACE &gt;&gt;&quot;)
    .sender()
    .queueName(&quot;&lt;&lt; QUEUE NAME &gt;&gt;&quot;)
    .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 a ServiceBusSenderClient instance. The buildClient() method is invoked to build the client. The resulting sender variable is of type ServiceBusSenderClient, which provides a synchronous programming model for sending messages to the Service Bus queue specified by the queue.
  • 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:

&lt;dependencies&gt;
    &lt;!-- Azure Messaging Service Bus --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.azure&lt;/groupId&gt;
        &lt;artifactId&gt;azure-messaging-servicebus&lt;/artifactId&gt;
        &lt;version&gt;7.7.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- Azure Identity --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.azure.identity&lt;/groupId&gt;
        &lt;artifactId&gt;azure-identity&lt;/artifactId&gt;
        &lt;version&gt;1.3.1&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- Azure Core --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.azure&lt;/groupId&gt;
        &lt;artifactId&gt;azure-core&lt;/artifactId&gt;
        &lt;version&gt;1.17.0&lt;/version&gt;
    &lt;/dependency&gt;
    
    &lt;!-- Azure Logging Dependencies (optional) --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.azure&lt;/groupId&gt;
        &lt;artifactId&gt;azure-core-logger-slf4j&lt;/artifactId&gt;
        &lt;version&gt;1.17.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
        &lt;version&gt;1.7.32&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

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 the java.lang.Object class, which is the root of the class hierarchy. That means ServiceBusSenderAsyncClient will inherits the basic functionalities provided by the java.lang.Object class, such as the toString(), hashCode(), and equals() 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(&quot;&lt;connectionString&gt;&quot;, &quot;&lt;queueOrTopicName&gt;&quot;);

        // Create a message
        ServiceBusMessage message = new ServiceBusMessage(&quot;Hello, Service Bus!&quot;);

        // Send the message asynchronously
        senderClient.sendMessage(message).subscribe(
            messageId -&gt; {
                System.out.println(&quot;Message sent successfully. MessageId: &quot; + messageId);
            },
            error -&gt; {
                System.err.println(&quot;Error occurred while sending the message: &quot; + error);
            },
            () -&gt; {
                System.out.println(&quot;Message sending complete.&quot;);
            }
        );
    }
}
  • Non-blocking behavior and want to leverage the benefits of asynchronous programming, you can use ServiceBusSenderAsyncClient check on the link for more information.

huangapple
  • 本文由 发表于 2023年7月13日 17:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677763.html
匿名

发表评论

匿名网友

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

确定