O'Neil Tomlinson
O'Neil Tomlinson

Reputation: 898

Read messages from Azure Service Bus Queue BUT NOT real time

I have specific business case where I need to read an azure service bus queue – but reading this queue should not be real time.

This is my setup

All the code snippets I've seen online are all triggered in real time. As they are all registering an event that get fired as soon as a message get sent to the queue.

I had this code simppet in my application but noticed that as soon as a message is added to the queue the message is pulled imediately from the queue, which i dont want. I want the message to remain in the queue until the end of the day

  public async Task<IEnumerable<ChangeNotification>> ReadChangeNotificationMessagesAsync()
{

    processor = client.CreateProcessor(serviceBusOptions.TopicName, serviceBusOptions.SubscriptionName, serviceBusProcessorOptions);
    processor.ProcessMessageAsync += AddNotificationToQueueEventAsync;
    processor.ProcessErrorAsync += ProcessErrorEventAsync;
    await processor.StartProcessingAsync();

}


private async Task AddNotificationToQueueEventAsync(ProcessMessageEventArgs args)
{
    var changeNotification = args.Message.Body.ToObjectFromJson<ChangeNotification>(
        new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

    //do some stuff
}

private Task ProcessErrorEventAsync(ProcessErrorEventArgs arg)
{
   //log error
}

serviceBusProcessorOptions = new ServiceBusProcessorOptions
    {
        MaxConcurrentCalls = serviceBusOptions.Value.MaxConcurrentCalls,
        AutoCompleteMessages = serviceBusOptions.Value.AutoCompleteMessages
   };

Can someone provided a bit of code snippet that will allow me to read the queue but not in real time

Upvotes: 1

Views: 1067

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136386

You can use a Timer Triggered Azure Function and schedule it to run once a day. In your Function code you can make use of Service Bus SDK to read messages from the Service Bus and process them.

UPDATE

I noticed that you are using Service Bus Processor to process the messages which basically provides an event based model for processing your messages.

Instead of using that, you can simply use ServiceBusReceiver and read messages manually using ReceiveMessagesAsync(Int32, Nullable<TimeSpan>, CancellationToken).

Upvotes: 1

Related Questions