DEV Community

Cover image for AWS SNS (Amazon Simple Notification Service) & Spring Boot with Email as subscriber
Milan Karajovic
Milan Karajovic

Posted on • Edited on

2 1 1

AWS SNS (Amazon Simple Notification Service) & Spring Boot with Email as subscriber

Introduction

The concepts of "topic" and "subscribe" are often linked, especially in contexts like messaging systems, event-driven architectures, or content platforms.

  • Publisher: This is the source or entity that produces messages or events. The publisher doesn't need to know who will consume its messages.

  • Topic: This acts as a channel or intermediary that categorizes messages. Publishers post messages to specific topics, and subscribers listen to those topics. It's used in systems like message brokers (e.g., RabbitMQ, AWS SNS, Apache Kafka) to allow publishers to send messages without worrying about who will receive them.

  • Subscriber: These are the entities that consume the messages from the topics they're interested in. Subscribers can dynamically choose topics to receive only the information they need.

Amazon SNS (Simple Notification Service) topic

Amazon SNS provides message delivery from publishers to subscribers using the pub/sub pattern. Publishers send messages to an SNS topic, and subscribers receive those messages through their chosen endpoints.
Topic Types: SNS supports two types of topics:

  • Standard Topics: Allow high throughput and best-effort message ordering at least one message delivery
  • FIFO Topics: Ensure message order and exactly-once message delivery. Subscribers can be other applications, an Amazon SQS queue, AWS lambda function, or HTTPS endpoint… We can see that in the picture bellow:

AWS Amazon SNS - Amazon Simple Notification Service

Create Topic and Subscribe on the topic

Create User in IAM
On the AWS dashboard choose service IAM (Manage access to AWS resources)
Create user or use existed user

  • For created user add for Permission policies: AmazonSNSFullAccess
  • On the card Security credentials, you can see option Access Keyes (access key - go to the process where you will create user_accessKeys.csv. In this file, you can see Access key ID and Secret access key. These values you will use in the spring Boot application, in the file: AppListener.java(/spring-boot-aws-sns/src/main/java/rs/karajovic/milan/config/AppListener.java))

Create topic in Amazon SNS
On the AWS dashboard choose Amazon SNS (Simple Notification Service) -> Go to link Topics -> Create topic:

  • Standard
  • Give name for topic
  • Create topic

Subscribe on the topic
Go to link Topics -> Choose created topic -> Create subscription:

  • There you can see Topic ARN
  • Also, you can see different protocols: Amazon Kinesis Data Firehose, Amazon SQS, AWS Lambda, Email, Email-JSON, HTTP, HTTPS, Platform application endpoint, SMS. For our demo, we will choose: Email-JSON.

When you choose Topics, and click on your topic, you can see fields: Name and ARN. Content of these fields you will use in the project, in the file: application.properties (/spring-boot-aws-sns/src/main/resources/application.properties)

Tech Stack

  • Java 17
  • SNS Amazon-AWSSDK
  • JPA
  • Maven
  • Lombok
  • JUnit
  • IAM
  • SNS (Simple Notification Service) Create spring Boot application using Spring Initializr using specific dependencies which are shown in the pom.xml
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
  <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>sns</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Allow the application to connect to AWS

Create application listener in which is need to set up an AWS access key and secret key in the Java system properties, so the app is able to connect to AWS. We got these properties in the step of creating a context in IAM, which was explained in the step before.

package rs.karajovic.milan.config;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

/**
 * 
 * @author Milan Karajovic <milan.karajovic.rs@gmail.com>
 *
 */

public class AppListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {

        // accessKeyId & for AWS user
        System.setProperty("aws.accessKeyId", "fill with accessKeyId & for AWS user");
        // secretAccessKey for AWS user
        System.setProperty("aws.secretAccessKey", "fill with secretAccessKey for AWS user");

    }
}
Enter fullscreen mode Exit fullscreen mode

Application listener should be added to the application.

