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.


  • 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.


  • 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 dependency into your pom.xml.


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.

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

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.
package com.otp;

import java.util.List;

public class SQS_Example {

    public static void main(String[] args) {
        SqsClient sqsClient = SqsClient.builder()
        String queue = "";
        readMessages(sqsClient, queue);

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


Hello world from Java!
Hello 1


Download Source From GIT:

Happy Learning 🙂