Working with Amazon Simple Queue Service message queues - AWS SDK for Java

You can now use the Amazon S3 Transfer Manager (Developer Preview) in the AWS SDK for Java 2.x for accelerated file transfers. Give it a try and let us know what you think! By the way, the AWS SDK for Java team is hiring software development engineers!

Working with Amazon Simple Queue Service message queues

A message queue is the logical container used for sending messages reliably in Amazon Simple Queue Service. 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 Simple Queue Service Developer Guide.

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

Create a queue

Use the SqsClient’s createQueue method, and provide a CreateQueueRequest object that describes the queue parameters.

Imports

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

Code

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

See the complete sample on GitHub.

List queues

To list the Amazon Simple Queue Service queues for your account, call the SqsClient’s listQueues 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.regions.Region; import software.amazon.awssdk.services.sqs.SqsClient; import software.amazon.awssdk.services.sqs.model.*; import java.util.List;

Code

String prefix = "que"; try { ListQueuesRequest listQueuesRequest = ListQueuesRequest.builder().queueNamePrefix(prefix).build(); ListQueuesResponse listQueuesResponse = sqsClient.listQueues(listQueuesRequest); for (String url : listQueuesResponse.queueUrls()) { System.out.println(url); } } catch (SqsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); }

See the complete sample on GitHub.

Get the URL for a queue

Call the SqsClient’s getQueueUrl method. with a GetQueueUrlRequest object.

Imports

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

Code

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

See the complete sample on GitHub.

Delete a queue

Provide the queue’s URL to the DeleteMessageRequest object. Then call the SqsClient’s deleteQueue method.

Imports

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

Code

public static void deleteSQSQueue(SqsClient sqsClient, String queueName) { try { GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder() .queueName(queueName) .build(); String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl(); DeleteQueueRequest deleteQueueRequest = DeleteQueueRequest.builder() .queueUrl(queueUrl) .build(); sqsClient.deleteQueue(deleteQueueRequest); } catch (SqsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

See the complete sample on GitHub.

More information