We have situation where we need to synchronize orders and order states to an ERP system. Everything is built around MassTransit 3 and RabbitMQ. An application consumes order related events and talks to the ERP systems API. For example, orders are created when consuming the OrderApproved event, and there’s events consumed for changing order states, and managing the whole order flow as well. So far so good.
Now we need to batch order creation and processing in the ERP system. The ERP systems API supports batching functionality, so the question is how to pick up all these order related events and build the API request to begin batch importing the orders. The batch import job should run every X minutes, importing a maximum of Y orders.
I have considered two scenarios.
Consume each message as we are today, but store the messages in a SQL table, ready to be processed by the batch import job, taking takes the oldest Y records, and builds the API request.
The other option is to utilize RabbitMQ. We could setup a binding that places order related messages into a separate queue, waiting for the batch job to execute. The batch job should then take the oldest Y messages (RMQ queues are FIFO based, right?) and build the API request.
I like this idea the best, but I am unsure about how to implement such functionality with MassTransit. I am thinking that the batch job should start a bus control with a receive endpoint pointing at the queue. However, how can I make sure that I only retrieve Y messages? Is there any functionality to retrieve “sets of messages”, or should I just use regular message handlers and then stop the bus control when some shared counter reaches Y? How would you approach this problem?