Search code examples
nservicebusazure-webjobsazureservicebus

Can webjob consume related messages before executing?


Is it possible for a webjob to listen to a queue and consume all related messages (based on message id or session id or something else) with a small delay after each new message?

For instance, three users edit and update three schedules. Two are for the same employee. Each schedule generates an event for PayrollUpdated for: 1) employee 100, week 1, year 2017, 2) employee 200, week 1, year 2017, 3) employee 100, week1, year 2017.

I’d like the webjob to listen to the queue and execute two concurrent webjobs, one for each employee,week,year identifier.

I cannot tell if batches or sessions can accomplish this. I am also unable to wrap my head around NServiceBus to know if that is an option or not.

Note: the webjob task is idempotent.


Solution

  • Is it possible for a webjob to listen to a queue and consume all related messages (based on message id or session id or something else) with a small delay after each new message?

    Out of the box no. To consume all related messages you'd need to use Message Sessions, which are not supported by the ServiceBus trigger. There is an issue for exactly this problem (and also an issue for Azure Functions since they rely on WebJobs SDK). Alternatively, you could look into creating a custom WebJob extension that would use Message Sessions.

    I am also unable to wrap my head around NServiceBus to know if that is an option or not.

    With NServiceBus you could achieve it using Sagas.