Work 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.
The sqsClient
variable that is used in the following examples can be created
from the following snippet.
SqsClient sqsClient = SqsClient.create();
When you create an SqsClient
by using the static create()
method,
the SDK configures the Region by using the default region provider chain and the credentials by using the default credentials provider chain.
Create a queue
Use the SqsClient’s
createQueue
method, and provide a CreateQueueRequest
object that describes the queue parameters as
shown in the following code snippet.
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
List queues
To list the Amazon Simple Queue Service queues for your account, call the
SqsClient’s
listQueues
method with a ListQueuesRequest
object.
When you use the form of the listQueues
You can supply a queue name prefix to the ListQueuesRequest
object to limit the results to queues that match
that prefix as shown in the following code.
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
Get the URL for a queue
The following code shows how to get the URL for a queue by calling 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;
See the complete sample
Delete a queue
Provide the queue’s URL to the DeleteQueueRequest
object. Then call the SqsClient’s
deleteQueue
method to delete a queue as shown in the following code.
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
More information
-
CreateQueue in the Amazon Simple Queue Service API Reference
-
GetQueueUrl in the Amazon Simple Queue Service API Reference
-
ListQueues in the Amazon Simple Queue Service API Reference
-
DeleteQueue in the Amazon Simple Queue Service API Reference