次の方法で共有


MessageQueue.Receive メソッド (TimeSpan, MessageQueueTransaction)

MessageQueue が参照するトランザクション キューで利用できる最初のメッセージを受信します。キューでメッセージが利用可能になるか、タイムアウトが経過するまで待機します。

Overloads Public Function Receive( _
   ByVal timeout As TimeSpan, _   ByVal transaction As MessageQueueTransaction _) As Message
[C#]
public Message Receive(TimeSpantimeout,MessageQueueTransactiontransaction);
[C++]
public: Message* Receive(TimeSpantimeout,MessageQueueTransaction* transaction);
[JScript]
public function Receive(
   timeout : TimeSpan,transaction : MessageQueueTransaction) : Message;

パラメータ

  • timeout
    新しいメッセージを検査できるようになるまでの待機時間を示す TimeSpan
  • transaction
    MessageQueueTransaction オブジェクト。

戻り値

キューで利用できる最初のメッセージを参照する Message

例外

例外の種類 条件
ArgumentException timeout パラメータに指定した値が不正です。 timeoutTimeSpan.Zero よりも小さいか、 TimeSpan.MaxValue よりも大きい可能性があります。
MessageQueueException タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

または

キューが非トランザクション キューです。

または

メッセージ キューの API にアクセスしたときにエラーが発生しました。

解説

このオーバーロードを使用すると、 transaction パラメータで定義された内部トランザクション コンテキストを使用してトランザクション キューからメッセージを受信し、キューにメッセージがない場合は、指定された時間内に戻ります。

Receive メソッドを使用すると、メッセージを同期的に読み取ることができるため、キューから削除できます。後続の Receive 呼び出しは、キューにある次のメッセージを返します。

このメソッドはトランザクション キューで呼び出されるため、トランザクションが中止されると、受信したメッセージがキューに戻されます。メッセージは、トランザクションがコミットされるまで、キューから恒久的に削除されることはありません。

キューにある最初のメッセージをキューから削除せずに読み取るには、 Peek メソッドを使用します。 Peek メソッドは、常にキューの最初のメッセージを返します。そのため、より優先順位の高いメッセージがそのキューに到達するまで、後続の呼び出しでも同じメッセージが返されます。 Peek の呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 Peek はキューのメッセージを削除しないため、 Abort を呼び出しても何もロールバックされません。

メッセージのキューへの到達を待機している間に、現在のスレッドがブロックされてもいい場合は、 Receive を呼び出します。スレッドは特定の期間、または timeout パラメータの InfiniteTimeout 値を指定した場合は無期限に、ブロックされます。メッセージを待機せずにアプリケーションの処理を継続する必要がある場合は、非同期メソッド BeginReceive を使用します。

このメソッドが各種のワークグループ モードで使用できるかどうかを次の表に示します。

ワークグループ モード 使用可否
ローカル コンピュータ はい
ローカル コンピュータ + 直接書式名 はい
リモート コンピュータ いいえ
リモート コンピュータ + 直接書式名 はい

使用例

 
Imports System
Imports System.Messaging

Namespace MyProject


    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        ' 
        ' This example sends and receives a message from
        ' a transactional queue.
        '**************************************************

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Send a message to a queue.
            myNewQueue.SendMessageTransactional()

            ' Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional()

            Return

        End Sub 'Main


        '**************************************************
        ' Sends a message to a transactional queue.
        '**************************************************

        Public Sub SendMessageTransactional()

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Send a message to the queue.
            If myQueue.Transactional = True Then

                myQueue.Send("My Message Data.", New _
                    MessageQueueTransaction())

            End If

            Return

        End Sub 'SendMessageTransactional


        '**************************************************
        ' Receives a message from the transactional queue.
        '**************************************************

        Public Sub ReceiveMessageTransactional()

            ' Connect to a transactional queue on the local computer.
            Dim myQueue As New MessageQueue(".\myTransactionalQueue")

            ' Set the formatter.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType([String])})

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction()

            Try

                ' Begin the transaction.
                myTransaction.Begin()

                ' Receive the message. 
                ' Wait five seconds for a message to arrive. 
                Dim myMessage As Message = myQueue.Receive(New _
                    TimeSpan(0, 0, 5), myTransaction)
                Dim myOrder As [String] = CType(myMessage.Body, _
                    [String])

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

                ' Handle nontransactional queues.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.TransactionUsage Then

                    Console.WriteLine("Queue is not transactional.")

                Else
                    ' Handle no message arriving in the queue.
                    If e.MessageQueueErrorCode = _
                        MessageQueueErrorCode.IOTimeout Then

                        Console.WriteLine("No message in queue.")

                    End If
                End If

                ' Else catch other sources of a MessageQueueException.

                ' Roll back the transaction.
                myTransaction.Abort()


                ' Catch other exceptions as necessary, such as InvalidOperationException,
                ' thrown when the formatter cannot deserialize the message.

            End Try

            Return

        End Sub 'ReceiveMessageTransactional

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        // 
        // This example sends and receives a message from
        // a transactional queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send a message to a queue.
            myNewQueue.SendMessageTransactional();

            // Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional();

            return;
        }


        //**************************************************
        // Sends a message to a transactional queue.
        //**************************************************
        
        public void SendMessageTransactional()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                myQueue.Send("My Message Data.", new 
                    MessageQueueTransaction());
            }

            return;
        }


        //**************************************************
        // Receives a message from the transactional queue.
        //**************************************************
        
        public  void ReceiveMessageTransactional()
        {
            // Connect to a transactional queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Set the formatter.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});
            
            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            try
            {
                // Begin the transaction.
                myTransaction.Begin();
                
                // Receive the message. 
                // Wait five seconds for a message to arrive. 
                Message myMessage =    myQueue.Receive(new 
                    TimeSpan(0,0,5), myTransaction); 
                
                String myOrder = (String)myMessage.Body;

                // Display message information.
                Console.WriteLine(myOrder);

                // Commit the transaction.
                myTransaction.Commit();

            }
            
            catch (MessageQueueException e)
            {
                // Handle nontransactional queues.
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.TransactionUsage)
                { 
                    Console.WriteLine("Queue is not transactional.");
                }

                // Handle no message arriving in the queue.
                else if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.IOTimeout)
                {
                    Console.WriteLine("No message in queue.");
                }
                
                // Else catch other sources of MessageQueueException.

                // Roll back the transaction.
                myTransaction.Abort();
            }

            // Catch other exceptions as necessary, such as 
            // InvalidOperationException, thrown when the formatter 
            // cannot deserialize the message.

            return;
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

