In this tutorial, we will see how to create SQS Standard and FIFO Queues using AWS Java SDK.

AWS SQS:

AWS SQS is actually the first-ever AWS service publicly available, so it’s one of the oldest services they have.

SQS basically a message queue service, so it gives you access to a message queue, which can be used to store messages while an application or a computer process them.

SQS Queue:

A queue is a temporary repository for messages which are waiting for processing.

Types of Queues:

There are 2 different types of SQS Queue available, the main difference between the two is how they handle the ordering messages.

  • Standard Queue
  • FIFO Queue

Standard Queue:

  • Standard queues, which are the default, and provide best-effort ordering.
  • In standard queues, you get a nearly unlimited number of transactions per second.
  • Standard queues are guarantee that a message will be delivered at least once.
  • As Standard queues provide best-effort ordering, which ensures that the messages are generally delivered in the same order that they were sent, but occasionally because of the highly distributed architecture, which allows such high throughput, more than one copy of a message may be delivered and it may be out of order as well.

FIFO Queue:

  • FIFO queue, the ordering of messages is strictly preserved.
  • FIFO stands for first-in-first-out delivery.
  • So the order in which messages are sent and received is strictly preserved and you get exactly once for processing.
  • FIFO queue message is delivered once and it remains available until a consumer process and deletes it after processing.
  • Unlike Standard queues, there is a limit of 300 transactions per second with FIFO queues apart from a FIFO queue has all the same capabilities as a Standard queue.
  • FIFO queues are well suited, where the order of the transactions is very important and eliminate the duplicated transactions.
  • FIFO queues are really good for financial applications or financial transactions. Anything where you need the jobs to be processed in a specific order.

Java AWS SQS Create Queue:

Now let’s create a Java application that sends some messages to the SQS.

Add the bellow amazon-aws-sdk dependency in 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] 
...
..

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.

Create SQS Standard Queue:

In the following code, we are creating a Standard SQS Queue.

The standard queue names only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length
SQS_Example
package com.otp;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;
import software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse;
import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest;
import software.amazon.awssdk.services.sqs.model.SqsException;

import java.util.List;

public class SQS_Example {

    public static void main(String[] args) {
        String queueName = "My-Sample-Queue";
        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();
        String queueUrl= createQueue(sqsClient, queueName);
        System.out.println("Queue URL : "+queueUrl);
        sqsClient.close();
    }

    public static String createQueue(SqsClient sqsClient,String queueName) {
        try {
            CreateQueueRequest cqr = CreateQueueRequest.builder()
                    .queueName(queueName)
                    .build();

            sqsClient.createQueue(cqr);

            GetQueueUrlResponse getQueueUrlResponse =
                    sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
            return getQueueUrlResponse.queueUrl();
        } catch (SqsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return null;
    }
}

Output:

Queue URL : https://sqs.us-west-2.amazonaws.com/12345678/My-Sample-Queue
Java AWS - SQS Send messages to Queue Standard

You can see the first My-Sample-Queue has been created in the AWS console. You can also observe that it is a Standard queue.

Create SQS FIFO Queue:

As we discussed earlier in this tutorial, the FIFO queues are first-in-first-out delivery. So the order in which messages are sent and received is strictly preserved and you get exactly once for processing.

Creatin the FIFO queues using aws java sdkis almost similar to the standard queue, but this time we need to pass the queue attributes to the CreateQueueRequest saying

QueueAttributeName set totrue.

The name of a FIFO queue can only include alphanumeric characters, hyphens, or underscores, must end with .fifo suffix and be 1 to 80 in length.
SQS_Example
package com.otp;
import java.util.Map;
import java.util.HashMap;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;

public class SQS_Example {

    public static void main(String[] args) {
        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();

        String fifoQueueUrl= createFIFOQueue(sqsClient, "My-FIFO-Queue.fifo");
        System.out.println("fifoQueueUrl : "+fifoQueueUrl);
        sqsClient.close();
    }

    public static String createFIFOQueue(SqsClient sqsClient,String queueName) {
        Map<QueueAttributeName, String> queueAttributes = new HashMap<>();
        queueAttributes.put(QueueAttributeName.FIFO_QUEUE, "true");
        try {
            CreateQueueRequest cqr = CreateQueueRequest.builder()
                    .queueName(queueName)
                    .attributes(queueAttributes)
                    .build();

            sqsClient.createQueue(cqr);

            GetQueueUrlResponse getQueueUrlResponse =
                    sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
            return getQueueUrlResponse.queueUrl();
        } catch (SqsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
}

Output:

fifoQueueUrl : https://sqs.us-west-2.amazonaws.com/123456/My-FIFO-Queue.fifo
Java AWS - SQS Create FIFO Queue

List Queues:

Reading all queues.
SQS_Example
package com.otp;

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

public class SQS_Example {

    public static void main(String[] args) {
        SqsClient sqsClient = SqsClient.builder()
                .region(Region.US_WEST_2)
                .build();
        listAllQueues(sqsClient);
        sqsClient.close();
    }
    
    public static void listAllQueues(SqsClient sqsClient) {

        try {
            ListQueuesRequest listQueuesRequest = ListQueuesRequest.builder().build();
            ListQueuesResponse listQueuesResponse = sqsClient.listQueues(listQueuesRequest);

            listQueuesResponse.queueUrls().forEach(System.out::println);

        } catch (SqsException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

Output:

https://sqs.us-west-2.amazonaws.com/123456/My-FIFO-Queue.fifo
https://sqs.us-west-2.amazonaws.com/123456/My-Sample-Queue

References:

Download Source From GIT:

Happy Learning 🙂