views:

276

answers:

1

hi.

i am sending messages in MSMQ by setting its priority. using C#

can i get the message from MSMQ having high priority first?

just like we get in Priority Queue.

and one thing more..

suppose there are three priority level

0 - high 1- medium 2 - low

the sequence in queue is 2001122221111100

now if i send message with high priority(0) where it will be placed?? by setting priority in MSMQ. will it behave like actual Priority Queue?

+3  A: 

MSMQ does support priority queuing of messages, however messages of the same priority are handled in order of arrival when dequeued. For example, if you send 3 messages, two of priority 7 and one of priority 0, then the first message of priority 7 that was received will be dequeued, followed by the second message of priority 7 that was received, finally followed by the message priority 0. You should not have to do anything special to process queued messages in their priority order...however just be aware that the "oldest" message of any given priority will be dequeued before the "newest" message of the same priority. It should also be noted that any transactional messages ignore their priority, IIRC.

EDIT:

While MSMQ supports priorities, it will not behave exactly like a priority queue. The two are different algorithms, with MSMQ being significantly more complex. When you set the priority of a message, not only does that help determine the order in which that message will be dequeued, it also affects the priority at which that message will propagate through the MSMQ service from sender/publisher to receiver/subscriber. Assuming you use the three lowest priorities (MSMQ supports 8 priorities, from 0 (lowest) to 7 (highest)), the following scenario might occur:

0 = low, 1 = medium, 2 = high

Sender sends messages with the given priorities at the specified times (minute:second):

0 @ 1:00  
2 @ 1:00
0 @ 1:01
1 @ 1:02
1 @ 1:03
0 @ 2:01
2 @ 2:01

Receiver queues up messages in its queue in the following order (assuming no messages are dequeued):

2 @ 1:00
2 @ 2:01
1 @ 1:02
1 @ 1:03
0 @ 1:00
0 @ 1:01
0 @ 2:01

When you process messages from the receiver's queue, they will be processed in both order of priority, as well as the time received.

jrista
suppose there are two messageslow, highand there comes a new message with "highest" priority. where it will be placed? and will this message be dequed first?
Mohsan
Based on your edited question, I think you have priority values backwards. In MSMQ, the higher the priority number, the higher the priority. That would make 0 = low, 1 = medium, 2 = high. In the event that you wanted an additional "highest" priority, you would have 3 = highest. If a message comes in with priority 3 (highest), it should get dequeued first, before any other message with any of the other three listed priorities.
jrista
Final note...MSMQ will never behave exactly like a priority queue. A priority queue is a much simpler algorithm than MSMQ's message ordering. Please see my updated answer for a better description of how MSMQ handles messages waiting to be dequeued.
jrista
got it. thanks and my problem is solved now:)
Mohsan
Great! :D Glad you solved the problem.
jrista