Brian Kessler
Brian Kessler

Reputation: 2327

In a C# TimerTrigger Function App, how can I process the service bus at a regular interval instead of continuously?

I'm creating an integration between Microsoft Azure and Salesforce, where the contents of a Microsoft Azure Service Bus must all be transformed and sent forward to Salesforce.

Salesforce has various limits on API consumption, so just transforming each message individually and sending it forward could lead to errors and data loss.

Instead, at a regular interval (for example every 30 minutes), I'd like to check the service bus, extract any waiting messages, and send them to Salesforce's bulk API.

The following code successfully collects data from the service bus

       this.queueClient.RegisterMessageHandler(
                this.ProcessMessagesAsync,
                new MessageHandlerOptions(this.ExceptionReceivedHandler) {
                    MaxConcurrentCalls = 1,
                    AutoComplete = false
                });

However, as the name of the function suggests, this is registering the message handler. The message will actually be handled as soon as it comes in. Rerunning this every 30 minutes does nothing useful, as it is just reregistering and not effecting the interval.

Any ideas how to delay handling?

Upvotes: 0

Views: 327

Answers (1)

krishg
krishg

Reputation: 6508

Instead of using QueueClient, you can use MessageReceiver to explicitly receive message(s). In your case I believe, you need a batch of messages on every trigger.

var receiverFactory = MessagingFactory.CreateFromConnectionString(<connectionString>);
var receiver = await receiverFactory.CreateMessageReceiverAsync(<queueName>, <receive mode>);
var messages = await receiver.ReceiveBatchAsync(<batch size>);

Upvotes: 1

Related Questions