Open In App

Spring Cloud AWS – Messaging Support

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads