Search code examples
c#msmq

Multithreaded MSMQ Listening


I'm currently reading from my MSMQ like so (simplified for brevity):

public void Start()
{
    this.queue.ReceiveCompleted += this.ReceiveCompleted;
    this.queue.BeginReceive();
}

void ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
    this.queue.EndReceive(e.AsyncResult);
            
    try
    {
        var m = e.Message;
        m.Formatter = this.formatter;

        this.Handle(m.Body);
    }
    finally
    {
        this.queue.BeginReceive();
    }
}

However this only allows me to process messages in serial. How do I modify this code to allow parallel message processing?

I know I can move the this.queue.BeginReceive(); out of the finally and into the top of the ReceiveCompleted but whats to stop that spawning as many threads as I have messages? How do I sensibly control the level of parallelism so I don't flood the threadpool? Is there some inbuilt mechanism for this, or do I have to write my own manager?

Edit

My aim is to process messages faster. The processing of the messages involves an async call to a third party, so currently my implementation is wasting a lot of time in getting through the queue.


Solution

  • I think it would be simpler to just host more instances of your queue reader. Then you can quickly scale up and down depending on need by deploying/undeploying more instances.

    Also it becomes a management, rather than a development concern, which is what scaling should be.