Spring Boot AWS SQS step-by-step

Kavindu Gayan
4 min readDec 25, 2020

--

Introduction

SQS (Simple Queue Service) is a fully manages queuing service that enables you to decouple your application. You can use SQS to send, receive, and store messages. There are two types of queues in SQS. They are Standard and FIFO. Here we are focusing on the Standard queue. If you want to learn more about SQS, please refer: https://aws.amazon.com/sqs/

Data flow

Figure 1

Step 1: create SQS (queue) in AWS

Type SQS in the search bar and select SQS (see Figure 2).

Figure 2

Click the create queue button (see Figure 3).

Figure 3

Select the standard queue and type a queue name and put other configurations as default (see Figure 4).

Figure 4

Finally, the queue has created (see Figure 5).

Figure 5

Step 2: Create an IAM group to access the queue

Under an IAM we have to create a group (see Figure 6).

Figure 6

Type group name (see Figure 7).

Figure 7

Attach policy (AmazonSQSFullAccess ), review and create the group (see Figure 8).

Figure 8

Now we have created the group (see Figure 9).

Figure 9

Step 3: Create the IAM user and attach it to our IAM group

Click the “add user” button under IAM -> users (see Figure 10).

Figure 10

Type the username and tick programmatic access (see Figure 11).

Figure 11

Select the group (see Figure 12).

Figure 12

User credentials will be received after successful user creation. You can download the credentials as a CSV file (see Figure 13).

Figure 13

Step 4: Let’s create the Spring Boot application.

You can use https://start.spring.io/ to generate the application.

Required dependencies in pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
</dependency>

Project structure

Figure 14

Step 4: Create application configurations

application.yml file

cloud:
aws:
region:
static: us-east-1 # Region where you create the queue
auto: false
credentials:
access-key: #access key
secret-key: #acess secret key

Create the Bean of QueueMessagingTemplate. which allows the application to Autowire the QueueMessagingTemplate.

@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSQSAsync());
}

Create the AmazonSQSAsync bean.

@Bean
@Primary
public AmazonSQSAsync amazonSQSAsync() {
return AmazonSQSAsyncClientBuilder
.standard()
.withRegion(region) //region name wich aquired by application.yml file
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(awsAccessKey, awsSecretKey))) // access key and secret key
.build();
}

Step 5: Let’s push messages to the queue.

@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
public void sendMessageToSqs(final Message message) {

queueMessagingTemplate.convertAndSend(QUEUE, message); //send to queue

}

Step 6: Get the message from the queue.

Now we have successfully sent a message to the AWS SQS. The next step is to get the message from the queue. For we use SqsListener, which listens to the messages from the queue.

@SqsListener(value = QUEUE, deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void getMessageFromSqs(Message message, @Header("MessageId") String messageId) {
LOGGER.info("Received message= {} with messageId= {}", message.toString(), messageId);
// TODO - save to db, send SMS, Send email ...etc.
}

Step 7: Let’s run the application and check the logs.

2020–12–25 13:03:45.678 INFO 21076 — — [nio-8089-exec-2] com.aws.sqs.controller.SqsController : Sending the message to the Amazon sqs.
2020–12–25 13:03:47.230 INFO 21076 — — [nio-8089-exec-2] com.aws.sqs.controller.SqsController : Message sent successfully to the Amazon sqs.
2020–12–25 13:03:47.298 INFO 21076 — — [enerContainer-2] com.aws.sqs.controller.SqsController : Received message= Message{id=105, date=Fri Dec 25 13:03:45 IST 2020, messageBody=’null’} with messageId= b82a082b-df25–407a-977d-5a73fbec438c

When a message sent at 13:03:45.678 and SqsListener receive that message at 2020–12–25 13:03:47.298

--

--

No responses yet