Receiving a message from the queue

The receiving application can simply invoke the ReceiveMessageRequest constructor and pass the URL of the message queue. This creates a request object that can then be used to invoke the RecieveMessage() function. This function then in turn returns a response object that consists of the messages from the queue. This response object can then be iterated to read all the messages received from the queue.

The following code creates a request object to receive the messages and invokes the RecieveMessage() function. It then iterates over the response object and prints out all the messages received from the queue:

var receive_message_request = new ReceiveMessageRequest { QueueUrl = queue_url };
var received_message_response = sqs_object.ReceiveMessage(receive_message_request);
if (received_message_response.Messages != null)
{
foreach (var message in received_message_response.Messages)
{
if (!string.IsNullOrEmpty(message.MessageId))
{
Console.WriteLine("MessageId : {0}", message.MessageId);
}
if (!string.IsNullOrEmpty(message.ReceiptHandle))
{
Console.WriteLine("ReceiptHandle: {0}", message.ReceiptHandle);
}
if (!string.IsNullOrEmpty(message.MD5OfBody))
{
Console.WriteLine("MD5OfBody: {0}", message.MD5OfBody);
}
if (!string.IsNullOrEmpty(message.Body))
{
Console.WriteLine("Body: {0}", message.Body);
}

foreach (string key in message.Attributes.Keys)
{
Console.WriteLine("Attribute");
Console.WriteLine("Name: {0}", key);
var value = message.Attributes[key];
Console.WriteLine("Value: {0}", string.IsNullOrEmpty(value) ? "(no value)" : value);
}
}
}

Thus, you can create different application components that can exchange messages using the SQS service.

Finally, let's delete the queue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.234.225