How to remove message from message queue (only if its well formatted)?
Asked Answered
H

2

1

I want to take message from one queue and send it to database. I want to do it only if it's in specific format.

If i use Receive method directly and some exception occurs while accessing Body of the Message, I lose the message since Receive method of the MessageQueue removes the message from the queue.

To avoid loss of message, now i first Peek the message, and if its well formatted, I use Receive method to remove it from the queue to send it to database.

Code I have written is like this:

 Message msg = _queue.Peek(new TimeSpan(0, 0, LoggingService.Configuration.ReceiveTimeout));

// LogMessage is my own class which is adding some more stuff to original message from MessageQueue                
LogMessage message = null;

                if (msg != null)
                {
                    if (!(msg.Formatter is BinaryMessageFormatter))
                        msg.Formatter = new BinaryMessageFormatter();

                    message = LogMessage.GetLogMessageFromFormattedString((string) msg.Body);

                    // Use Receive method to remove the message from queue. This line will we executed only if the above line does not
                    // throw any exception i.e. if msg.Body does not have any problem
                    Message wellFormattedMsg =
                         _queue.ReceiveById(msg.Id);

                      SendMessageToDatabase(message);
                }

Is this logic right to first using Peek and then Receive? Or is there any other better way f achieving the same thing? Please note that I dont want to get all messages at a time. MessageQueue is non transactional.

Hutment answered 25/2, 2011 at 9:57 Comment(1)
Related How can I remove messages from a queue?Smalley
A
2

This is the same approach that I take when manually dequeuing message one at a time and I've not come across any issues with it.

The one thing that you do not appear to dealing with is how to handle a message on the queue that does not have the required format. Is your intention to leave it on the queue? If so, you might end up with a very large queue and have all sorts of issues with peeking at messages further up the queue that have not yet been expected. It would appear to make more sense to also de-queue those messages that do not have the required format and store them elsewhere if they cannot be deleted.

Amherst answered 25/2, 2011 at 10:32 Comment(3)
I upvoted for you for now. You are right. In my logic further, I move such bad formatted messages to some other queue which is created just to keep such problemetic messages only. With my approach, i was worried if it will cost some considerable performance issue if messages are large in number and we are Peeking in before Receiving everytime.Hutment
If you are always going to either handle the message or move it to another queue, the peek might not be needed as irresepective of the content of the message you will always dequeue it.Amherst
You are right. Peek is not at all necessary. I can achieve same thing by handling exception. If I add this line message = LogMessage.GetLogMessageFromFormattedString((string) msg.Body); in try block and in exception block if i send message as it is, to another queue, i achieve what i want. Thanks for your inputs. I mark it as an answer.Hutment
C
1

"If i use Receive method directly and some exception occurs while accessing Body of the Message, I lose the message since Receive method of the MessageQueue removes the message from the queue."

You should be using transactional receives so that the message returns to the queue when/if the transaction aborts.

Cheers
John Breakwell

Cymatium answered 25/2, 2011 at 12:59 Comment(2)
It's not possible to use transactions since message queue is non transactional as mentioned in the question.Hutment
Sorry - missed that bit at the end. Advice still stands. Is there a reason for not using transactional queues in the first place? You say you want to avoid message loss but are using a non-transactional queue.Cymatium

© 2022 - 2024 — McMap. All rights reserved.