DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Component Tests for Spring Cloud Microservices
  • How To Build Web Service Using Spring Boot 2.x

Trending

  • Monolith: The Good, The Bad and The Ugly
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Code Reviews: Building an AI-Powered GitHub Integration
  1. DZone
  2. Coding
  3. Frameworks
  4. Amazon Simple Queue Service With Spring Boot: How To Send and Receive Messages

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

Learn how to send your Spring Boot messages with confidence.

By 
Sumit Verma user avatar
Sumit Verma
·
Dec. 03, 18 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
55.0K Views

Join the DZone community and get the full member experience.

Join For Free

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)

  • A producer (component 1) sends message A to a queue, and the message is distributed across the Amazon SQS servers redundantly.

  • When a consumer (component 2) is ready to process messages, it consumes messages from the queue, and message A is returned. While message A is being processed, it remains in the queue and isn't returned to subsequent receive requests for the duration of the visibility timeout.

  • The consumer (component 2) deletes message A from the queue to prevent the message from being received and processed again when the visibility timeout expires.

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.

Spring Framework Web Service Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Spring Boot: Testing Service Layer Code with JUnit 5 and Mockito, RESTful Web Services
  • Component Tests for Spring Cloud Microservices
  • How To Build Web Service Using Spring Boot 2.x

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!