Reputation: 540
Because NServiceBus doesn't seem to support adding a priority mechanism to the message queue, I want to implement this myself.
public void Handle(DoAnAction message)
{
_messageManager.Insert(message);
}
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, ...
Upvotes: 6
Views: 471
Reputation: 2283
You can use Sagas to do what you are looking for, essentially having one saga instance per client ID. The saga serves as a "choke point" and can make sure only N messages at a time are being processed for each client.
This might reduce the throughput below maximum capacity under lower levels of load, but could result in the more "fair" distribution you're trying to achieve.
Does that make sense?
Upvotes: 0
Reputation: 26354
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).
Upvotes: 2