In this tutorial, we are going to see how to read messages from AWS SQS using Java SDK.

Java read messages from SQS:

You may be wonder that what is the use of this article while we have a provision to add subscriptions or triggers to the SQS directly that can automatically trigger SNS or lambda accordingly whenever a message sent to the SQS.  Generally, we call this kind of design a push service.

The intention of this approach is for pulling the messages from the SQS whenever we need them, which we call on-demand pulling.

Pre-Requisites:

  • Make sure you have an AWS account and valid credentials to access the resources.
  • Better you install the AWS CLI on your machine for troubleshooting.
  • Configure AWS credentials on your local machine.

Versions:

  • Java 14
  • AWS SDK 2.16.29

As part of this example, we are going to send messages to the SQS queue, messages can be sent to the SQS queue in two ways; single message and sending a bulk of messages at a time.

Java read messages from SQS Example:

Add software.amazon.awssdk dependency into your pom.xml.

pom.xml
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sqs</artifactId>
</dependency>

The Amazon AWS SDK for Java is used to interact with AWS resources from a Java application.

Run the maven clean install command to download the dependencies.

Terminal
 % mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< AWS_EXample:java-aws-examples >--------------------
[INFO] Building java-aws-examples 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
...
..

I am suspecting you have already messages in the SQS because it doesn’t make any sense to read the messages without having them. If you are interested in sending messages to SQS, I have written an article on it please go through it.

I have a FIFO queue and 2 messages in it. let’s try to read them.

SQS_Example.java
package com.otp;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;
import java.util.List;

public class SQS_Example {

    public static void main(String[] args) {
        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();
        String queue = "https://sqs.us-west-2.amazonaws.com/12345/My-FIFO-Queue.fifo";
        readMessages(sqsClient, queue);
        sqsClient.close();
    }

    public static void readMessages(SqsClient sqsClient, String queueUrl) {
        try {
            ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
                    .queueUrl(queueUrl)
                    .maxNumberOfMessages(5)
                    .build();
            List<Message> messages = sqsClient.receiveMessage(receiveMessageRequest).messages();
            messages.forEach(message -> System.out.println(message.body()));
        } catch (SqsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

Output:

Terminal
Hello world from Java!
Hello 1

References:

Download Source From GIT:

Happy Learning 🙂