メッセージを受信し、アプリケーションによって処理される MSMQ キューのセットアップがあります。別のプロセスをキューにサブスクライブさせ、メッセージを読み取ってその内容をログに記録したいと考えています。
私はすでにこれを用意していますが、問題は常にキューを覗いていることです。これが実行されているときのサーバーの CPU は約 40% です。mqsvc.exe は 30% で実行され、このアプリは 10% で実行されます。メッセージが来るのを待つだけで、それが通知され、サーバーを常にポーリングせずにログに記録するものが必要です。
Dim lastid As String
Dim objQueue As MessageQueue
Dim strQueueName As String
Public Sub Main()
objQueue = New MessageQueue(strQueueName, QueueAccessMode.SendAndReceive)
Dim propertyFilter As New MessagePropertyFilter
propertyFilter.ArrivedTime = True
propertyFilter.Body = True
propertyFilter.Id = True
propertyFilter.LookupId = True
objQueue.MessageReadPropertyFilter = propertyFilter
objQueue.Formatter = New ActiveXMessageFormatter
AddHandler objQueue.PeekCompleted, AddressOf MessageFound
objQueue.BeginPeek()
end main
Public Sub MessageFound(ByVal s As Object, ByVal args As PeekCompletedEventArgs)
Dim oQueue As MessageQueue
Dim oMessage As Message
' Retrieve the queue from which the message originated
oQueue = CType(s, MessageQueue)
oMessage = oQueue.EndPeek(args.AsyncResult)
If oMessage.LookupId <> lastid Then
' Process the message here
lastid = oMessage.LookupId
' let's write it out
log.write(oMessage)
End If
objQueue.BeginPeek()
End Sub