2

メッセージを受信し、アプリケーションによって処理される 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
4

5 に答える 5

4

MSMQEvent.Arrivedを使用してメッセージを追跡してみましたか?

MSMQEvent オブジェクトの Arrived イベントは、開いているキューを表す MSMQQueue オブジェクトのインスタンスの MSMQQueue.EnableNotification メソッドが呼び出され、メッセージが見つかったとき、またはキュー内の該当する位置に到着したときに発生します。

于 2010-05-13T14:12:40.427 に答える
3

ピークの反復の間にThread.Sleep(10)を指定すると、大量のサイクルを節約できる場合があります。

私が考えることができる他の唯一のオプションは、キュー読み取りアプリケーションにロギングを組み込むことです。

于 2010-05-03T19:09:11.593 に答える
1

各メッセージを 1 回だけ確認できる API はありません。

問題はBeginPeek、キューにメッセージが既にある場合にコールバックをすぐに実行することです。メッセージを削除していないため (これは結局のところピークMessageFoundであり、受信ではありません!)、コールバックが再びピークを開始すると、プロセスが最初からやり直され、ほぼ常に実行されます。

最適なオプションは、メッセージをライターまたはリーダーに記録することです。ジャーナリングは短期間は機能しますが (受信したメッセージのみを気にする場合)、長期的な解決策にはなりません:

ジャーナリング用に構成されたキューからメッセージを取得する場合のパフォーマンス オーバーヘッドは、ジャーナリングなしでメッセージを取得する場合よりも約 20% しか大きくありませんが、実際のコストは、チェックされていない MSMQ サービスがメモリ不足になった場合、またはマシンのディスクが不足した場合に発生する予期しない問題です。スペース

于 2010-05-03T19:38:25.897 に答える
0

これは私にとってはうまくいきます。メッセージを待っている間、スレッドをブロックします。各ループ サイクルは、クラス メンバー_bServiceRunningをチェックして、スレッドを中止する必要があるかどうかを確認します。

    private void ProcessMessageQueue(MessageQueue taskQueue)
    {
        // Set the formatter to indicate body contains a binary message:
        taskQueue.Formatter = new BinaryMessageFormatter();

        // Specify to retrieve selected properties.
        MessagePropertyFilter myFilter = new MessagePropertyFilter();
        myFilter.SetAll();
        taskQueue.MessageReadPropertyFilter = myFilter;

        TimeSpan tsQueueReceiveTimeout = new TimeSpan(0, 0, 10); // 10 seconds

        // Monitor the MSMQ until the service is stopped:
        while (_bServiceRunning)
        {
            rxMessage = null;

            // Listen to the queue for the configured duration:
            try
            {
                // See if a message is available, and if so remove if from the queue if any required
                // web service is available:
                taskQueue.Peek(tsQueueReceiveTimeout);

                // If an IOTimeout was not thrown, there is a message in the queue
                // Get all the messages; this does not remove any messages
                Message[] arrMessages = taskQueue.GetAllMessages();

                // TODO: process the message objects here;
                //       they are copies of the messages in the queue
                //       Note that subsequent calls will return the same messages if they are
                //       still on the queue, so use some structure defined in an outer block
                //       to identify messages already processed.

            }
            catch (MessageQueueException mqe)
            {
                if (mqe.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    // The peek message time-out has expired; there are no messages waiting in the queue
                    continue; // at "while (_bServiceRunning)"
                }
                else
                {
                    ErrorNotification.AppLogError("MSMQ Receive Failed for queue: " + mqs.Name, mqe);
                    break; // from "while (_bServiceRunning)"
                }
            }
            catch (Exception ex)
            {
                ErrorNotification.AppLogError("MSMQ Receive Failed for queue: " + mqs.Name, ex);
                break; // from "while (_bServiceRunning)"
            }
        }

    } // ProcessMessageQueue()
于 2010-05-03T20:03:59.223 に答える