MessageQueue::ReceiveByCorrelationId Method (String^, TimeSpan)

 

Receives the message that matches the given correlation identifier (from a non-transactional queue) and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public:
Message^ ReceiveByCorrelationId(
	String^ correlationId,
	TimeSpan timeout
)

Parameters

correlationId
Type: System::String^

The CorrelationId of the message to receive.

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.

MessageQueueException

The message with the specified correlationId does not exist in the queue and did not arrive before the time-out expired.

-or-

An error occurred when accessing a Message Queuing method.

This method looks in the non-transactional queue referenced by the MessageQueue for a message whose CorrelationId matches the specified correlationId parameter. This method returns immediately if the message with the correlation identifier specified by the correlationId parameter is in the queue. Otherwise, the method waits the given period of time for a new message to arrive. If a new message does not arrive before the time-out expires, 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 receive messages from a queue. The Receive method returns the first message in the queue, and the ReceiveById(String^) method is used to retrieve a message by specifying its unique identifier.

To read a message with a specified correlation identifier without removing it from the queue, use the PeekByCorrelationId(String^) method. The PeekByCorrelationId(String^) method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue.

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 ReceiveByCorrelationId(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;

// Receive the acknowledgement message from the admin queue.
Message^ ackMsg = adminQueue->ReceiveByCorrelationId(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();

.NET Framework
Available since 1.1
Return to top
Show: