需要注意的是在使用打開操作后返回了一個 MSMQQueue 對象。下面是一個典型的新建和刪除操作例子: < % Dim objQueue Set objQueue = Server.CreateObject("MSMQ.MSMQQueueInfo") objQueue.PathName = "./MyQu" objQueue.Create %>
< % Dim objQueue Set objQueue = Server.CreateObject("MSMQ.MSMQQueueInfo") objQueue.PathName = "./MyQu" objQueue.Delete %>
( 2 )、 MSMQQueue
MSMQQueue 類用來描述一個在 MSMQ 服務中打開的隊列。該類提供了一個用來在指針隊列中的消息進行循環的功能。你不能夠打開一個使用了 MSMQQueue 類的隊列要這么干只能夠使用 MSQMQueueInfo (見上例),雖然許多 ASP 運用程序通常使用 MSMQ 來發消息,但是很多時候也需要 ASP 來顯示這個消息的具體內容。
獲取消息的方式有兩種:同步方式,異步方式,但是 ASP 只能夠使用同步方式。這是因為 ASP 不能夠在服務端申明一個 WithEvents 變量。
下面先舉一個異步方式使用 MSMQ 的例子(僅 VB 中) Option Explicit Dim m_objQueueInfo As New MSMQQueueInfo Dim m_objQueue As MSMQQueue Dim WithEvents m_objMSMQEvent As MSMQEvent
PRivate Sub Form_Load() m_objQueueInfo.PathName = "./MyQu" m_objQueueInfo.Label = "My Sample Queue" On Error Resume Next m_objQueueInfo.Create On Error GoTo 0 Set m_objQueue = m_objQueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
Set m_objMSMQEvent = New MSMQEvent m_objQueue.EnableNotification m_objMSMQEvent, MQMSG_CURRENT, 1000 End Sub
Private Sub m_objMSMQEvent_Arrived(ByVal Queue As Object, ByVal Cursor As Long) Dim m_objMessage As MSMQMessage Set m_objMessage = Queue.PeekCurrent MsgBox "Message Received: " & m_objMessage.Label m_objQueue.EnableNotification m_objMSMQEvent, MQMSG_NEXT, 10000 End Sub
Private Sub m_objMSMQEvent_ArrivedError(ByVal Queue As Object, ByVal ErrorCode As Long, ByVal Cursor As Long) MsgBox "Error accorded: " & ErrorCode End Sub
這段代碼首先建立一個隊列(如果它還不存在的話)。然后 m_objMSMQEvent 對象通過調用 EnableNotification 連接到 MSMQQueue 對象。一旦連接到 MSMQEvent 對象 , 接下來需做的僅僅是完成 Arrived 和 Arrived_Error ( 可選的 ) 事件。 Arrived 事件當一個新的消息到達隊列時將被觸發該事件返回兩個指針 , 一個是指向隊列中應該從來開始讀消息的位置,另外一個是當前的位置。如果發生錯誤,將觸發 ArrivedError 事件當同步獲取消息時,會一直等到消息可獲取或則超時時程序才會不被掛起。代碼如下: Public Sub DisplayMessages() Dim objQueueInfo As New MSMQQueueInfo Dim objQueue As MSMQQueue Dim objMessage As MSMQMessage objQueueInfo.PathName = "./MyQu" objQueueInfo.Label = "My Sample Queue"
On Error Resume Next objQueueInfo.Create On Error GoTo 0 Set objQueue = objQueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE) Do While True Set objMessage = objQueue.Peek(, , 1000) If objMessage Is Nothing Then Exit Do MsgBox "Message: " & objMessage.Label Loop MsgBox "No more new messages." objQueue.Close Set objMessage = Nothing Set objQueue = Nothing Set objQueueInfo = Nothing End Sub
( 3 )、 MSMQMessage
MSMQMessage 類支持隊列中消息的所有屬性。 MSMQ 消息有兩個方法和繁多的屬性。其中兩個最主要的屬性是: Body 和 LabeL 。最主要的方法有 Send 。有兩種方法來獲取消息: opening , peeking 。當使用 opening 方式后,該消息將會被刪除掉;當使用 peeking 方式后,該消息仍然保存在隊列中直到它過期。它們的返回值都是指向該消息的指針。下例的代碼將打開一個消息,并顯示其 Body 和 Label Private Sub LookForMessage() Dim objQInfo As New MSMQQueueInfo Dim objQReceive As MSMQQueue Dim objMessage As MSMQMessage objQInfo.PathName = "./test" Set objQReceive = objQInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE) Set objMessage = objQReceive.Receive(, , , 1000) If Not objMessage Is Nothing Then MsgBox objMessage.Label & " - " & objMessage.Body Else Msgbox "Nothing in the queue" End If objQReceive.Close Set objQInfo = Nothing Set objQReceive = Nothing Set objMessage = Nothing End Sub
這段代碼打開一個隊列并在該隊列中查找消息,使用 Receive 方法,主要是設置一個 1000 微秒的超時 , 它告訴 MSMQ1000 微秒后停止查找設置一個非常段的超時的功能主要是用來檢查是否存在消息而不是等候一個消息。也就是說如果你知識想看看是否有消息可以使用該方法。如果無消息,返回的指針為空 (If Not objMessage Is Nothing) 。下面是發送一個消息的代碼: < % Dim objQInfo Dim objQSend Dim objMessage Set objQInfo = Server.CreateObject("MSMQ.MSMQQueueInfo") objQInfo.PathName = "./test" Set objQSend = objQInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) Set objMessage = Server.CreateObject("MSMQ.MSMQMessage") objMessage.Label = "This is the label." objMessage.Body = "This is the body." objMessage.Send objQSend objQSend.Close Set objQInfo = Nothing Set objQSend = Nothing Set objMessage = Nothing %>