AWS SDK for Java
Developer Guide

Working with Amazon SQS Message Queues

Important

This is a preview release and is not recommended for production environments.

A message queue is the logical container used for sending messages reliably in Amazon SQS. There are two types of queues: standard and first-in, first-out (FIFO). To learn more about queues and the differences between these types, see the Amazon SQS Developer Guide.

This topic describes how to create, list, delete, and get the URL of an Amazon SQS queue by using the AWS SDK for Java.

Create a Queue

Use the SqsClientcreateQueue method, and provide a CreateQueueRequest object that describes the queue parameters.

Imports

import software.amazon.awssdk.services.sqs.SqsClient; import software.amazon.awssdk.services.sqs.model.CreateQueueRequest;

Code

CreateQueueRequest createQueueRequest = CreateQueueRequest.builder().queueName(queueName).build(); sqsClient.createQueue(createQueueRequest);

See the complete sample on GitHub.

List Queues

To list the Amazon SQS queues for your account, call the SqsClientlistQueues method with a ListQueuesRequest object.

Using the listQueues overload without any parameters returns all queues, up to 1,000 queues. You can supply a queue name prefix to the ListQueuesRequest object to limit the results to queues that match that prefix.

Imports

import software.amazon.awssdk.services.sqs.model.ListQueuesRequest; import software.amazon.awssdk.services.sqs.model.ListQueuesResponse;

Code

String prefix = "que"; ListQueuesRequest listQueuesRequest = ListQueuesRequest.builder().queueNamePrefix(prefix).build(); ListQueuesResponse listQueuesResponse = sqsClient.listQueues(listQueuesRequest); for (String url : listQueuesResponse.queueUrls()) { System.out.println(url); }

See the complete sample on GitHub.

Get the URL for a Queue

Call the SqsClientgetQueueUrl method. with a GetQueueUrlRequest object.

Imports

import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest; import software.amazon.awssdk.services.sqs.model.GetQueueUrlResponse;

Code

GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); System.out.println(queueUrl);

See the complete sample on GitHub.

Delete a Queue

Provide the queue's URL to the DeleteMessageRequest object. Then call the SqsClientdeleteQueue method.

Imports

import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest; import software.amazon.awssdk.services.sqs.model.DeleteQueueRequest;

Code

DeleteQueueRequest deleteQueueRequest = DeleteQueueRequest.builder().queueUrl(queueUrl).build(); sqsClient.deleteQueue(deleteQueueRequest);

See the complete sample on GitHub.

More Info