Amazon AWS | SQS

What is Amazon SQS?

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message oriented middleware, and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.

 

Benefits

Eliminate administrative overhead

 

With SQS, there is no upfront cost, no need to acquire, install, and configure messaging software, and no time-consuming build-out and maintenance of supporting infrastructure.

 

Reliably deliver messages

 

SQS lets you decouple application components so that they run and fail independently, increasing the overall fault tolerance of the system.

 

Keep sensitive data secure

 

You can use Amazon SQS to exchange sensitive data between applications using server-side encryption (SSE) to encrypt each message body.

 

Scale elastically and cost-effectively

 

SQS scales elastically with your application so you don’t have to worry about capacity planning and pre-provisioning.

 

 

WorkFlow

The following scenario describes the lifecycle of an Amazon SQS message in a queue, from creation to deletion.

 

 

Getting Started with Amazon SQS

Before you begin, complete the steps in Setting Up Amazon SQS.

 

Step 1: Create a Queue

 

  1. Sign in to the Amazon SQS console.

  2. Choose Create New Queue.

  3. On the Create New Queue page, ensure that you're in the correct region and then type the Queue Name.

  4. Standard is selected by default. Choose FIFO.

  5. To create your queue with the default parameters, choose Quick-Create Queue.

 

Your new queue is created and selected in the queue list.

 

Step 2: Send a Message

 

After you create your queue, you can send a message to it. The following example shows sending a message to an existing queue.

 

  1. From the queue list, select the queue that you've created.

  2. From Queue Actions, select Send a Message.

  3. Your message is sent and the Send a Message to QueueName dialog box is displayed, showing the attributes of the sent message.

 

Step 3: Receive and Delete Your Message

 

After you send a message into a queue, you can consume it (retrieve it from the queue). When you request a message from a queue, you can't specify which message to get. Instead, you specify the maximum number of messages (up to 10) that you want to get.

 

Step 4: Delete Your Queue

 

If you don't use an Amazon SQS queue (and don't foresee using it in the near future), it is a best practice to delete it from Amazon SQS.

 

SQS Client


// TsgcHTTPAWS_SQS_Client is the component used for connect to Amazon SQS. 
// Client connects using HTTPs protocol and authenticates using Access Key provided by Amazon.
 
// Before you attempts to connect to SQS service, you must set some data in AWSOptions property.
 
// Region: your endpoint region, example: us-east-1.
// AccessKey: access key provided by Amazon.
// SecretKey: secret key provided by Amazon.
 
// The following methods are supported by SQS client:
 
// AddPermission
// Adds a permission to a queue for a specific principal. This allows sharing access to the queue.
 
// ChangeMessageVisibility
// Changes the visibility timeout of a specified message in a queue to a new value. The default visibility timeout for a message is 30 seconds. 
// The minimum is 0 seconds. The maximum is 12 hours.
 
// ChangeMessageVisibilityBatch
 
// Changes the visibility timeout of multiple messages. This is a batch version of ChangeMessageVisibility. 
// The result of the action on each message is reported individually in the response. You can send up to 10 ChangeMessageVisibility requests.
 
// CreateQueue
// Creates a new standard or FIFO queue. You can pass one or more attributes in the request.
 
  vURL := SQS.CreateQueue('sqs_queue');
  if vURL  '' then
    DoLog('#CreateQueue: ' + vURL);
 
// DeleteMessage
// Deletes the specified message from the specified queue. To select the message to delete, use the ReceiptHandle of the message.
 
  if SQS.DeleteMessage('sqs_queue', '...receipt handle goes here...') then
    DoLog('#DeleteMessage: ok');
  else
    DoLog('#DeleteMessage: error');
 
// DeleteMessageBatch
// Deletes up to ten messages from the specified queue. This is a batch version of DeleteMessage. The result of the action on each message is reported individually in the response.
 
// DeleteQueue
// Deletes the queue specified by the queue name, regardless of the queue's contents.
 
  if SQS.DeleteQueue(txtQueueName.Text) then
    DoLog('#Delete Queue: ok')
  else
    DoLog('#Delete Queue: error');
 
// GetQueueAttributes
// Gets attributes for the specified queue.
 
  oAttributes := TsgcSQSAttributes.Create;
  Try
    if SQS.GetQueueAttributes('sqs_queue', oAttributes) then
    begin
      for i := 0 to oAttributes.Count - 1 do
        DoLog('#Attribute: ' + TsgcSQSAttribute(oAttributes.Item[i])
          .AttributeName + ' ' + TsgcSQSAttribute(oAttributes.Item[i])
          .AttributeValue);
    end
    else
      DoLog('#GetQueueAttributes: error');
  Finally
    FreeAndNil(oAttributes);
  End;
 
// GetQueueUrl
// Returns the URL of an existing Amazon SQS queue.
  
// ListDeadLetterSourceQueues
// Returns a list of your queues that have the RedrivePolicy queue attribute configured with a dead-letter queue.
 
// ListQueueTags
// List all cost allocation tags added to the specified Amazon SQS queue.
 
// PurgeQueue
// Deletes the messages in a queue specified by the QueueName parameter.
 
  if SQS.PurgueQueue('sqs_queue') then
    DoLog('#PurgueQueue: ok')
  else
    DoLog('#PurgueQueue: error');
 
// ReceiveMessage
// Retrieves one or more messages (up to 10), from the specified queue.
 
  oResponses := TsgcSQSReceiveMessageResponses.Create;
  Try
    if SQS.ReceiveMessage('sqs_test', oResponses) then
    begin
      for i := 0 to oResponses.Count - 1 do
      begin
        DoLog('#ReceiveMessage: ' + TsgcSQSReceiveMessageResponse(oResponses.Item[i]).Body);
        FReceiptHandle := TsgcSQSReceiveMessageResponse(oResponses.Item[i]).ReceiptHandle;
      end;
    end;
  Finally
    FreeAndNil(oResponses);
  End;
 
// RemovePermission
// Revokes any permissions in the queue policy that matches the specified Label parameter.
 
// SendMessage
// Delivers a message to the specified queue.
 
  if SQS.SendMessage('sqs_queue', 'My First Message') then
    DoLog('#SendMessage: ok')
  else
    DoLog('#SendMessage: error');
 
// SendMessageBatch
// Delivers up to ten messages to the specified queue. This is a batch version of SendMessage.
 
// SetQueueAttributes
// Sets the value of one or more queue attributes. When you change a queue's attributes, the change can take up to 60 seconds 
// for most of the attributes to propagate throughout the Amazon SQS system. 
 
  oAttributes := TsgcSQSAttributes.Create;
  Try
    oAttributes.AddSQSAttribute(sqsatVisibilityTimeout, '45');
    if SQS.SetQueueAttributes('sqs_queue', oAttributes) then
      DoLog('#SetQueueAttributes: ok')
    else
      DoLog('#SetQueueAttributes: error');
  Finally
    FreeAndNil(oAttributes);
  End;
 
// TagQueue
// Add cost allocation tags to the specified Amazon SQS queue.
 
// UntagQueue
// Remove cost allocation tags from the specified Amazon SQS queue.

 

 

Events

OnSQSBeforeRequest

 

This event is called before sqs component does an HTTP request. You can get access to URL parameter and if Handled parameter is set to True, means component won't do an HTTP request.

 

OnSQSError

 

If there is any error when component do a request, this event will be called with Error Code and Error Description.

 

OnSQSResponse

 

This event is called after an HTTP request with raw response from server.