In this blog post, we will understand how to send Amazon SQS (Simple Queue Service) messages from spring boot application. This helps you understand how to integrate Amazon SQS with Spring Boot application so that we can send messages to the queue.
Before creating queues, let’s understand a few things about Amazon SQS:
- Amazon Simple Queue Service is a fully managed job queue with messages stored across multiple data centers.
- SQS can send, receive or delete up to 10 messages at a time or in once batch to save cost.
- Messages can be processed only once per visibility timeout, once processed, delete them from the queue. If they fail, they are available for the process again.
- Messages can be retained from 1 minute to 14 days (default is 4 days).
- Messages that are failing can be placed in queue called Dead Letter Queue which can be used to analyze the cause.
- Each message has a global unique ID, Sent Timestamp and can contain up to 10 attributes of metadata (string, binary or number).
- SQS doesn’t guarantee FIFO access as they are distributed in nature.
- Queue names are limited to 80 alphanumeric character including hyphens and underscores and no special characters are allowed.
- Queues can be shared across different accounts within the same region to send/receive messages.
Let’s understand more about visibility timeout
Amazon SQS doesn’t automatically delete the message once the consumer receives and processes message from the queue. As SQS are distributed in nature, they can be shared across multiple consumers to consume the messages.
To prevent other consumers from processing the message again, the message is hidden from other consumers for a specific period of time. The time window is called Visibility Timeout. Consumer should make a delete call to delete the messages from queue. Amazon SQS requires you to delete the message, just that in JMS world, the consumer acknowledges that it is done processing and the JMS broker takes care of removing the message from queue after processing or consumed by consumer.
Deletion should happen within that message visibility timeout period because it is during this period that the message is not available to other consumers. Once the message is visible again, there may be chances for reprocessing it.
Below diagram explains the concept of Message Visibility Timeout
Implementing visibility timeout
The visibility timeout can be set at queue level as shown below:
The default visibility timeout for SQS is 30 seconds which is configured at the time of queue creation and configured at queue level. This applies to all messages in a queue.
How to create Amazon SQS
- Sign into AWS console account. In find services search with SQS.
- Click on create new queue.
Below are the main differences between standard queue and FIFO queue. You can select either FIFO queue or Standard queue as per your requirement.
Message Order
Standard Queues Messages are delivered in random order as they are sent, chances that more than once copy of message might be delivered.
FIFO Queues As the name itself tells us, this queue offers FIRST in FIRST out delivery and processed exactly once the order is strictly preserved.
Delivery
Standard Queues will allow us to send duplicated messages and delivered at least once.
FIFI Queues Delivers messages exactly once, doesn’t allow duplicated in the queue.
Regions
Standard Queues Available across all the regions
FIFO queues are available in limited regions US West (Oregon), US East (Ohio), US East (N. Virginia), and EU (Ireland).
Once queue is created you will get queue details as shown below:
You need to add permissions to queue. You can’t add/delete messages from queue until there are no permissions set to the queue.
Each queue has its own URL. Make a note the queue URL as we need in our application:
Below are the examples of queue URLs. Two different types of queue URLs are generated.
For standard queue, please refer to the URL below:
https://sqs.us-east-2.amazonaws.com/014348870670/StandardDemoQueue
For FIFO queue URL, please refer to the URL below:
https://sqs.us-east-2.amazonaws.com/014348870670/test.fifo
FIFO queue URL ends with .FIFO as extension
Queue URL will contain region, account ID, and queue name. In most of the cases queue region will be us-east-1 and us-east-2 https://sqs.[region].amazonaws.com/[account_id]/[queue_name]
Now that we have covered the process of queue creation in AWS, let’s move on learning how to integrate it using our spring boot application.
In order to integrate Java application with AWS console, we need to add amazon SDK in application.
Amazon SDK is available in maven repository. Add below dependency in POM.xml file.
Let’s add the following properties:
cloud.aws.stack.auto=false
cloud.aws.credentials.useDefaultAwsCredentialsChain=true
cloud.aws.region.static = us-east-2
cloud.aws.credentials.access-key=
cloud.aws.credentials.secret-key=
cloud.aws.sqsurl=https://sqs.us-east-2.amazonaws.com/014348870670/demotes
cloud.aws.stack.auto
- Enable/Disable the automatic stack name detection in the application.
- In order to interact with the stack resources, Spring Cloud AWS allows developers to work with logical names instead of the random physical ones.
useDefaultAwsCredentialsChain This chain looks for credentials in multiple places in the following order:
- Environment variables–AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
- Java system properties–aws.accessKeyId and aws.secretKey.
- Credentials files by using AWS provide by AWS CLI.
- ECS container credentials which is loaded from Amazon ECS if the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set.
- Instance profile credentials– used on EC2 instances, and delivered through the Amazon EC2 metadata service. You can specify the IP address for this value.
Configure SQS client as shown below with Basic Credentials object:
Now we have to call sendMessage() using SQSClient
Hope this blog post helped you understand how to send messages to Amazon SQS with Spring Boot. SQS removes dependencies between the application components and is used to coordinate between micro services, reliable for the both small and large-scale applications.
Author
Sujani Nekkanti works as a Technical Associate at Evoke Technologies and has 4+ years of experience in developing applications based on Java and J2EE technologie. She loves to spend her leisure time exploring emerging technologies and blogging. |
1 Comment
I have several questions, do you have the code on github?