Menu
AWS SDK for Java
Developer Guide

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

Create a Queue

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

Imports

Copy
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

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

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

See the complete sample.

Listing Queues

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

Imports

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

Code

Copy
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

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

Code

Copy
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 sample.

Get the URL for a Queue

Call the AmazonSQS client's getQueueUrl method.

Imports

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

Code

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

See the complete sample.

Delete a Queue

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

Imports

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

Code

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

See the complete sample.

More Info