🚀 Optimised AWS SQS Integration in Spring Boot Java Microservices: Long-Polling and Short Polling 🚀

SAURABH SHARMA
2 min readOct 9, 2023

--

Explained AWS SQS and it’s configuration

Today, let’s dive deep into integrating AWS Simple Queue Service (SQS) with Spring Boot, enhancing the efficiency and scalability of your microservices architecture. SQS is a powerful, fully managed message queuing service that enables you to decouple and scale microservices, ensuring reliable communication between them.

Here’s an optimised and well-structured code sample demonstrating the integration with AWS SQS in Spring Boot Java microservices, along with Long-Polling and Short Polling implementations for efficient message retrieval.

1. Gradle Dependencies:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'com.amazonaws:aws-java-sdk-sqs'
implementation 'org.springframework.cloud:spring-cloud-starter-aws'
}

2. AWS Configuration:

@Configuration
public class AwsConfig {

@Value("${aws.accessKey}")
private String awsAccessKey;

@Value("${aws.secretKey}")
private String awsSecretKey;

@Value("${aws.region}")
private String awsRegion;

@Bean
public AmazonSQS amazonSQS() {
return AmazonSQSClient.builder()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
.withRegion(awsRegion)
.build();
}
}

3. SQS Producer:

@Service
public class SqsProducer {

@Autowired
private AmazonSQS amazonSQS;

@Value("${aws.sqs.queueUrl}")
private String queueUrl;

public void sendMessage(String message) {
amazonSQS.sendMessage(queueUrl, message);
}
}

4. Long-Polling Consumer:

@Service
public class LongPollingConsumer {

@Autowired
private AmazonSQS amazonSQS;

@Value("${aws.sqs.queueUrl}")
private String queueUrl;

public List<Message> receiveMessagesWithLongPolling() {
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl)
.withWaitTimeSeconds(20) // Long polling time (1-20 seconds)
.withMaxNumberOfMessages(10);
return amazonSQS.receiveMessage(receiveMessageRequest).getMessages();
}
}

5. Short-Polling Consumer:

@Service
public class ShortPollingConsumer {

@Autowired
private AmazonSQS amazonSQS;

@Value("${aws.sqs.queueUrl}")
private String queueUrl;

public List<Message> receiveMessagesWithShortPolling() {
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl)
.withMaxNumberOfMessages(10);
return amazonSQS.receiveMessage(receiveMessageRequest).getMessages();
}
}

In this code:

  • Gradle dependencies include necessary Spring Boot starters and AWS SQS SDK.
  • AWS Configuration sets up the AmazonSQS bean.
  • SQS Producer sends messages to the specified queue.
  • Long-Polling Consumer retrieves messages using long-polling, waiting up to 20 seconds for messages.
  • Short-polling Consumer retrieves messages without waiting.

By implementing Long-Polling, your microservices can efficiently retrieve messages with reduced latency, while Short-Polling provides a basic way to fetch messages instantly.

Feel free to adjust the waitTimeSeconds parameter for long-polling based on your use case's optimal latency and throughput requirements.

Happy coding! If you have any questions or need further clarification, please drop a comment below. Let’s continue making amazing Java applications on AWS!

#Java #SpringBoot #AWS #Microservices #SQS #Coding #DeveloperCommunity #JavaProgramming #AWSIntegration

--

--

SAURABH SHARMA
SAURABH SHARMA

Written by SAURABH SHARMA

Technology Enthusiast and Open Source Technology advocate

No responses yet