Introduction
Amazon Simple Queue Service (SQS) is a fully managed message queuing service that allows users to exchange and store messages between services. The service places messages into a queue. Users or services then retrieve these messages, and after processing, the messages are deleted from the queue.
The benefits of using SQS are,
- Availability
- Reliability
- Scalability
In this article, we are going to learn about how to poll messages from SQS using C#.
Types of SQS Queue
Two types of SQS queues are available.
- Standard queues
- FIFO queues
Standard Queue
- Standard queues are optimized for high throughput, permitting an unlimited number of transactions per second and ensuring at least one message delivery. However, strict message ordering is not guaranteed with standard queues.
- Standard queues are used in scenarios where speed and scalability take precedence over sequence, such as in large-scale data processing or handling background tasks.
FIFO Queue
- FIFO queues guarantee that messages are delivered in the precise order they are sent, providing exact-once processing.
- FIFO queues are used for workflows where maintaining order accuracy is important, such as bank transactions, order management systems etc.
SQS Message Polling
The ReceiveMessageRequest is a request used to receive messages from a queue. The receiveMessageRequest method takes the below arguments.
- QueueUrl (Mandatory)
- MaxNumberOfMessages (Optional)
- VisibilityTimeout (Optional)
- WaitTimeSeconds (Optional)
- AttributeNames (Optional)
Once a message is successfully processed, it should be deleted from the SQS. To delete the message from the queue, we need to call the delete message method (DeleteMessageAsync) explicitly. The QueueUrl and the messageReceiptHandle are inputted using the DeleteMessageAsync method. The ReceiptHandle is a unique identifier associated with receiving a message. Each time you receive a message (even if the same queue message is received again), the ReceiptHandle will be different.
private IAmazonSQS _sqsClient = new AmazonSQSClient(Amazon.RegionEndpoint.EUWest2);
public async Task FunctionHandlerPollMsg(ILambdaContext context)
{
var messageResponse = await GetMessage();
foreach (var message in messageResponse.Messages)
{
try
{
Console.WriteLine(message.Body);
await DeleteMessage(message);
}
catch (Exception ex)
{
context.Logger.LogError(ex.Message);
}
}
}
public async Task<ReceiveMessageResponse> GetMessage()
{
var request = new ReceiveMessageRequest
{
QueueUrl = _queueUrl
};
return await _sqsClient.ReceiveMessageAsync(request);
}
public async Task DeleteMessage(Message message)
{
var request = new DeleteMessageRequest
{
QueueUrl = _queueUrl,
ReceiptHandle = message.ReceiptHandle
};
var response = await _sqsClient.DeleteMessageAsync(request);
if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"({message.ReceiptHandle}) - Error processing delete message request");
}
}
Join the conversation! Your thoughts help the community grow.