Amazon SQS (Simple Queue Service) – Glue to build applications in cloud

Amazon SQS (Simple Queue Service) – Glue to build applications in cloud

Introduction

Amazon SQS is a messaging queue service that handles message or workflows between other components in a system. Amazon SQS, queues up work to run asynchronously on distributed Amazon EC2 nodes. It offloads the administrative burden of operating and scaling a highly available message cluster.

 Use case

Let’s have a use case on how to publish and consume a message and then delete the message (after processing request) from that queue in Amazon SQS using Java.

What we need to do:

  • Produce Message
  • Consume Message

Solution

Pre-requisites:

  • Get the Access Key and secret  Key by following the steps given below
    • Go to Amazon IAM Console https://console.aws.amazon.com/iam/home?#home
    • Select a user name from the navigation menu Users
    • Click on User Action -> Manage Access Keys -> Create Access Key
    • It will generate access key and secret Key. You can also download the credentials

Produce Message:

 

Consume Message:

 

  • SQSProducerCredentials.properties

accessKey=acceskeyOfamazonSQS
secretKey= SecretKeyOfamazonSQS
queue_uri=AmazonQueueURIToSendtheMessage
message=yourmessage

  • SQSConsumerCredentials.properties

accessKey=acceskeyOfamazonSQS
secretKey= SecretKeyOfamazonSQS
queue_uri=AmazonQueueURIToListen
number_of_message=10
wait_time_second=1

Challenges:

  • While consuming message there is chance of retrieving the same message more than one time, if the message is not deleted from the queue after processing. This happens only when the message is still present in the queue even after it’s has been retrieved by the consumer. So we have to make sure we delete the message once the consumer retrieves it.  This is to acknowledge that the message has been processed.
  • We can also set time delay to consume message in consumer by adding “withWaitTimeSeconds(seconds)” property but the time delay should not exceed 15minutes, if it does then the message will not be consumed.

Conclusion

Amazon SQS is a distributed queue messaging service.  It supports programmatic sending of messages via web service applications as a way to communicate over the Internet. SQS is intended to provide a highly scalable hosted message queue that resolves issues arising from the common producer-consumer problem or connectivity between producer and consumer.

This is one type of achieving the producer-consumer activities with the help of this use case.

We have used polls on SQS using thread which is expensive; it would be great if we use an alternative code like (listener) instead of thread ex which can subscribe to the queue and take necessary action when new message arrives.

Reference

 

4408 Views 1 Views Today