Exam 70-554 - Send messages to a message queue and delete messages from a message queue.

Section 1

  • Part 4
    • Topic 3

Send messages to a message queue and delete messages from a message queue.

  • Create a message.
  • Post a message.
  • Receive a message synchronously.
  • Decide which formatter to use.
  • Read a message body.
  • Delete queued messages.

Summary

To send a message to a queue, you need to first create an instance of the MessageQueue class that points to the queue that you wish to send messages to. As part of creating the instance you can specify a formatter by passing it in to the constructor of the Message Queue class. Once you have a handle to the message queue you can construct and send messages to it. When you create an instance of the Message class you can set its body to your message and set a number of different properties to control how the message will be handled

Here is an example from msdn of creating and posting a message to a queue.

System.Messaging.MessageQueue myMQ1 = new System.Messaging.MessageQueue (@".\YourQueue");
System.Messaging.Message newMessage = new System.Messaging.Message("Hello again");
newMessage.Label = "This is the label";
myMQ1.Send(newMessage);

To receive the message from the queue synchronously call the Receive method of the message queue instance pointing to the queue you want to receive a message from. You must pass in a timespan object that defines how long you would like to wait for a message to return. Once you have the message you must ensure you have the right formatter so that the body can be read. Below is an example from msdn for receiving a message:

System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(".\\MyQueue");
mq.Send("1", "1");
System.Messaging.Message m = mq.Receive(new TimeSpan(0,0,3));
m.Formatter = new System.Messaging.XmlMessageFormatter(new string[] {"System.String,mscorlib"});
Console.WriteLine(m.Body);

.Net includes three predefined formatters: XmlMessageFormatter, the default formatter formats messages into human readable xml strings, BinaryMessageFormatter, serialized messages into binary streams that are not human readable, are compact and extremely fast, and the ActiveXMessageFormatter, which persist data into primitive types that can interoperate with previous versions of message queueing, and is still fairly compact and fast.

To delete a message you simply need to receive it. This can be problematic if you need to delete a queued message on a queue that has many valid messages. You can peek through the queue and try using ReceiveByLookupId method of the queue to obtain a specific message.

Other Resources & Links:

Sending Complex Messages
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingqueues.asp

Receiving Messages Programatically
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingqueues.asp

Message Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingqueues.asp

mtelligent reloaded: Tech podcast reviews

Exam 70-554 - Create, delete, and set permissions on a message queue.