By sending the MSMQQueueInfo object of a second queue along with a message, the sending application indicates that it expects a response message from the receiving application. The MSMQQueueInfo object is passed in the ResponseQueueInfo property of the message.
When the receiving application reads the message and sees that a response is requested (ResponseQueueInfo is not NULL), it should then return a response message to the queue specified by ResponseQueueInfo. For more information on response queues, see Response Queues.
Set qinfos = query.LookupQueue(Label:="Response Queue") qinfos.Reset Set qinfoResp = qinfos.Next If qinfoResp Is Nothing Then Set qinfoResp = New MSMQQueueInfo qinfoResp.PathName = ".\RespQueue" qinfoResp.Label = "Response Queue" qinfoResp.Create End If
Set qinfos = query.LookupQueue(Label:="Destination Queue") qinfos.Reset Set qinfoDest = qinfos.Next If qinfoDest Is Nothing Then Set qinfoDest = New MSMQQueueInfo qinfoDest.PathName = ".\DestQueue" qinfoDest.Label = "Destination Queue" qinfoDest.Create End If
Set q = qinfoDest.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) msgSent.Label = "Test Message" msgSent.Body = "This message tests the response queue." response = MsgBox("Do you want a response message?", 4) If response = 6 Then Set msgSent.ResponseQueueInfo = qinfoResp End If msgSent.Send q
Set q = qinfoDest.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE) Set msgRead = q.Receive If Not msgRead.ResponseQueueInfo Is Nothing Then Set qResp = msgRead.ResponseQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) msgResp.Label = "Response Message" msgResp.Body = "This is a response message" msgResp.CorrelationId = msgRead.id msgResp.Send qResp MsgBox "A response message was returned to :" + msgRead.ResponseQueueInfo.PathName Else MsgBox "No response was requested." End If
This example locates the response queue and the destination queue (creating them if needed), displays a message box that allows you to request a response message, then sends the message depending on your response.
Next, the example reads the message from the queue, returning a response message if one is requested. If the response message is requested, its correlation identifier, CorrelationId, is set to the message identifier of the original message and the response message is sent.
To try this example using Microsoft® Visual Basic® (version 5.0), paste the code into the code window of a form, and then run the example and click the form.
Dim query As New MSMQQuery Dim qinfos As MSMQQueueInfos Dim qinfoResp, qinfoDest As MSMQQueueInfo Dim qRead, qResp As New MSMQQueue Dim msgSent As New MSMQMessage Dim msgRead As New MSMQMessage Dim msgResp As New MSMQMessage Private Sub Form_Click() '********************************** ' Locate response queue (create one ' if one doesn't exist). '********************************** Set qinfos = query.LookupQueue(Label:="Response Queue") qinfos.Reset Set qinfoResp = qinfos.Next If qinfoResp Is Nothing Then Set qinfoResp = New MSMQQueueInfo qinfoResp.PathName = ".\RespQueue" qinfoResp.Label = "Response Queue" qinfoResp.Create End If '********************************** ' Locate destination queue '(create one if one doesn't exist). '********************************** Set qinfos = query.LookupQueue(Label:="Destination Queue") qinfos.Reset Set qinfoDest = qinfos.Next If qinfoDest Is Nothing Then Set qinfoDest = New MSMQQueueInfo qinfoDest.PathName = ".\DestQueue" qinfoDest.Label = "Destination Queue" qinfoDest.Create End If '************************ ' Open destination queue. '************************ Set q = qinfoDest.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) '************** ' Send Message. '************** msgSent.Label = "Test Message" msgSent.Body = "This message tests the response queue." response = MsgBox("Do you want a response message?", 4) If response = 6 Then Set msgSent.ResponseQueueInfo = qinfoResp End If msgSent.Send q MsgBox "The message was sent. Check the MSMQ Explorer to see the messages in the queue." q.Close '************************************ ' Read the message in the destination ' queue and send response message if ' one is requested. '************************************ Set q = qinfoDest.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE) Set msgRead = q.Receive If Not msgRead.ResponseQueueInfo Is Nothing Then Set qResp = msgRead.ResponseQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) msgResp.Label = "Response Message" msgResp.Body = "This is a response message" msgResp.CorrelationId = msgRead.id msgResp.Send qResp MsgBox "A response message was returned to :" + msgRead.ResponseQueueInfo.PathName Else MsgBox "No response was requested." End If End Sub