MessageQueue::PeekByCorrelationId Method (String^, TimeSpan)
Peeks the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.
Assembly: System.Messaging (in System.Messaging.dll)
Parameters
- correlationId
-
Type:
System::String^
The CorrelationId of the message to peek.
- timeout
-
Type:
System::TimeSpan
A TimeSpan that indicates the time to wait until a new message is available for inspection.
Return Value
Type: System.Messaging::Message^The Message whose CorrelationId matches the correlationId parameter passed in.
| Exception | Condition |
|---|---|
| ArgumentNullException | The correlationId parameter is null. |
| ArgumentException | The value specified for the timeout parameter is not valid, possibly timeout is less than TimeSpan::Zero or greater than MessageQueue::InfiniteTimeout. |
| InvalidOperationException | The message with the specified correlationId does not exist in the queue and did not arrive before the time-out expired. |
| MessageQueueException | A message did not arrive before the time-out expired. -or- An error occurred when accessing a Message Queuing method. |
This method looks in the queue referenced by the MessageQueue for a message whose CorrelationId matches the specified correlationId parameter. If no message is found that matches the correlationID parameter, and no new message arrives in the queue within the period specified by the timeout parameter, an exception is thrown.
The timeout parameter does not specify the total running time for this method. Rather, it specifies the time to wait for a new message to arrive in the queue. Each time a new message arrives, this method examines the CorrelationId of the new message to see if it matches the correlationId parameter. If not, this method starts the time-out period over and waits for another new message to arrive. Therefore, if new messages continue to arrive within the time-out period, it is possible for this method to continue running indefinitely, either until the time-out period expires without any new messages arriving, or until a message arrives whose CorrelationId matches the correlationId parameter.
The CorrelationId property is used to tie a message sent to the queue to associated response, report, or acknowledgment messages.
Two other methods allow you to peek messages in a queue. The Peek method returns the first message in the queue, and the PeekById(String^) method is used to retrieve a message by specifying its unique identifier.
The following table shows whether this method is available in various Workgroup modes.
Workgroup mode | Available |
|---|---|
Local computer | Yes |
Local computer and direct format name | Yes |
Remote computer | No |
Remote computer and direct format name | Yes |
The following code example demonstrates the use of PeekByCorrelationId(String^, TimeSpan).
// Connect to a queue on the local computer. MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue"); // Create a new message. Message^ msg = gcnew Message("Example Message Body"); // Designate a queue to receive the acknowledgement message for this // message. msg->AdministrationQueue = gcnew MessageQueue(".\\exampleAdminQueue"); // Set the message to generate an acknowledgement message upon its // arrival. msg->AcknowledgeType = AcknowledgeTypes::PositiveArrival; // Send the message. queue->Send(msg, "Example Message Label"); // Get the message's Id property value. String^ id = msg->Id; // Receive the message from the queue. msg = queue->ReceiveById(id, TimeSpan::FromSeconds(10.0)); // Connect to the admin queue. MessageQueue^ adminQueue = gcnew MessageQueue(".\\exampleAdminQueue"); // Set the admin queue's MessageReadPropertyFilter property to ensure // that the acknowledgement message includes the desired properties. adminQueue->MessageReadPropertyFilter->Acknowledgment = true; adminQueue->MessageReadPropertyFilter->CorrelationId = true; // Peek at the acknowledgement message. Message^ ackMsg = adminQueue->PeekByCorrelationId(id, TimeSpan::FromSeconds(10.0)); // Display the acknowledgement message's property values. Console::WriteLine("Message.Label: {0}", ackMsg->Label); Console::WriteLine("Message.Acknowledgment: {0}", ackMsg->Acknowledgment); Console::WriteLine("Message.CorrelationId: {0}", ackMsg->CorrelationId); adminQueue->Close(); queue->Close();
Available since 1.1