/// <summary>
/// Provides a container class for the example.
/// </summary>
__gc class MyNewQueue 
{
    //*************************************************
    // Sends a message to a transactional queue.
    //*************************************************
public:
    void SendMessageTransactional() 
    {
        // Connect to a queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Send a message to the queue.
        if (myQueue->Transactional == true) 
        {
            myQueue->Send(S"My Message Data.", new MessageQueueTransaction());
        }

        return;
    }

    //*************************************************
    // Receives a message from the transactional queue.
    //*************************************************
public:
    void ReceiveMessageTransactional() 
    {
        // Connect to a transactional queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Set the formatter.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(String);
        myQueue->Formatter = new XmlMessageFormatter( p );

        // Create a transaction.
        MessageQueueTransaction* myTransaction = new MessageQueueTransaction();

        try 
        {
            // Begin the transaction.
            myTransaction->Begin();

            // Receive the message. 
            // Wait five seconds for a message to arrive. 
            Message* myMessage = myQueue->Receive(TimeSpan(0, 0, 5), myTransaction); 

            String* myOrder = static_cast<String*>(myMessage->Body);

            // Display message information.
            Console::WriteLine(myOrder);

            // Commit the transaction.
            myTransaction->Commit();

        } 
        catch (MessageQueueException* e) 
        {
            // Handle nontransactional queues.
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::TransactionUsage) 
            { 
                Console::WriteLine(S"Queue is not transactional.");
            }

            // Handle no message arriving in the queue.
            else if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::IOTimeout) 
            {
                Console::WriteLine(S"No message in queue.");
            }

            // Else catch other sources of MessageQueueException.

            // Roll back the transaction.
            myTransaction->Abort();
        }

        // Catch other exceptions as necessary, such as 
        // InvalidOperationException, thrown when the formatter 
        // cannot deserialize the message.

        return;
    }
};

//*************************************************
// Provides an entry point into the application.
// 
// This example sends and receives a message from
// a transactional queue.
//*************************************************

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Send a message to a queue.
    myNewQueue->SendMessageTransactional();

    // Receive a message from a queue.
    myNewQueue->ReceiveMessageTransactional();

    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

MessageQueue クラス | MessageQueue メンバ | System.Messaging 名前空間 | MessageQueue.Receive オーバーロードの一覧 | MessageQueueTransaction | Transactional | InfiniteTimeout | ReceiveById | ReceiveByCorrelationId | Peek | BeginReceive