Spring Boot With AWS SQS

Spring Cloud messaging support provides a convenient way to interact with AWS SQS service, With the help of spring cloud messaging support we can use all well-known Spring Boot features. It also offers multiple useful features compare to SDK provided by AWS.

The code for this post is available on Github here

Create a standard AWS SQS queue

Navigate to aws consol -> Simple queue service -> create queue. Then select standard queue and provide name to queue.Click on create queue.

Create IAM Role and IAM Group, Which will have access to our queue.

Using Spring cloud messaging

The Spring Cloud AWS messaging module comes as a standalone module and can be imported with the following dependency
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
</dependency>

Providing AWS credential and SDK configurations

In order to make calls to the AWS Services the credentials must be configured for the the Amazon SDK. In order to access SQS service we can configure access key and secret key using yaml or properties files
1
2
3
4
5
6
7
8
9
10
cloud:
aws:
region:
static: us-east-1
auto: false
credentials:
access-key: XXXX
secret-key: XXXX
end-point:
uri: https://sqs.us-east-1.amazonaws.com/549485575026/spring-boot-poc

Sending message to SQS.

In order to send messages to SQS queue, Spring boot provides QueueMessagingTemplate which uses AmazonSQSAsync

Configuration for QueueMessagingTemplate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
public class SQSConfig {
@Value("${cloud.aws.region.static}")
private String region;
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSQSAsync());
}
@Bean
@Primary
public AmazonSQSAsync amazonSQSAsync() {
return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.US_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
}
}

QueueMessagingTemplate Provides a convenient method convertAndSend which can be used to send domain objects as message. QueueMessagingTemplate delegate the conversion process to an instance of the MessageConverter interface. This interface defines a simple contract to convert between Java objects and SQS messages.

Message Publisher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
@Slf4j
public class Publisher {
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
@Value("${cloud.aws.end-point.uri}")
private String endpoint;
@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTask() {
log.info("Sending Message to SQS ");
//queueMessagingTemplate.send(endpoint, MessageBuilder.withPayload("Niraj").build());
queueMessagingTemplate.convertAndSend(endpoint, new Pojo("SomeRandomValue"));
}
}

Consuming Messages from SQS

Spring boot provides a convenient Annotation @SqsListener. In below example a queue listener container is started that polls the spring-boot-poc queue. The incoming messages is converted to the type of method argument, in this case Pojo.

As the deletionPolicy is provided as ON_SUCCESS it means, Message will be Deleted from Queue only when successfully executed by listener method (no exception thrown). We can set the Global deletion policy for all the queues which are consumed by
SqsListener by using property cloud.aws.sqs.handler.default-deletion-policy=ON_SUCCESS

Message Consumer
1
2
3
4
5
6
7
8
@Component
@Slf4j
public class Consumer {
@SqsListener(value = "spring-boot-poc",deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void processMessage(Pojo message) {
log.info("Message from SQS {}", message);
}
}

Note:
On application startup, you might see exception related to Metadata or RegistryFactoryBean. You need to exclude some auto configuration. You can find more details
https://stackoverflow.com/a/67409356/320087

exclude autoconfigure
1
2
3
4
5
6
7
 
spring:
autoconfigure:
exclude:
- org.springframework.cloud.aws.autoconfigure.context.ContextInstanceDataAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextStackAutoConfiguration
- org.springframework.cloud.aws.autoconfigure.context.ContextRegionProviderAutoConfiguration

The code for this post is available on Github here

Share Comments