简体   繁体   中英

How to add retry options (retry policy) to publisher/subscriber client which uses Azure Service Bus Java SDK?

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

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");
        }
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM