Amazon SQS with Spring Boot

Chiwa Kantawong (Pea)
3 min readAug 11, 2021

--

Amazon SQS provides queues for high-throughput, system-to-system messaging. You can use queues to decouple heavyweight processes and to buffer and batch work. Amazon SQS stores messages until microservices and serverless applications process them. ตรงตัวไม่ต้องแปลอะไรเยอะเนอะ ฮ่า

มาสร้าง Queue กันครับ

ตั้งชื่อว่า spring-boot-queue

สร้าง Spring Boot Application กันเลยครับ

แก้ pom เป็นแบบนี้นะครับ

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zengcode</groupId>
<artifactId>spring-boot-sqs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-sqs</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>

เพิ่ม application.yml

cloud:
aws:
region:
static: ap-southeast-1
auto: false
credentials:
access-key: xx
secret-key: xx
end-point:
uri: https://sqs.ap-southeast-1.amazonaws.com/083572612527/spring-boot-queue
stack:
auto: false

สร้าง configuration class กัน

package com.example.springboot.sqs.controller;

import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.SubscribeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SQSController {

private static String TOPIC_ARN = "arn:aws:sns:ap-southeast-1:083572612527:sns-spring-boot-test-topic";

@Autowired
private AmazonSNSClient amazonSNSClient;

@GetMapping("/subscribe/{email}")
public String subscribe(@PathVariable("email") String email) {
SubscribeRequest subscribeRequest = new SubscribeRequest(TOPIC_ARN, "email", email);
amazonSNSClient.subscribe(subscribeRequest);
return "Please check your email";
}

@GetMapping("/publish/{message}")
public String publish(@PathVariable("message") String message) {
PublishRequest publishRequest = new PublishRequest(TOPIC_ARN, "Test from Spring boot", message);
amazonSNSClient.publish(publishRequest);
return "Plush done!!";
}


}

ต่อไปสร้าง Rest Controller กันครับ

package com.example.springboot.sqs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SQSController {

@Value("${cloud.aws.end-point.uri}")
private String endPoint;

@Autowired
private QueueMessagingTemplate queueMessagingTemplate;


@GetMapping("/publish/{message}")
public String publish(@PathVariable("message") String message) {
queueMessagingTemplate.send(endPoint, MessageBuilder.withPayload(message).build());
return "Plush done!!";
}


}

มารัน กัน

สำเร็จครับ

มา pooling message ดูกัน

ต่อไปเราจะมาสร้าง Consumer เพื่อที่จะ consumed message จาก SQS กันครับ

package com.example.springboot.sqs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SQSController {

@Value("${cloud.aws.end-point.uri}")
private String endPoint;

@Autowired
private QueueMessagingTemplate queueMessagingTemplate;


@GetMapping("/publish/{message}")
public String publish(@PathVariable("message") String message) {
queueMessagingTemplate.send(endPoint, MessageBuilder.withPayload(message).build());
return "Plush done!!";
}

@SqsListener("spring-boot-queue")
public void consumedMessageFromQueue(String message) {
System.out.println("Message form queue : " + message);
}

}

มาทดลอง

http://localhost:8080/publish/Hello%20From%20Zengcode

ก็จะได้

ง่ายดายเนอะ

--

--