0
# Queue Operations
1
2
Queue operations provide comprehensive management capabilities for Amazon SQS queues, including creation, deletion, configuration, listing, and maintenance. These operations handle both standard and FIFO queues with full support for all queue attributes and configuration options.
3
4
## Create Queue
5
6
### Create Standard Queue
7
8
Create a new SQS queue with optional configuration attributes.
9
10
```java { .api }
11
CreateQueueResult createQueue(CreateQueueRequest request);
12
13
// Convenience method
14
CreateQueueResult createQueue(String queueName);
15
16
class CreateQueueRequest extends AmazonWebServiceRequest {
17
CreateQueueRequest();
18
CreateQueueRequest(String queueName);
19
20
String getQueueName();
21
CreateQueueRequest withQueueName(String queueName);
22
23
Map<String, String> getAttributes();
24
CreateQueueRequest withAttributes(Map<String, String> attributes);
25
CreateQueueRequest addAttributesEntry(String key, String value);
26
27
Map<String, String> getTags();
28
CreateQueueRequest withTags(Map<String, String> tags);
29
CreateQueueRequest addTagsEntry(String key, String value);
30
}
31
32
class CreateQueueResult {
33
String getQueueUrl();
34
}
35
```
36
37
**Usage Example:**
38
39
```java
40
// Simple queue creation
41
CreateQueueRequest request = new CreateQueueRequest("MyQueue");
42
CreateQueueResult result = client.createQueue(request);
43
String queueUrl = result.getQueueUrl();
44
System.out.println("Created queue: " + queueUrl);
45
46
// Queue with attributes
47
Map<String, String> attributes = new HashMap<>();
48
attributes.put(QueueAttributeName.VisibilityTimeout.toString(), "60");
49
attributes.put(QueueAttributeName.MessageRetentionPeriod.toString(), "1209600"); // 14 days
50
attributes.put(QueueAttributeName.DelaySeconds.toString(), "0");
51
attributes.put(QueueAttributeName.ReceiveMessageWaitTimeSeconds.toString(), "20"); // Long polling
52
53
CreateQueueRequest advancedRequest = new CreateQueueRequest()
54
.withQueueName("MyAdvancedQueue")
55
.withAttributes(attributes)
56
.addTagsEntry("Environment", "Production")
57
.addTagsEntry("Team", "Backend");
58
59
CreateQueueResult advancedResult = client.createQueue(advancedRequest);
60
```
61
62
### Create FIFO Queue
63
64
Create a FIFO (First-In-First-Out) queue for guaranteed message ordering and exactly-once processing.
65
66
```java
67
// FIFO queue creation
68
Map<String, String> fifoAttributes = new HashMap<>();
69
fifoAttributes.put(QueueAttributeName.FifoQueue.toString(), "true");
70
fifoAttributes.put(QueueAttributeName.ContentBasedDeduplication.toString(), "true");
71
72
CreateQueueRequest fifoRequest = new CreateQueueRequest()
73
.withQueueName("MyQueue.fifo") // Must end with .fifo
74
.withAttributes(fifoAttributes);
75
76
CreateQueueResult fifoResult = client.createQueue(fifoRequest);
77
```
78
79
## Delete Queue
80
81
### Delete Queue
82
83
Remove a queue and all its messages permanently.
84
85
```java { .api }
86
DeleteQueueResult deleteQueue(DeleteQueueRequest request);
87
88
// Convenience method
89
DeleteQueueResult deleteQueue(String queueUrl);
90
91
class DeleteQueueRequest extends AmazonWebServiceRequest {
92
DeleteQueueRequest();
93
DeleteQueueRequest(String queueUrl);
94
95
String getQueueUrl();
96
DeleteQueueRequest withQueueUrl(String queueUrl);
97
}
98
99
class DeleteQueueResult {
100
// Empty result class - success indicated by no exception
101
}
102
```
103
104
**Usage Example:**
105
106
```java
107
// Delete queue
108
try {
109
client.deleteQueue(new DeleteQueueRequest(queueUrl));
110
System.out.println("Queue deleted successfully");
111
} catch (QueueDoesNotExistException e) {
112
System.err.println("Queue does not exist: " + e.getMessage());
113
}
114
115
// Note: After deletion, queue name cannot be reused for 60 seconds
116
```
117
118
## Queue URL Management
119
120
### Get Queue URL
121
122
Retrieve the URL of an existing queue by name.
123
124
```java { .api }
125
GetQueueUrlResult getQueueUrl(GetQueueUrlRequest request);
126
127
// Convenience method
128
GetQueueUrlResult getQueueUrl(String queueName);
129
130
class GetQueueUrlRequest extends AmazonWebServiceRequest {
131
GetQueueUrlRequest();
132
GetQueueUrlRequest(String queueName);
133
134
String getQueueName();
135
GetQueueUrlRequest withQueueName(String queueName);
136
137
String getQueueOwnerAWSAccountId();
138
GetQueueUrlRequest withQueueOwnerAWSAccountId(String queueOwnerAWSAccountId);
139
}
140
141
class GetQueueUrlResult {
142
String getQueueUrl();
143
}
144
```
145
146
**Usage Example:**
147
148
```java
149
// Get queue URL by name
150
GetQueueUrlRequest request = new GetQueueUrlRequest("MyQueue");
151
GetQueueUrlResult result = client.getQueueUrl(request);
152
String queueUrl = result.getQueueUrl();
153
154
// Cross-account queue access
155
GetQueueUrlRequest crossAccountRequest = new GetQueueUrlRequest()
156
.withQueueName("SharedQueue")
157
.withQueueOwnerAWSAccountId("123456789012");
158
159
GetQueueUrlResult crossAccountResult = client.getQueueUrl(crossAccountRequest);
160
```
161
162
## List Queues
163
164
### List All Queues
165
166
Retrieve URLs of queues with optional name prefix filtering.
167
168
```java { .api }
169
ListQueuesResult listQueues(ListQueuesRequest request);
170
171
// Convenience methods
172
ListQueuesResult listQueues();
173
ListQueuesResult listQueues(String queueNamePrefix);
174
175
class ListQueuesRequest extends AmazonWebServiceRequest {
176
ListQueuesRequest();
177
ListQueuesRequest(String queueNamePrefix);
178
179
String getQueueNamePrefix();
180
ListQueuesRequest withQueueNamePrefix(String queueNamePrefix);
181
182
Integer getMaxResults();
183
ListQueuesRequest withMaxResults(Integer maxResults);
184
185
String getNextToken();
186
ListQueuesRequest withNextToken(String nextToken);
187
}
188
189
class ListQueuesResult {
190
List<String> getQueueUrls();
191
String getNextToken();
192
}
193
```
194
195
**Usage Example:**
196
197
```java
198
// List all queues
199
ListQueuesResult allQueues = client.listQueues();
200
for (String queueUrl : allQueues.getQueueUrls()) {
201
System.out.println("Queue: " + queueUrl);
202
}
203
204
// List queues with prefix
205
ListQueuesRequest prefixRequest = new ListQueuesRequest("prod-");
206
ListQueuesResult prodQueues = client.listQueues(prefixRequest);
207
208
// Paginated listing
209
ListQueuesRequest paginatedRequest = new ListQueuesRequest()
210
.withMaxResults(100);
211
212
ListQueuesResult page1 = client.listQueues(paginatedRequest);
213
if (page1.getNextToken() != null) {
214
ListQueuesResult page2 = client.listQueues(paginatedRequest
215
.withNextToken(page1.getNextToken()));
216
}
217
```
218
219
## Queue Attributes
220
221
### Get Queue Attributes
222
223
Retrieve configuration and status information about a queue.
224
225
```java { .api }
226
GetQueueAttributesResult getQueueAttributes(GetQueueAttributesRequest request);
227
228
// Convenience method
229
GetQueueAttributesResult getQueueAttributes(String queueUrl, List<String> attributeNames);
230
231
class GetQueueAttributesRequest extends AmazonWebServiceRequest {
232
GetQueueAttributesRequest();
233
GetQueueAttributesRequest(String queueUrl);
234
GetQueueAttributesRequest(String queueUrl, List<String> attributeNames);
235
236
String getQueueUrl();
237
GetQueueAttributesRequest withQueueUrl(String queueUrl);
238
239
List<String> getAttributeNames();
240
GetQueueAttributesRequest withAttributeNames(List<String> attributeNames);
241
GetQueueAttributesRequest withAttributeNames(String... attributeNames);
242
GetQueueAttributesRequest withAttributeNames(QueueAttributeName... attributeNames);
243
}
244
245
class GetQueueAttributesResult {
246
Map<String, String> getAttributes();
247
}
248
```
249
250
**Usage Example:**
251
252
```java
253
// Get all attributes
254
GetQueueAttributesRequest request = new GetQueueAttributesRequest(queueUrl)
255
.withAttributeNames(QueueAttributeName.All);
256
257
GetQueueAttributesResult result = client.getQueueAttributes(request);
258
Map<String, String> attributes = result.getAttributes();
259
260
// Display key attributes
261
System.out.println("Queue ARN: " + attributes.get("QueueArn"));
262
System.out.println("Messages Available: " + attributes.get("ApproximateNumberOfMessages"));
263
System.out.println("Messages in Flight: " + attributes.get("ApproximateNumberOfMessagesNotVisible"));
264
System.out.println("Visibility Timeout: " + attributes.get("VisibilityTimeout"));
265
266
// Get specific attributes
267
GetQueueAttributesRequest specificRequest = new GetQueueAttributesRequest(queueUrl)
268
.withAttributeNames(
269
QueueAttributeName.ApproximateNumberOfMessages,
270
QueueAttributeName.CreatedTimestamp,
271
QueueAttributeName.Policy
272
);
273
274
GetQueueAttributesResult specificResult = client.getQueueAttributes(specificRequest);
275
```
276
277
### Set Queue Attributes
278
279
Modify queue configuration by setting attribute values.
280
281
```java { .api }
282
SetQueueAttributesResult setQueueAttributes(SetQueueAttributesRequest request);
283
284
// Convenience method
285
SetQueueAttributesResult setQueueAttributes(String queueUrl, Map<String, String> attributes);
286
287
class SetQueueAttributesRequest extends AmazonWebServiceRequest {
288
SetQueueAttributesRequest();
289
SetQueueAttributesRequest(String queueUrl, Map<String, String> attributes);
290
291
String getQueueUrl();
292
SetQueueAttributesRequest withQueueUrl(String queueUrl);
293
294
Map<String, String> getAttributes();
295
SetQueueAttributesRequest withAttributes(Map<String, String> attributes);
296
SetQueueAttributesRequest addAttributesEntry(String key, String value);
297
}
298
299
class SetQueueAttributesResult {
300
// Empty result class - success indicated by no exception
301
}
302
```
303
304
**Usage Example:**
305
306
```java
307
// Update queue attributes
308
Map<String, String> newAttributes = new HashMap<>();
309
newAttributes.put(QueueAttributeName.VisibilityTimeout.toString(), "120"); // 2 minutes
310
newAttributes.put(QueueAttributeName.MessageRetentionPeriod.toString(), "604800"); // 7 days
311
newAttributes.put(QueueAttributeName.ReceiveMessageWaitTimeSeconds.toString(), "20"); // Long polling
312
313
SetQueueAttributesRequest request = new SetQueueAttributesRequest()
314
.withQueueUrl(queueUrl)
315
.withAttributes(newAttributes);
316
317
client.setQueueAttributes(request);
318
319
// Set dead letter queue policy
320
String dlqPolicy = "{"
321
+ "\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:123456789012:MyDeadLetterQueue\","
322
+ "\"maxReceiveCount\":5"
323
+ "}";
324
325
SetQueueAttributesRequest dlqRequest = new SetQueueAttributesRequest()
326
.withQueueUrl(queueUrl)
327
.addAttributesEntry(QueueAttributeName.RedrivePolicy.toString(), dlqPolicy);
328
329
client.setQueueAttributes(dlqRequest);
330
```
331
332
## Queue Maintenance
333
334
### Purge Queue
335
336
Remove all messages from a queue without deleting the queue itself.
337
338
```java { .api }
339
PurgeQueueResult purgeQueue(PurgeQueueRequest request);
340
341
// Convenience method
342
PurgeQueueResult purgeQueue(String queueUrl);
343
344
class PurgeQueueRequest extends AmazonWebServiceRequest {
345
PurgeQueueRequest();
346
PurgeQueueRequest(String queueUrl);
347
348
String getQueueUrl();
349
PurgeQueueRequest withQueueUrl(String queueUrl);
350
}
351
352
class PurgeQueueResult {
353
// Empty result class - success indicated by no exception
354
}
355
```
356
357
**Usage Example:**
358
359
```java
360
try {
361
client.purgeQueue(new PurgeQueueRequest(queueUrl));
362
System.out.println("Queue purged successfully");
363
} catch (PurgeQueueInProgressException e) {
364
System.err.println("Purge already in progress: " + e.getMessage());
365
} catch (QueueDoesNotExistException e) {
366
System.err.println("Queue does not exist: " + e.getMessage());
367
}
368
369
// Note: Purge operation can only be called once every 60 seconds
370
```
371
372
## Queue Attribute Reference
373
374
### Standard Queue Attributes
375
376
All available queue attributes and their purposes:
377
378
```java { .api }
379
enum QueueAttributeName {
380
All, // All attributes (for retrieval only)
381
Policy, // Queue access policy
382
VisibilityTimeout, // Message visibility timeout (seconds)
383
MaximumMessageSize, // Maximum message size (bytes)
384
MessageRetentionPeriod, // Message retention period (seconds)
385
ApproximateNumberOfMessages, // Approximate number of visible messages
386
ApproximateNumberOfMessagesNotVisible, // Approximate number of in-flight messages
387
CreatedTimestamp, // Queue creation timestamp
388
LastModifiedTimestamp, // Last modification timestamp
389
QueueArn, // Queue ARN
390
ApproximateNumberOfMessagesDelayed, // Approximate number of delayed messages
391
DelaySeconds, // Default delay for messages (seconds)
392
ReceiveMessageWaitTimeSeconds, // Long polling wait time (seconds)
393
RedrivePolicy, // Dead letter queue configuration
394
FifoQueue, // FIFO queue flag
395
ContentBasedDeduplication, // Content-based deduplication flag
396
KmsMasterKeyId, // KMS key for encryption
397
KmsDataKeyReusePeriodSeconds, // KMS data key reuse period
398
DeduplicationScope, // FIFO deduplication scope
399
FifoThroughputLimit, // FIFO throughput limit
400
RedriveAllowPolicy, // Dead letter queue allow policy
401
SqsManagedSseEnabled // SQS-managed encryption flag
402
}
403
```
404
405
**Common Attribute Values:**
406
407
```java
408
// Visibility timeout: 0 seconds to 12 hours (43200 seconds)
409
attributes.put("VisibilityTimeout", "300"); // 5 minutes
410
411
// Message retention: 60 seconds to 14 days (1209600 seconds)
412
attributes.put("MessageRetentionPeriod", "1209600"); // 14 days
413
414
// Maximum message size: 1024 bytes to 256 KB (262144 bytes)
415
attributes.put("MaximumMessageSize", "262144"); // 256 KB
416
417
// Delay seconds: 0 to 900 seconds (15 minutes)
418
attributes.put("DelaySeconds", "0");
419
420
// Long polling: 0 to 20 seconds
421
attributes.put("ReceiveMessageWaitTimeSeconds", "20");
422
423
// FIFO queue configuration
424
attributes.put("FifoQueue", "true");
425
attributes.put("ContentBasedDeduplication", "true");
426
```
427
428
## Error Handling
429
430
Common exceptions for queue operations:
431
432
```java
433
try {
434
CreateQueueResult result = client.createQueue(request);
435
} catch (QueueNameExistsException e) {
436
// Queue exists with different attributes
437
System.err.println("Queue exists with different attributes: " + e.getMessage());
438
} catch (QueueDeletedRecentlyException e) {
439
// Queue was recently deleted (60 second cooldown)
440
System.err.println("Queue recently deleted, wait before recreating: " + e.getMessage());
441
}
442
443
try {
444
client.setQueueAttributes(request);
445
} catch (InvalidAttributeNameException e) {
446
System.err.println("Invalid attribute name: " + e.getMessage());
447
} catch (InvalidAttributeValueException e) {
448
System.err.println("Invalid attribute value: " + e.getMessage());
449
}
450
451
try {
452
client.purgeQueue(request);
453
} catch (PurgeQueueInProgressException e) {
454
System.err.println("Purge operation already in progress");
455
}
456
```