The MessageQueue Class

Team Fly 

Page 347

This form of the Send method sends a simple message to the queue. The first argument is the message's body and the second argument is the message's label (a string that will be displayed on the MSMQ snap-in under the Label heading).

To read the message from the queue, call the MessageQueue1 component's Receive method. This method returns a Message object, which exposes many properties. The Body and Label properties return the message's body and label, respectively. Enter the following statements in another button's Click event handler to retrieve the message and display its label and body on a message box:

 Private Sub Button2_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _                           Handles Button2.Click     Dim msg As Message     msg = MessageQueue1.Receive     MsgBox(msg.Label & vbCrLf & msg.Body) End Sub 

The Receive method retrieves the first message in the queue. Usually this is the oldest message in the queue, unless the messages have different priorities. Messages with higher priority are stored ahead of messages with lower priority. You can't set the priority (or many other properties of a message) with the simple form of the Send method shown here. As you will see shortly, you must create a Message object, then set its properties, and finally send it with the Send method. We rarely use this simple form of the Send command; we've only shown it here to simplify the example.

Even simple text messages are first serialized into XML format before they're written to a queue. You can read the XML-serialized description of a message with the BodyStream property of the Message object. The following statements read the XML description of a text message:

 msg = MessageQueue1.Receive Dim buffer(msg.BodyStream.Length - 1) As Byte msg.BodyStream.Read(buffer, 0, msg.BodyStream.Length) Console.WriteLine(System.Text.Encoding.UTF7.GetString(buffer)) 

The Receive method won't return without reading a message. If the queue is currently empty, the Receive method will wait until a new message arrives. We describe how the Receive method is used in the section ''The Message Class," later in this chapter.

The simple code examples we use in the following sections are parts of the SimpleQueue sample application. This application assumes that the ToolkitQueue private queue exists on the local machine. Follow the steps outlined in the section "Creating New Queues" to create the ToolkitQueue on your machine (or edit the properties of the MessageQueue1 object in the application).

The MessageQueue Class

Once you know how to reference queues, you can use the MessageQueue class's methods to create new queues, delete existing ones, and find out whether a specific queue exists or not from within your code. The Exists method of the MessageQueue class returns True if the queue specified with the argument to the method exists, False otherwise. To create a new queue, pass its path or format name as argument to the Create method of the MessageQueue class. The Create method has a second overloaded form

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net