Frequently, you would like the sending application to receive a message back from the processing application. This is accomplished by setting up a response queue. This causes the roles of the sending and processing application to be reversed for the return message. In other words, the processing application will post a message, typically containing the result or status of the original request into a message queue. The original sending application will then receive the message and act accordingly.
The creation of a response queue is accomplished through the use of the ResponseQueueInto property of the MSMQMessage object. The code below illustrates how to set the queue up to be used.
Private Sub PostMessage()
Dim QI as New MSMQQueueInfo
Dim RequestQ As MSMQQueue
Dim ResponseQ As New 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"
ResponseQ.PathName = ".\ResponseQueue"
ResponseQ.Create
Set msg.ResponseQueueInfo = ResponseQ
msg.Send RequestQ
RequestQ.Close
End Sub
On the back end, the receiving application must act appropriately and return
a message to the sending process. As you can see, the ResponseQueueInfo property
contains a reference to a valid queue. It is this queue that gets opened and
posted in the same manner as any other queue. An example appears below.
Private Sub ReceiveMessage()
Dim QI as New MSMQQueueInfo
Dim ReceiveQ As MSMQQueue
Dim ResponseQ As MSMQQueue
Dim msg As MSMQMessage
Dim RespMsg As New MSMQMessage
QI.PathName = "MyServer\QueueName"
Set ReceiveQ = QI.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
Set msg = ReceiveQ.Receive(ReceiveTimeout:=1000)
If msg is Nothing Then
MsgBox "No message was received"
End If
If Not msg.ResponseQueueInfo is Nothing Then
Set ResponseQ = msg.ResponseQueueInfo.Open( _
MQ_SEND_ACCESS, MQ_DENY_NONE)
RespMsg.Label = "Response"
RespMsg.Body = "Response Body"
RespMsg.CorrelationId = msg.Id
RespMsg.Send ResponseQ
ResponseQ.Close
End If
ReceiveQ.Close
End Sub
As always, an article of this length cannot cover much more that the concepts
and basic functionality behind a topic as complicated as message queuing. If
you're interested in learning more about this topic, might we suggest a book
that is as close to a bible in this area as you could find. It is "Professional
Windows DNA: Building Distributed Web Applications with VB, COM+, MSMQ, SOAP,
and ASP".
Comments