SQS (Amazon Simple Queue Services)
Amazon SQS (Amazon Simple Queue Services) is a queuing service used for high-throughput, system-to-system messaging. You can use queues to decouple heavyweight processes and to buffer and batch work. Amazon SQS stores messages until microservices and serverless applications process them.
Steps to implementing Amazon SQS
Create a Queue
You will have the provide the name of the Queue to create a simple queue in the Amazon cloud.
Create QueueURL
After this, you have to create a queue URL that would basically build with your configuration you can create this on your own by providing your location, your queue name, etc.
Example - https://{region_name}/queue.|apidomain|/{account_number}/{queue_name}
Note
region_name is one of the most important parts of this queue url else this would process the complete flow but you will not be able to find the queue on amazon console, because of the region mismatch.
Process queue
You need to create a message request with the queue URL and the message body and send it to the available queue.
Receive Message
The last step is to receive the messages sent to the defined queue. You need to create a receive request and call the receive request method of the SQS client.
You can follow the same process for using this simple queue service over the Amazon Console too. You need to login to the console first. After that, you will see a screen like this.
Type "Simple Queue Service" in the textbox and select from the results shown on the screen.
Here, you will have the list of a queue available on your own AWS the above screenshot, I have created two queues named:
- TestingQueue
- TestingQueue1
I am going to show you a working example of the ASQS in .NET using Visual Studio.
Let's begin.
Step 1
Step 2
Create a Windows or web application or whatever you want. I have created a Windows.Form application and have taken two buttons one for creating the queues and other for processing the queue.
Step 3
After this, you need to set up your project to use the Amazon Services. First, you have to specify the Access Key, Secret Key, and Region in web.config if you are using the web application; and in app.config if you are using the Windows application.
- <appSettings>
- <add key="AWSAccessKey" value="*******************" />
- <add key="AWSSecretKey" value="***********************************" />
- <add key="AWSRegion" value="*********" />
- </appSettings>
Remember, the key name must be in the case as mentioned. and make sure you enter the correct region from aws console.
Step 4
After this, you need to install a few of the necessary .dll files from the NuGet Package Manager. You can add these required libraries from the NuGet Package Manager or PM> console by typing the name of the libraries, as shown in the references.
Now, it's time to write a few lines of code for the same.
- using Amazon.SQS;
- using Amazon.SQS.Model;
- using System;
- using System.Linq;
- using System.Windows.Forms;
-
- namespace AmazonSQS
- {
- public partial class Form1 : Form
- {
- public AmazonSQSConfig SqsConfig { get; set; }
- public AmazonSQSClient SqsClient { get; set; }
- public string QueueUrl { get; set; }
- public string MessageBody { get { return "This is a simple message queue test"; }}
- public Form1()
- {
- InitializeComponent();
- SqsConfig = new AmazonSQSConfig
- {
- ServiceURL = "http://sqs.us-east-2.amazonaws.com"
- };
- SqsClient = new AmazonSQSClient(SqsConfig);
- QueueUrl = "https://sqs.*********.amazonaws.com/*************/TestingQueue1";
- }
-
- private void BtnCreateQueue_Click(object sender, EventArgs e)
- {
- var createQueueRequest = new CreateQueueRequest();
- createQueueRequest.QueueName = "TestingQueue123";
-
- var createQueueResponse = SqsClient.CreateQueue(createQueueRequest);
- MessageBox.Show(createQueueRequest.QueueName + " queue created successfully..");
- }
-
- private void BtnProcessQueue_Click(object sender, EventArgs e)
- {
-
- var sendMessageRequest = new SendMessageRequest
- {
- QueueUrl = QueueUrl,
- MessageBody = "This is a simple message queue test"
- };
- var sendMessageResponse = SqsClient.SendMessage(sendMessageRequest);
-
-
- var receiveMessageRequest = new ReceiveMessageRequest
- {
- QueueUrl = QueueUrl
- };
- var receiveMessageResponse = SqsClient.ReceiveMessage(receiveMessageRequest);
- var messages = receiveMessageResponse.Messages;
-
- if (messages.Any())
- {
- for (int i = 0; i < messages.Count(); i++)
- {
- if (messages[i].Body == MessageBody)
- {
- var receiptHandle = messages[i].ReceiptHandle;
- MessageBox.Show(messages[i].Body + "found in Queue");
- }
- }
- }
- else {
- MessageBox.Show("failed to process...");
- }
- }
- }
- }
If you successfully processed the queue you will get the message.body + “found in Queue” message at message box,
If you have any queries or questions, I will be happy to answer.
Happy coding! Enjoy.