Implement a delay in C# MessageQueue using MSMQ

Yuen Lian WU 0 Reputation points
2024-10-22T13:52:00.8066667+00:00

Hi All,

I am currently facing a technical problem dealing with MSMQ, C# MessageQueue and Thread.

My system is using MSMQ and C# MessageQueue framework. At the moment it will read message one by one from MSMQ and process it in C#.

There is a business scenario that the message that I am processing will be relying on another message that comes in later (say 5 minutes later). Therefore I need process this message later after the dependant message has persisted into database. 

I want to put the original processing message back to the Queue with delay, retry for 10 times max with 10 seconds interval each. What is the best way for me to implement this logic given my current module is single threaded. It reads message one by one from the Queue in FIFO order.

Current Code:

var message = new System.Messaging.Message(messageBody, formatter)
_myQueue.Send(message)

I am thinking to use a ThreadPool to manage any messages requires a delay but my colleagues strongly against it as current module is single threaded and my separate thread may have adverse effect to it.

Your suggestion is greatly appreciated.

MSMQ

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-10-22T16:08:38.6333333+00:00

    you should create a second retry queue. the entries should have a wrapper with the retry count, timestamp etc, so you can process the logic. check after process each item in the primary queue. you can use peek to see if the first entry in the retry queue is 5 minutes old. process the retry queue until empty of item is not 5 minutes old.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.