<% Dim objQueueInfo Dim objQueue Set objQueueInfo=Server.CreateObject("MSMQ.MSMQQueueInfo") objQueue.PathName = ".\MyQu" Set objQueue = objQueueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) %>
<% 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 %>
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
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
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
<% 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 %>
对于 MSMQ 的有关基本知识我们已做了举例介绍,下面将就它的具体运用进行的了解及探讨!
在运用程序中 MSMQ 可以有很多的运用,但是最普遍的运用是卸载另一个线程中的进程。 ( 例如和 MSMQ 在同一台机器上的 IIS) 或则是另外一台机器上的 IIS. 通过卸载这些阻塞的进程,就能够使得 ASP 程序能够继续运行下去了。
一般来说,判断是否需要卸载任务进程要做两件事:
一是根据该进程运行的时间。
二是根据用户是否有回应(例如聊天室里面某个用户几个小时都没说话了)。
例如:如果服务端的一个 Web 网页的任务花费了太长的时间 , 用户会得到一个网页超时的错误信息,我们一般可以通过重新单击刷新或者简单的放弃这个网页。但现在可以改变一下处理方式,例如进行后台处理,而不是简单的靠增加处理网页的时间方式。要知道,后台处理方式也能够提高网站性能的。
MSMQ 还有一个功能,就是能够控制消息的 body 中特定的一个 COM 对象。只要该对象支持 Idispatch 和 Ipersist (IPersistStream or IPersistStorage ) 接口就行。
其中最常用的,能支持的两个就是 ADODB.Recordset ( 或 ADOR.Recordset ) 和 Word.Document 。在下面我们举一个处理 ADODB.Recordset 例子。
例:如何处理 ADODB.Recordset :
Public Sub SendRecordsetInMessage() Dim objQInfo As New MSMQ.MSMQQueueInfo Dim objQSend As MSMQ.MSMQQueue Dim objMessage As New MSMQ.MSMQMessage Dim objRS As New ADOR.Recordset Dim a As New MSMQQueue With objRS .CursorLocation = adUseClient .Fields.Append "FN", adVarChar, 25 .Fields.Append "LN", adVarChar, 25 .Open .AddNew .Fields("FN") = "Chris" .Fields("LN") = "Blexrud" .Update .AddNew .Fields("FN") = "Shayna" .Fields("LN") = "Blexrud" .Update End With objQInfo.PathName = ".\test" Set objQSend = objQInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE) objMessage.Label = "Recordset State!!!!" objMessage.Body = objRS objMessage.Send objQSend objQSend.Close Set objQInfo = Nothing Set objQSend = Nothing Set objMessage = Nothing Set objRS = Nothing End Sub