Working with Amazon SQS Message Queues - Amazon SDK for Java 1.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

We announced the upcoming end-of-support for Amazon SDK for Java (v1). We recommend that you migrate to Amazon SDK for Java v2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Working with Amazon SQS Message Queues

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 Amazon SDK for Java.

Create a Queue

Use the AmazonSQS client’s createQueue method, providing a CreateQueueRequest object that describes the queue parameters.

Imports

import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.AmazonSQSException; import com.amazonaws.services.sqs.model.CreateQueueRequest;

Code

AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); CreateQueueRequest create_request = new CreateQueueRequest(QUEUE_NAME) .addAttributesEntry("DelaySeconds", "60") .addAttributesEntry("MessageRetentionPeriod", "86400"); try { sqs.createQueue(create_request); } catch (AmazonSQSException e) { if (!e.getErrorCode().equals("QueueAlreadyExists")) { throw e; } }

You can use the simplified form of createQueue, which needs only a queue name, to create a standard queue.

sqs.createQueue("MyQueue" + new Date().getTime());

See the complete example on GitHub.

Listing Queues

To list the Amazon SQS queues for your account, call the AmazonSQS client’s listQueues method.

Imports

import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.ListQueuesResult;

Code

AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); ListQueuesResult lq_result = sqs.listQueues(); System.out.println("Your SQS Queue URLs:"); for (String url : lq_result.getQueueUrls()) { System.out.println(url); }

Using the listQueues overload without any parameters returns all queues. You can filter the returned results by passing it a ListQueuesRequest object.

Imports

import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder; import com.amazonaws.services.sqs.model.ListQueuesRequest;

Code

AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); String name_prefix = "Queue"; lq_result = sqs.listQueues(new ListQueuesRequest(name_prefix)); System.out.println("Queue URLs with prefix: " + name_prefix); for (String url : lq_result.getQueueUrls()) { System.out.println(url); }

See the complete example on GitHub.

Get the URL for a Queue

Call the AmazonSQS client’s getQueueUrl method.

Imports

import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder;

Code

AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); String queue_url = sqs.getQueueUrl(QUEUE_NAME).getQueueUrl();

See the complete example on GitHub.

Delete a Queue

Provide the queue’s URL to the AmazonSQS client’s deleteQueue method.

Imports

import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClientBuilder;

Code

AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); sqs.deleteQueue(queue_url);

See the complete example on GitHub.

More Info