Java AWS - How to read messages from SQS - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to see how to read messages from AWS SQS using Java SDK.
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.
Add software.amazon.awssdk dependency into your 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.
 % 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.
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:
Hello world from Java!
Hello 1
AWS SQS developer guide
Send messages to SQS
Happy Learning 🙂

Java AWS – How to Send Messages to SQS Queues
Java AWS – How to Create SQS Standard and FIFO Queues
How to install AWS CLI on Windows 10
Python – AWS SAM Lambda Example
Javascript – How to listObjects from AWS S3
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
How to connect AWS EC2 Instance using PuTTY
[Fixed] – Malformed Lambda proxy response –  status 502
Python – How to read environment variables ?
Spring Boot RabbitMQ Consumer Messages Example
How add files to S3 Bucket using Shell Script
Python raw_input read input from keyboard
Python How to read input from keyboard
Spring Boot Kafka Consume JSON Messages Example

Java AWS – How to Send Messages to SQS Queues
Java AWS – How to Create SQS Standard and FIFO Queues
How to install AWS CLI on Windows 10
Python – AWS SAM Lambda Example
Javascript – How to listObjects from AWS S3
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
How to connect AWS EC2 Instance using PuTTY
[Fixed] – Malformed Lambda proxy response –  status 502
Python – How to read environment variables ?
Spring Boot RabbitMQ Consumer Messages Example
How add files to S3 Bucket using Shell Script
Python raw_input read input from keyboard
Python How to read input from keyboard
Spring Boot Kafka Consume JSON Messages Example
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows