Spring Cloud AWS - Messaging Support
Last Updated :
28 Apr, 2025
Spring Cloud for AWS integration process with hosted Amazon Web Services is made easier. It provides an easy means to use popular Spring idioms and APIs, like the messaging or caching API, to interface with services supplied by AWS. The hosted services allow developers to focus on developing their applications rather than worrying about infrastructure or upkeep. On the Amazon Web Services platform, Amazon SQS is a hosted messaging service that overtures point-to-point conversation with queues.
Implementation of Messaging Support in Spring Cloud AWS
There are a few steps that need to be configured to enable Messaging Support in Spring Cloud AWS
1. Add Spring Cloud AWS Dependency
First, add the appropriate maven/gradle dependencies for messaging support in pom.xml
. For SQS (Simple Queue Service), we need to add spring-cloud-starter-aws-messaging
.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
2. Annotation-based SNS Listener
Annotation-based SNS Listener manages incoming notifications and subscription confirmations for AWS SNS (Simple Notification Service) using appropriate annotations.
Java
@Controller
@RequestMapping("/sns/receive")
public class SnsEndpointController
{
@NotificationMessageMapping
public void receiveNotification(@NotificationMessage String message, @NotificationSubject String subject)
{
// ...
}
@NotificationSubscriptionMapping
public void confirmSubscription(NotificationStatus notificationStatus)
{
notificationStatus.confirmSubscription();
}
The
receiveNotification
method annotated with @NotificationMessageMapping that handles notification messages. It takes message content and subject as parameters.The
confirmSubscription
method annotated with @NotificationSubscriptionMapping handles subscription confirmation requests by confirming the subscription state.
3. Simple Queue Service Configuration
Now we will configure SQS queue URL and AWS region in application.properties
.
cloud.aws.region.static=us-west-2 //can add region based on specification
cloud.aws.sqs.queue.url=https://sqs.us-west-2.amazonaws.com/123456789012/my-queue
4. SQS Integration
To integrate with Amazon SQS, we need to add @EnableSqs
annotation to configure a SimpleMessageListenerContainer
to listen to SQS queues. We can define listeners to process messages received from the queues.
Java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.aws.messaging.config.annotation.EnableSqs;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
@SpringBootApplication
@EnableSqs
public class SQSListenerApplication {
public static void main(String[] args) {
SpringApplication.run(SQSListenerApplication.class, args);
}
@SqsListener("my-sqs-queue")
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
// Process the received message
}
}
5. Use the convertAndSend()
We may use the convertAndSend() function to transmit the messages:
Java
@Autowired
QueueMessagingTemplate messagingTemplate;
public void send(String topicName, Object message) {
messagingTemplate.convertAndSend(topicName, message);
}
6. Simple Notification Service
Now we will configure SNS (Simple Notification Service) topic ARN and AWS region in application.properties
.
cloud.aws.region.static=us-west-2
cloud.aws.sns.topic.arn=arn:aws:sns:us-west-2:123456789012:my-topic
7. SNS Integration
To integrate with Amazon SNS and configure a SimpleMessageListenerContainer to listen to SNS messages in Spring Cloud AWS, we need to add the @EnableSns
annotation. This annotation enables the necessary infrastructure for handling SNS messages in your Spring Boot application.
Java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.aws.messaging.config.annotation.EnableSns;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.cloud.aws.messaging.listener.annotation.SnsListener;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableSns
public class AwsSnsApplication {
public static void main(String[] args) {
SpringApplication.run(AwsSnsApplication.class, args);
}
@SnsListener("arn:aws:sns:region:account-id:topic-name")
public void receiveMessage(String message) {
System.out.println("Received message from SNS: " + message);
// received message process
}
}
Note: Replace the placeholder values (us-west-2
, 123456789012
, my-queue
, my-topic
) with your actual AWS region, account ID, queue/queue URL, and SNS topic ARN respectively
We can also publish messages to a subject using NotificationMessagingTemplate, just like we do with SQS. We need an AmazonSNS client in order to generate it:
Java
// Configuration method annotated with @Bean to create a NotificationMessagingTemplate
@Bean
public NotificationMessagingTemplate notificationMessagingTemplate(
AmazonSNS amazonSNS) {
// Create and return a new NotificationMessagingTemplate with the provided AmazonSNS instance
return new NotificationMessagingTemplate(amazonSNS);
}
8. Configure the endpoints
The project only supports HTTP(S), not the other numerous SNS endpoints (SQS, HTTP(S), email, and SMS) that AWS provides.An MVC controller's endpoints may be configured.On the controller level, the subject name must be added to the @RequestMapping annotation.
Java
@Controller
@RequestMapping("/topic-subscriber")
public class SNSEndpointController {
// Method mapped to handle confirmation of unsubscribe messages
@NotificationSubscriptionMapping
public void confirmUnsubscribeMessage(NotificationStatus notificationStatus) {
// Confirm the subscription status when receiving an unsubscribe message
notificationStatus.confirmSubscription();
}
// Method mapped to handle incoming notification messages
@NotificationMessageMapping
public void receiveNotification(@NotificationMessage String message,
@NotificationSubject String subject) {
// Handle the received notification message
// (Actual implementation logic should be added here)
}
// Method mapped to handle confirmation of subscription messages
@NotificationUnsubscribeConfirmationMapping
public void confirmSubscriptionMessage(NotificationStatus notificationStatus) {
// Confirm the subscription status when receiving a subscription confirmation message
notificationStatus.confirmSubscription();
}
}
confirmUnsubscribeMessage:
This
method confirms the subscription status on receiving an unsubscribe message.receiveNotification
: This
method handles incoming notification messages.confirmSubscriptionMessage
: This
method confirms the subscription status upon receiving a subscription confirmation message.
Similar Reads
What Is Spring AWS Cloud ?
Nowadays, cloud computing has been involved in almost all application development. Many big and startup companies prefer to use the cloud as it is very efficient and easy to set up their infrastructure. When it comes to Java development, Spring and Spring boot frameworks have been preferred by many
14 min read
Spring Cloud AWS - S3
In Spring Boot, Spring Cloud AWS can provide integration with the Amazon Web Service (AWS), and it can include the Amazon Storage Service(S3). When it's working with the S3 in the Spring Cloud AWS application. This integration can allow the developers to easily interact with the S3 buckets and the o
9 min read
Spring Cloud AWS - EC2
The Spring Cloud for AWS (Amazon Web Services) component makes integrating with hosted Amazon Web Services easier. It provides an easy method to use popular Spring idioms and APIs, such as the messaging or caching API, to communicate with AWS-provided services. Without worrying about infrastructure
3 min read
Spring Cloud AWS - RDS
The fundamental component of Spring Cloud AWS, Spring Cloud AWS Core offers fundamental security and configuration setup services. This module will be used by developers through other modules, not directly. Support for cloud-based environment setups is offered via the core module, which gives users
3 min read
Spring Cloud Vs Spring Boot Actuator
Spring CloudSpring Cloud's main purpose is to give tools and services for managing and making distributed systems, especially in a microservices architecture. The developer wants to speed up the process of building patterns in distributed systems, these tools can fulfill their requirement. They incl
5 min read
Spring Cloud Stream - Event Routing
Spring Cloud Stream Event Routing is the ability to route events to a specific event subscriber or a specific destination. Routes 'TO' and 'FROM' will be used here. Using the Spring Cloud Stream architecture, developers may create extremely scalable event-driven microservices that are integrated wit
5 min read
What is Spring Cloud?
There are many reasons to use Spring Framework for example if you want faster development, less configuration, auto-configuration, embedded server, production-ready application, and many more. But apart from that most importantly we have ready-made support for microservices and this ready-made suppo
2 min read
Spring Cloud Bus
In Spring Boot, Spring Cloud Bus is the lightweight event bus framework that can be provided by the Spring Cloud, and it can enable the communication and coordination between the microservices or the disturbed system by providing the mechanism for transmitting messages across the system. Spring Clou
10 min read
Spring Boot - Getting Started
Spring Boot is a part of the larger Spring Framework ecosystem which is known for its comprehensive programming and configuration model for the modern Java-based enterprise applications. Spring Boot has emerged as a go-to framework for creating REST APIs, microservices, and web applications with les
5 min read
Spring Cloud Stream - Demystified and Simplified
Spring framework was introduced for ease of Java application development. Later spring boot gained popularity for its easy-to-configure nature. To make the framework survive in the industry with demand for event-driver streaming architecture, Spring introduced a new framework named Spring Cloud Stre
4 min read