maddog
maddog

Reputation: 194

Grouping published events in NServiceBus

I'm using NServiceBus to send orders (one order per command) to the back-end systems. Each order has a Customer (parent). The back-end system publishes an "order accepted" event after an order is saved successfully. There are multiple subscribers to this event one of them is a file generator component which generates an XML file to be consumed by a third party. This file is generated per customer. Since the published event is at the order level every time an "order accepted" event is published the file component creates the entire file for the Customer.

Is there a way within NServiceBus to group events at the subscriber so that we can reduce the number of times the file generator runs if there are multiple orders for the same customer?

One idea we had was to put the subscriber to sleep for a fixed time and when it wakes up it can group the messages in its queue by customer and generate the file once per customer. Does this sound like a good idea?

Thanks in advance.

Upvotes: 2

Views: 156

Answers (1)

David Boike
David Boike

Reputation: 18635

The best way to do this is to use a Saga with the behavior of a debouncer.

The saga would subscribe to the event, and every time it is observed, request a new timeout for some period of time you are willing to wait, let's say 5 minutes.

If the timeout message arrives and there have been no additional events observed since it was set, then you can send a command to run the file generator.

This makes the assumption that the incoming events are going to arrive in spurts, that is, you'll receive a grouping of them close together and then none for quite some time. Using a debouncer like this, if you received one event per minute forever, then the file generator would never be executed.

Upvotes: 7

Related Questions