Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producer/Consumer in NServiceBus command handler

Because NServiceBus doesn't seem to support adding a priority mechanism to the message queue, I want to implement this myself.

  • Command handler (Producer):
public void Handle(DoAnAction message)
{
  _messageManager.Insert(message);
}
  • Single Consumer (different thread):
public void Run()
{
  DoAnAction message;
  while (true) 
  {
    if (_messageManager.TryDequeue(out message)) 
    {
      doALongCall(message);
    }
    else 
    {
      Thread.sleep(200);
    }
  }
}

Is this even a good idea to begin with? I don't like the idea that I can lose messages this way.

Update: The use case: we have many clients who can send a message DoAnAction. The handling of this Action takes a while. Problem is when 1 client decides to send 200 DoAnActions, all other clients have to wait 2-3 hours so all these messages can be processed (FIFO). Instead I would like to process these messages in an order based on the client.

So even if client A still has 200 messages to process, when client B sends a message it will get queued next. If client B would send 3 messages, the queue would look like this: B-A, B-A, B-A, A, A, A, ...

like image 458
andy Avatar asked Apr 09 '19 10:04

andy


1 Answers

Often times, I have found that the real reason to do this comes from a business standpoint. In that case, it makes sense to model it in the code to reflect the rules and regulations around the business.

Imagine you have a SendEmail message but you want to 'prioritize' some of the messages based on the customer it is intended for. You can design your system differently so that you have two message types, one the regular SendEmail and one called SendPriorityEmail which goes to a different endpoint/queue. You will need to determine in your code which message to send.

Separating the messages at the root means you have more flexibility (that also come from the business) which comes useful when doing monitoring, SLAs and quality of service type of things when it comes to the more important customers (in this case).

like image 66
Hadi Eskandari Avatar answered Oct 22 '22 19:10

Hadi Eskandari