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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • NGINX With Eureka Instead of Spring Cloud Gateway or Zuul
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

Trending

  • Solid Testing Strategies for Salesforce Releases
  • Contextual AI Integration for Agile Product Teams
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Chaos Engineering for Microservices
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Spring Boot Listener for AWS SQS With Spring Cloud

Spring Boot Listener for AWS SQS With Spring Cloud

This tutorial will demonstrate how to set up your Spring Cloud instance to listen for messages on your Amazon SQS queue.

By 
Bill Schneider user avatar
Bill Schneider
·
Sep. 21, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
40.0K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Cloud AWS makes it easy to create a Java method that listens for messages on an Amazon SQS queue.  Between Spring Boot and Spring Cloud, I was surprised by just how little code I needed to make all this happen.

The key is that when you have the right dependencies in your Maven POM, all you have to do is annotate your listener method with  @SqsListener:

@SqsListener("your-queue-name")
public void listen(DataObject message) {
LOG.info("!!!! received message {} {}", message.getFoo(), message.getBar());
}


The dependency on spring-cloud-starter-aws takes care of initializing everything and scanning for annotated methods.

I put a gist on Github to illustrate the full Java class and full Maven POM.

Converting Messages to Java Objects

Your listener method can accept a String as an argument, or it can convert JSON in the message into Java objects.  Spring will use Jackson to convert the incoming JSON to Java, with appropriate annotations:

@JsonCreator
public DataObject(@JsonProperty("foo") String foo, @JsonProperty("bar") String bar) {
this.foo = foo;
this.bar = bar;
}

Command Line Arguments and Authentication

You specify AWS credentials and region through Spring Boot properties. I passed these as command line arguments through Eclipse, where I was debugging locally/not on AWS:

  •  --cloud.aws.region.static=us-east-1 set my region to US East 1 (Northern VA).

  •  --cloud.aws.credentials.useDefaultAwsCredentialsChain=true tells Spring Boot to use the AWS  DefaultAWSCredentialsChain, which will pull credentials from either environment vars or  ~/.aws/credentials file.

  • I also set the environment variable  AWS_PROFILE for the default credentials chain to find my credentials under the correct profile.

If I were running in AWS itself, the EC2 instance metadata could have determined the region automatically, and also provided credentials via the instance profile.

Testing via AWS Console

I used the AWS console to send test messages. The main catch is that if you are using JSON messages and using Spring to automatically deserialize JSON to your objects via @JsonProperty  annotations, you will need to specify the message attribute (header) contentType  with value  application/json. Otherwise, the conversion will fail with an unhelpful error message like "Cannot convert from [java.lang.String] to .... for GenericMessage ...." with no indication why there was a failure.

There is another alternative for reconfiguring the default Spring messaging classes to ignore the contentType  header.

Spring Framework AWS Spring Cloud Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • NGINX With Eureka Instead of Spring Cloud Gateway or Zuul
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

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!