Now we start to get into the details. Before you can use the code that we will show in the rest of this article, you must include a reference to "Microsoft Message Queue Object Library" in your project. We will start by showing you how to send a message. The code shown below illustrates how to post a message to a queue.
Private Sub PostMessage()
Dim QI as New MSMQQueueInfo
Dim RequestQ As MSMQQueue
Dim msg As New MSMQMessage
QI.PathName = "MyServer\QueueName"
Set RequestQ = QI.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
Msg.Label = "Sales Order 1"
Msg.Body = "Customer=Bruce;Product=Pokemon;" & _
"Quantity=1000000"
msg.Send RequestQ
RequestQ.Close
End Sub
First of all, lets' discuss the objects that are used in this code snippet. The
MSMQQueueInfo object represents the entire message queue environment. By setting
the path name, you are indicating the server on which the queue resides ('MyServer')
and the name of the queue ('QueueName'). It is quite possible to have more than
one queue on a server. And when you define a message queue, you should try to
make the names as meaningful as possible to help with the development process
as your use for message queues increases.
By using the Open method, you create an MSMQQueue object that represents the queue specified in PathName. The parameters for Open include the access level for the queue (typically either send or receive) and a locking value that determines if others can have the queue open at the same time (typically, you don't need to open a queue for exclusive access).
The MSMQMessage object represents the message itself. Again, there are a number
of properties available, but the most frequently used ones are Label and Body.
Label associates a text string with the message. This string gets displayed if
you look at the queue details through the MSMQ Explorer. The Body property contains
the text that makes up the message. As you will see shortly, this is a simplistic
view of the message body, but it will do for now.
Finally, the Send method on the MSMQMessage object is used to post the message
to a queue. The MSMQQueue object that is to receive the message is passed as
a parameter. At this point, the message has been placed in the specified queue.
Comments