@SpringBootApplication
public class SpringBootAwsSns {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootAwsSns.class);
        app.addListeners(new AppListener());
        app.run(args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Set up SNS properties

In application.properties set values for aws-sns region and topicArn. We got these properties in the step of creating topic in Amazon SNS, which was explained in the step before.

#### AWS ### -
aws.sns.region=fill with region where you crated sns
aws.sns.topicArn=fill with created arn for test-top-arn

management.endpoints.web.exposure.include=*
Enter fullscreen mode Exit fullscreen mode

Configuration SNS Client

Now we need to configure the SNS Client that will publish messages to the topic. We created a class that reads the AWS SNS properties from the properties file. It is in the class /spring-boot-aws-sns/src/main/java/rs/karajovic/milan/config/AwsProperties.java.

package rs.karajovic.milan.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotNull;

/**
 * 
 * @author Milan Karajovic <milan.karajovic.rs@gmail.com>
 *
 */

@Configuration
@ConfigurationProperties(prefix = "aws.sns")
public class AwsProperties {

    @Getter @Setter
    @NotNull
    private String region;

    @Getter @Setter
    @NotNull
    private String topicArn;


}
Enter fullscreen mode Exit fullscreen mode

Then, configure an SNS Client with the appropriate region.

package rs.karajovic.milan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;

/**
 * 
 * @author Milan Karajovic <milan.karajovic.rs@gmail.com>
 *
 */

@Configuration
public class SnsConfig {

    @Autowired
    private AwsProperties awsProperties;

    @Bean
    public SnsClient snsClient() {
        return SnsClient.builder()
                .region(Region.of(awsProperties.getRegion()))
                .build();
    }
}
Enter fullscreen mode Exit fullscreen mode

Build a message with a message attributes

In our scenario, subscribers to the topic wish to receive notifications about changes in temperature, whether it increases or decreases. Create the Message class to specify the attributes that are relevant to these subscribers.

package rs.karajovic.milan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;

/**
 * 
 * @author Milan Karajovic <milan.karajovic.rs@gmail.com>
 *
 */

@Configuration
public class SnsConfig {

    @Autowired
    private AwsProperties awsProperties;

    @Bean
    public SnsClient snsClient() {
        return SnsClient.builder()
                .region(Region.of(awsProperties.getRegion()))
                .build();
    }
}
Enter fullscreen mode Exit fullscreen mode
package rs.karajovic.milan.model;

/**
 * 
 * @author Milan Karajovic <milan.karajovic.rs@gmail.com>
 *
 */

public enum EventType {
    DROP, INCREASE
}
Enter fullscreen mode Exit fullscreen mode

Next, we create a builder that constructs an Amazon PublishRequest object from a given message. This message leverages Amazon SNS message attributes, which allow for greater flexibility. One key advantage of using these attributes is that they enable subscribers to filter their subscriptions based on specific attributes, ensuring they only receive the notifications that are relevant to their interests.

public class RequestBuilder {
  public static final String COUNTRY = "Country";
  public static final String REGION = "Region";
  public static final String EVENT_TYPE = "EventType";
  public static final String CITY = "City";
  public static final String NEW_TEMPERATURE = "NewTemperature";
  public static final String DEFAULT_MESSAGE_BODY = "Please see attributes.";


  public static PublishRequest build(String topicArn, Message message) {
      Map<String, MessageAttributeValue> attributes = new HashMap<>();
      attributes.put(COUNTRY, buildAttribute(message.getCountry(), "String"));
      attributes.put(REGION, buildAttribute(message.getRegion(), "String"));
      attributes.put(EVENT_TYPE, buildAttribute(message.getEventType().toString(), "String"));
      attributes.put(CITY, buildAttribute(message.getCity(), "String"));
      attributes.put(NEW_TEMPERATURE, buildAttribute(message.getNewTemperature().toString(), "Number"));

      PublishRequest request = PublishRequest.builder()
              .topicArn(topicArn)
              .message(DEFAULT_MESSAGE_BODY)
              .messageAttributes(attributes)
              .build();

      return request;
  }

  private static MessageAttributeValue buildAttribute(String value, String dataType) {
      return MessageAttributeValue.builder()
              .dataType(dataType)
              .stringValue(value)
              .build();
  }
}
Enter fullscreen mode Exit fullscreen mode

Publish a message

Create an endpoint that publishes a message to the specified topic. Upon execution, the endpoint provides the status code, the response message, and the unique message ID generated by Amazon SNS for the published message. In the event of an exception, the endpoint will return the corresponding status code along with the error message as received from Amazon SNS. The next step involves transforming the message into an Amazon PublishRequest and proceeding to publish it.

@RestController
public class MessageController {

  private final MessagePublisher messagePublisher;

  public MessageController(MessagePublisher messagePublisher) {
      this.messagePublisher = messagePublisher;
  }

  @PostMapping(value = "/publish")
  @ResponseStatus(HttpStatus.CREATED)
  public SnsResponse publishMessage(@RequestBody Message message) {
      return messagePublisher.publish(message);
  }

  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ExceptionHandler(RuntimeException.class)
  private String handleException(RuntimeException e) {
      return e.getMessage();
  }
}
Enter fullscreen mode Exit fullscreen mode

Also, we will create model SNS response which will be used to receive the response from the publish endpoint:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
public class SnsResponse {
    private Integer statusCode;
    private String message;
    private String publishedMessageId;

    @Override
    public String toString() {
        return "SnsResponse{statusCode=" + statusCode + 
               ", message='" + message + ''' + 
               ", publishedMessageId='" + publishedMessageId + "'}";
    }

}
Enter fullscreen mode Exit fullscreen mode

The code handles various exceptions that Amazon SNS might throw, with SdkException handling as a general catch-all for other potential exceptions.

Send a message in SNS using local environment development tool

Run created Spring Boot app and send a message to the publish endpoint using Postman like it is shown in the picture:

Publish message to aws SNS

The response body indicates that Amazon SNS returned a 200 status code along with the message “OK” and a message ID, verifying that the message was successfully published.

Response after send message to AWS SNS

Check the email address that was subscribed to the topic earlier; you should find a notification from AWS there.

Receive message which sent to the SNS AWS

Docker & docker-compose

This spring boot application we can also dockerize. Tor that we are using the Dockerfile and docker-compose.yml files whic are in the root of the project.
Dockerfile:

Create Dockerfile for the AWS SNS Spring Boot

When image is created, we start the container using docker-compose.yml file:
docker-compose.yml:

Dockercompose for AWS SNS Spring Boot

When the container is started, we can send a message to the publish endpoint using Postman like it is shown in the picture:

Send message to the AWS SNS Spring Boot

Contact and support

author: Milan Karajovic
Portfolio: https://milan.karajovic.rs/
GitHub: https://github.com/Milan-Karajovic/AWS-SNS-Spring-Boot
Follow me on LinkedIn: https://www.linkedin.com/in/milan-karajovic-3a1243359/

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Q Developer CLI agent provides a lightning-fast coding experience that can read and write files locally, call AWS APIs, run bash commands, or create code—all while adapting to your feedback in real-time.

Start coding for free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay