Long-running MSMQ message processing. Limiting worker threads count

Viewed 366

The environment

  • The host application is a Windows Service (.NET 3.5);
  • There's no possibility to switch to .NET 4+ framework yet.

The process

  • Processing one message might take up to 5 minutes.
  • Messages are received using an async pattern (ReceiveCompleted event subscription):

    private void OnMessageReceived(object source, ReceiveCompletedEventArgs eventArgs) {
    
        var queue = (MessageQueue)source;
        Message m = null;
    
        try {                
            var processor = new Thread(() => {
                try {
                    // 1. process the message
                    // 2. send a feedback to m's response queue
                }
                catch(Exception ex) {
                    Logger.Log.Error(ex);
                    // 1. send a feedback to m's response queue
                } 
            };
            processor.Start();
        }
        catch(Exception ex) {
            Logger.Log.Error(ex);
        }
    
        queue.BeginReceive();
    }
    

The problem

Spawning separate worker threads should be followed by some limiting I guess.

Let's say I want to process messages using 1-5 worker threads (the maximum), if all available workers are busy, then:

  • the processing for the current message is omitted and feedback is sent to the response queue (message is lost);
  • message is sent back to the queue, feedback is sent (processing is postponed);

Does the part if all available workers are busy mean that I must implement something similar to a thread pool?

I've added to the question. This is due to some code I've seen where Rx Buffering is used. I don't quite understand if this might be applied in my case. Can I utilize Rx in my case?

1 Answers
Related