1

I'm testing retry options for Azure Service Bus publisher/subscriber client because after a sudden connection failure the client will not retry to send or receive messages.

Following is the code for publisher client sendMessage() method and I have set maximum delivery count to 1000 for the subscription. Still the client uses default retryPolicy values and I cannot see it retries as I have given in amqpRetryOptions.

static void sendMessage() {
        // create Retry Options for the Service Bus client
        AmqpRetryOptions amqpRetryOptions = new AmqpRetryOptions();
        amqpRetryOptions.setDelay(Duration.ofSeconds(1));
        amqpRetryOptions.setMaxRetries(120);
        amqpRetryOptions.setMaxDelay(Duration.ofMinutes(5));
        amqpRetryOptions.setMode(AmqpRetryMode.EXPONENTIAL);
        amqpRetryOptions.setTryTimeout(Duration.ofSeconds(5));

        // create a Service Bus Sender client for the queue
        ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
                .connectionString(connectionString)
                .retryOptions(amqpRetryOptions)
                .sender()
                .topicName(topicName)
                .buildClient();

        // send one message to the topic
        senderClient.sendMessage(new ServiceBusMessage("Hello, World! "));
        System.out.println("Sent a single message to the topic");
    }

Is my approach wrong?

  • If so, what is the standard way?
  • If not how can approach retry mechanism?

If not how to

  • Do only setMaxRetries() or other functions also don't work? This might be helpful: [AmqpRetryOptions.java](https://github.com/Azure/azure-sdk-for-java/blob/azure-messaging-servicebus_7.3.0/sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/AmqpRetryOptions.java) – Ecstasy Jul 19 '21 at 07:53
  • Nothing works. Maybe it's due to non transient exceptions are not handled by Azure. Is there example Java code to handle retrying or catching non transient exceptions manually? – Binod Karunanayake Jul 19 '21 at 10:32
  • https://github.com/microsoft/azure-spring-boot/issues/817 do you have similar problem? – Vova Bilyachat Jul 26 '21 at 04:19

1 Answers1

1

I was able to get retrying mechanism work using ServiceBusSenderAsyncClient. Also, I could catch exceptions to check whether the cause is transient or not.

static void sendMessage() {
    // create Retry Options for the Service Bus client
    AmqpRetryOptions amqpRetryOptions = new AmqpRetryOptions();
    amqpRetryOptions.setDelay(Duration.ofSeconds(1));
    amqpRetryOptions.setMaxRetries(5);
    amqpRetryOptions.setMaxDelay(Duration.ofSeconds(15));
    amqpRetryOptions.setMode(AmqpRetryMode.EXPONENTIAL);
    amqpRetryOptions.setTryTimeout(Duration.ofSeconds(5));

   // instantiate a client that will be used to call the service
   ServiceBusSenderAsyncClient serviceBusSenderAsyncClient = new ServiceBusClientBuilder()
       .connectionString(connectionString)
       .retryOptions(amqpRetryOptions)
       .sender()
       .topicName(topicName)
       .buildAsyncClient();

    // create a message
    ServiceBusMessage serviceBusMessage = new ServiceBusMessage("Hello, World!\n")

    // send the message to the topic
    serviceBusSenderAsyncClient.sendMessage(serviceBusMessage).subscribe(
        unused -> System.out.println("Message sent successfully"),
        error -> {
            ServiceBusException serviceBusException = (ServiceBusException) error;
            System.out.println(serviceBusException.isTransient());
        },
        () -> {
            System.out.println("Message sent successfully");
        }
    );
}

  • Found sample codes of Service Bus SDK on https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/servicebus/azure-messaging-servicebus/src/samples/java/com/azure/messaging/servicebus – Binod Karunanayake Jul 26 '21 at 04:25