How to process MSMQ messages in parallel

2019-03-09 15:39发布

I'm writing a windows service to consume MSMQ messages. The service will have periods of high activity (80k messages coming in very quickly) and long periods of inactivity (could be several days without a new message).

Processing the messages is very network-bound, so I get a big benefit out of parallelism. But during periods of inactivity, I don't want to tie up a bunch of threads waiting for messages that aren't coming anytime soon.

The MSMQ interface seems to be very focused on a synchronous workflow - get one message, process it, get another, etc. How should I structure my code so that I can take advantage of parallelism during periods of high activity but not tie up a bunch of threads during periods of no activity? Bonus points for using the TPL. Pseudocode would be appreciated.

7条回答
叛逆
2楼-- · 2019-03-09 16:19

Just trying somehow similar, tpl seems to be able to throw some sort of thread safety exception where it runs into physical issues, eg try create a sqlconnection outside of the tpl foreach and use it within the loop body - it threw an exception for me. I new up a queue before entering the body, enumerating over a list of strings and it seemed ok, my code was processing 10000 items consistantly under 500 ms using 1way messaging on a i7 2500 8gb and local msmq

查看更多
登录 后发表回答