In
this blog I am going to give a brief knowledge on AWS SQS and how we can write C# code to access and use AWS SQS.
What is SQS ?
Amazon Simple Queue Service (SQS) is a managed message queuing service that enables to scale and decouple micro-services, distributed systems, and server less applications.
How SQS Works ?
SQS basically exposes an API to publish and consume messages. In SQS a single message will get delivered to single consumer only, even when lot of concurrent consumers are running. When a consumer receives a message, that particular moment it get blocked to be accessed by other consumers.
Explaining two scenarios -
- If we have SSO Login to AWS Console.
- If we have AWS account with IAM login.
1. C# Code to connect with SQS when user have SSO Login -
- Add References AWSSDK.Core, AWSSDK.SimpleNotificationService and AWSSDK.SQS -
- Headers Used -
using System;
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
- Code which will command SQS to create queue and store message, also helps us to fetch the message stored in it -
Complete Code -
using System;
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
namespace WindowsService1
{
class CSahrp_to_SQS
{
public void happy()
{
}
static void Main(String[] args)
{
Console.WriteLine("********************Amazon SQS******************");
AmazonSQSClient sqs = new AmazonSQSClient(RegionEndpoint.USEast1);
var sqsrequest = new CreateQueueRequest { QueueName = "Acknowledgement" };
var createQueueResponse = sqs.CreateQueueAsync(sqsrequest).Result;
var myQueueUrl = createQueueResponse.QueueUrl;
var listQueuesRequest = new ListQueuesRequest();
var listQueuesResponse = sqs.ListQueuesAsync(listQueuesRequest);
Console.WriteLine("List of Amazon SQS");
foreach (var queueUrl in listQueuesResponse.Result.QueueUrls)
{
Console.WriteLine($"QueuesUrl: ", queueUrl);
}
Console.ReadLine();
var queueUrl = sqs.GetQueueUrlAsync("Acknowledgement").Result.QueueUrl;
var recieveMessageRequest = new ReceiveMessageRequest
{
QueueUrl = "https://sqs.us-east-1.amazonaws.com/772486769836/ball"
};
var sendMessageResponse = sqs.SendMessage("https://sqs.us-east-1.amazonaws.com/772486769836/ball", "Message Sent");
var recieveMessageResponse = sqs.ReceiveMessage(recieveMessageRequest);
foreach (var message in recieveMessageResponse.Messages)
{
Console.WriteLine("Message \n");
Console.WriteLine($"Mesaage Id: {message.MessageId} \n");
Console.WriteLine($"Message Body: {message.Body} \n");
Console.WriteLine(message);
}
}
}
}
For any queries please comment below i will try to solve your queries.
Comments
Post a Comment