How to set message visibility timeout in both queue and subscription programaically

Deepak Goyal 0 Reputation points
2023-08-03T11:59:30.29+00:00

I want to set the message visibility timeout programmatically for both the Azure Service Bus Queue and Topic. Could you please provide code snippets demonstrating how to achieve this? Additionally, I would like to know the maximum visibility timeout allowed for both the Queue and Topic in Azure Service Bus.

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
{count} votes

2 answers

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2023-08-03T12:10:01.4666667+00:00

    @Deepak Goyal Thanks for reaching out. Can you please confirm what do you mean by "visibility timeout"? Do you mean the message TTL, entity (queue/topic TTL) or the operation timeout while receiving/sending the message and in which language you want to do it programmatically?

    Assuming that you want to set the message time to live when sending the message then you can leverage the Azure.Messaging.ServiceBus package in your dot net application.

    For more details on different samples, you can refer to this document.

                string connectionString = "yourservicebus connection string";
                string topicName = "test";
    
                var client = new ServiceBusClient(connectionString);
                var sender = client.CreateSender(topicName);
                var message = new ServiceBusMessage("Test Message");
                //The below code set the TTL on message to 1 minute (expiry time utcnow + 1 minute expiry).
                message.TimeToLive = TimeSpan.FromMinutes(1);
                await sender.SendMessageAsync(message);
    

    The TTL is max 14 days for service bus basic tier and for standard and premium namespace it is max integer value. You can find more details in this document.

    Please "Accept Answer" if the answer is helpful so that it can help others in the community.


  2. Periyasamy Neelamanikandan 0 Reputation points
    2023-09-19T18:12:42.3466667+00:00

    @MayankBargali-MSFT Looking for visibility timeout : visibility timeout, a period of time during which queue prevents all consumers from receiving and processing the message.

    The above answer provided is for TTL if TTL is met the message will move to DLQ. Visibility timeout is a time period message wait in a queue before retry.

    Similar feature (Visibility Timeout) in AWS SQS : https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.