Amazon Simple Queue Service With Spring Boot: How To Send and Receive Messages

What is AWS SQS?

Amazon Simple Queue Service (Amazon SQS) is a distributed message queuing service introduced by Amazon.com in late 2004. 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 problemor [BS1] connectivity between producer and consumer. 

Amazon SQS automatically deletes messages that have been in a queue for more than maximum message retention period. The default message retention period is 4 days. However, you can set the message retention period to a value from 60 seconds to 1,209,600 seconds (14 days) using the SetQueueAttributes action.

Message Lifecycle

Image title

(Image reference from aws.amazon.com)

SQS Types

  1. Standard Queue: Amazon SQS offers standard as the default queue type. Standard queues support a nearly unlimited number of transactions per second (TPS) per action. Standard queues support at-least-once message delivery.
  2. FIFO Queue: FIFO queues are available in the US East (N. Virginia), US East (Ohio), US West (Oregon), EU (Ireland), Asia Pacific (Sydney), and Asia Pacific (Tokyo) regions. FIFO queues have all the capabilities of the standard queue.

FIFO (First-In-First-Out) queues are designed to enhance messaging between applications when the order of operations and events is critical, or where duplicates can't be tolerated, for example:

  1. Ensure that user-entered commands are executed in the right order.

  2. Display the correct product price by sending price modifications in the right order.

  3. Prevent a student from enrolling in a course before registering for an account.

Creating an Amazon SQS 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. The name of a FIFO queue must end with the .fifo suffix.
  4. Standard is selected by default.
  5. Create your queue

Integration AWS SQS with Spring Boot

To send a message to SQS from Spring Boot, we require below dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-messaging</artifactId>    <version>2.0.1.RELEASE</version>
</dependency>


Define the SQS url in application.properties:

sqs.url:https://[EC2-Instance OR region or zone ].amazonaws.com/[20XXXXXXXXXX]/sqs-queue-name

Sending Message to SQS:

final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();  //to create AmazonSQS 
object.sqs.sendMessage(new SendMessageRequest(sqsURL, “This is my first message to SQS”));


sqsURL is the url that you can get from the application.properties. To inject the value from application properties or Environment value, please use the @Value  annotation:

@Value("${sqs.url}")
private String sqsURL;


By using the above code snippets, you can easily send the message to SQS.

Reading Message from SQS (Asynchronous)

@Value("${sqs.url}")
private String sqsURL;
final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
while(true) {
log.info("Receiving messages from MyQueue.\n");
final ReceiveMessageRequest receiveMessageRequest =
new ReceiveMessageRequest(sqsURL)
.withMaxNumberOfMessages(1)
.withWaitTimeSeconds(3);
final List<com.amazonaws.services.sqs.model.Message> messages =   
          sqs.receiveMessage(receiveMessageRequest).getMessages();


By default, it will read a single message from the queue. It won’t read all the message and respond. To get the multiple messages, you need define the number in withMaxNumberOfMessages(1).

Conclusion

So, here we saw that it is very easy to send and receive message to Amazon SQS with Spring Boot. We can build and consume SQS by using the AWS provided libraries and function for our application as per the requirement.

Please have a look at the code implementation.

 

 

 

 

Top