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, ...