Search code examples
javaazureazureservicebus

What is the difference between ServiceBusSenderClient and ServiceBusSenderAsyncClient in Azure Service Bus?


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();

Solution

    • 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:

    <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 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("<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.