Set up a Spring Boot app with Amazon SQS

In this article we’re going to create a Spring Boot project using Spring Initializr that will allow us to interact with Amazon SQS queues.

Amazon SQS and Spring Series

This article is part of a series:

  1. Create a queue in Amazon SQS
  2. Set up a Spring Boot app with Amazon SQS
  3. Send messages to an Amazon SQS queue using Spring Boot
  4. Receive messages from an Amazon SQS queue using Spring Boot

Create a new Spring Boot project using Spring Initializr

  1. Go to https://start.spring.io/
  2. Accept the default Project, Language, and Spring Boot version. Enter a descriptive group and artifact name.

    spring-initializr-basics
  3. You can expand the Options section if you need to change the Java version (default is Java 8). I’m using the latest Java version at the time I wrote the article, Java 13.
  4. Add the necessary dependencies.
Spring Initializr dependencies to add
  1. Click Generate.
  2. Save the zip file to your computer. Unzip the file and open the project in your favorite IDE. I’m using IntelliJ IDEA.
Project opened in IntelliJ IDEA

Dependencies

Open the pom.xml file to review the dependencies.

Dependencies in pom.xml

The following are the most important dependencies and how they’ll be used in the project.

ArtifactIdPurpose
spring-cloud-starter-aws-messagingSend and receive messages to/from an Amazon SQS queue
spring-boot-starter-webCreate endpoints to send and receive messages
spring-boot-starter-data-jpaSave messages received from the queue to a database
mysql-connector-javaSave messages to a MySQL database
flyway-coreCreate database tables using a database migration tool
spring-boot-starter-testWrite unit tests and integration tests using libraries such as JUnit Jupiter, Mockito, and various Spring testing libraries

Add Amazon SQS properties

Add the following properties to the application.properties file .

### AWS ###
cloud.aws.credentials.accessKey=your_access_key
cloud.aws.credentials.secretKey=your_secret_key
cloud.aws.region.static=us-east-2
cloud.aws.stack.name=Spring-Sqs

### Add this property to be able to start app before adding a data source ###
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Code language: plaintext (plaintext)

To obtain these values, in AWS go to Cloud Formation > Stacks.

Stack name and region

AWS Cloud Formation Stacks

This view provides you with the stack name and region. Click on the region drop down to get the region name in the format needed for the properties file.

AWS Region selector

Access key and secret key

Select the stack from the list. Click on the Outputs tab to obtain the access key and secret key.

spring-sqs-stack-output

Start the application

Now you’ll be able to start the app successfully:

Spring Boot console output

Leave a Comment