The Message class represents a message in MSMQ. The Message class provides one static method called InfiniteTimeout that specifies that no timeout exists. This class provides more than 40 properties and some of them represent the body text, format, security, ID, and priority of the message. You'll see this class's properties in the sample applications throughout this chapter.
You can use the Message class to create a message. Once you have created a message, you can use it as an argument of the Send and Receive methods of MessageQueue to send and receive messages. The following code creates a message and sets its priority, body, and label:
Dim msg1 As Message = New Message() msg1.Priority = MessagePriority.High msg1.Body = "Body of first message" msg1.Label = "First Message"
The Purge method of MessageQueue removes all the messages from a queue. The following code removes messages from MessageQueue1:
MessageQueue1.Purge()
You can remove a single message from the queue by calling the Receive method of MessageQueue. You can also use the ReceiveById method to remove a message identified by the ID. You can also call the PeekById method to find a specific message and then use the RecieveById method to remove it.
Tip | You can always remove messages manually from the Server Explorer by selecting the Queue Messages Clear Messages menu option. There's no way you can remove a single message from a queue manually. |
You can set both a queue's and a message's priority. Both MessageQueue and Message classes provide properties to set their priorities. You can use the BasePriority property of MesssageQueue to get and set a priority of a queue. It's only applied on public queues. The BasePriority of a private queue is always 0.
You can use the Priority property of the Message class to get and set the priority of a message. The MessagePriority enumeration specifies the value of the Priority property. Table 21-4 describes the MessagePriority enumeration members.
PROPERTY | DESCRIPTION |
---|---|
Lowest | Lowest priority |
VeryLow | Between Low and Lowest message priority |
Low | Low priority |
Normal | Normal priority |
AboveNormal | Between High and Normal message priority |
High | High priority |
VeryHigh | Between Highest and High message priority |
Highest | Highest priority |
The following code creates two messages and sets their priority, body, and label properties:
Dim msg1 As Message = New Message() Dim msg2 As Message = New Message() msg1.Priority = MessagePriority.High msg1.Body = "Body of first message" msg1.Label = "First Message" msg2.Priority = MessagePriority.Normal msg2.Label = "Second Message" msg2.Body = "Body of second message"