0
# AWS Java SDK for Amazon SQS
1
2
The AWS Java SDK for Amazon SQS module provides Java client classes for communicating with Amazon Simple Queue Service (SQS). It enables developers to interact with AWS's fully managed message queuing service, supporting both standard and FIFO queues with comprehensive functionality for creating, configuring, and managing queues, sending and receiving messages, handling message attributes, and implementing reliable distributed communication patterns.
3
4
## Package Information
5
6
- **Package Name**: aws-java-sdk-sqs
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Group ID**: com.amazonaws
10
- **Artifact ID**: aws-java-sdk-sqs
11
- **Version**: 1.12.789
12
- **Installation**: Add to Maven pom.xml:
13
14
```xml
15
<dependency>
16
<groupId>com.amazonaws</groupId>
17
<artifactId>aws-java-sdk-sqs</artifactId>
18
<version>1.12.789</version>
19
</dependency>
20
```
21
22
## Core Imports
23
24
```java
25
import com.amazonaws.services.sqs.AmazonSQS;
26
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
27
import com.amazonaws.services.sqs.model.*;
28
```
29
30
For asynchronous operations:
31
32
```java
33
import com.amazonaws.services.sqs.AmazonSQSAsync;
34
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;
35
```
36
37
For buffered/optimized operations:
38
39
```java
40
import com.amazonaws.services.sqs.buffered.AmazonSQSBufferedAsyncClient;
41
import com.amazonaws.services.sqs.buffered.QueueBufferConfig;
42
```
43
44
## Basic Usage
45
46
```java
47
import com.amazonaws.services.sqs.AmazonSQS;
48
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
49
import com.amazonaws.services.sqs.model.*;
50
51
// Create SQS client
52
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
53
54
// Create a queue
55
CreateQueueRequest createRequest = new CreateQueueRequest("my-queue");
56
CreateQueueResult createResult = sqs.createQueue(createRequest);
57
String queueUrl = createResult.getQueueUrl();
58
59
// Send a message
60
SendMessageRequest sendRequest = new SendMessageRequest()
61
.withQueueUrl(queueUrl)
62
.withMessageBody("Hello from SQS!");
63
SendMessageResult sendResult = sqs.sendMessage(sendRequest);
64
65
// Receive messages
66
ReceiveMessageRequest receiveRequest = new ReceiveMessageRequest(queueUrl);
67
ReceiveMessageResult receiveResult = sqs.receiveMessage(receiveRequest);
68
69
for (Message message : receiveResult.getMessages()) {
70
System.out.println("Message: " + message.getBody());
71
72
// Delete the message after processing
73
sqs.deleteMessage(new DeleteMessageRequest()
74
.withQueueUrl(queueUrl)
75
.withReceiptHandle(message.getReceiptHandle()));
76
}
77
```
78
79
## Architecture
80
81
The AWS Java SDK for SQS is organized around several key components:
82
83
- **Client Interfaces**: `AmazonSQS` (sync) and `AmazonSQSAsync` (async) provide the core API
84
- **Client Implementations**: Standard clients (`AmazonSQSClient`, `AmazonSQSAsyncClient`) and optimized buffered client (`AmazonSQSBufferedAsyncClient`)
85
- **Client Builders**: Fluent builders for easy client configuration and construction
86
- **Request/Response Models**: Strongly-typed classes for all SQS operations
87
- **Exception Hierarchy**: Comprehensive error handling with specific exceptions for different failure scenarios
88
- **Configuration**: Queue-specific configuration options and buffered client tuning parameters
89
90
## Capabilities
91
92
### Client Management
93
94
Core client creation, configuration, and lifecycle management for connecting to Amazon SQS.
95
96
```java { .api }
97
// Main synchronous client interface
98
interface AmazonSQS {
99
void setEndpoint(String endpoint);
100
void setRegion(Region region);
101
void shutdown();
102
ResponseMetadata getCachedResponseMetadata(AmazonWebServiceRequest request);
103
}
104
105
// Client builder for configuration
106
class AmazonSQSClientBuilder {
107
static AmazonSQSClientBuilder standard();
108
static AmazonSQS defaultClient();
109
AmazonSQS build();
110
}
111
```
112
113
[Client Management](./client-management.md)
114
115
### Queue Operations
116
117
Complete queue lifecycle management including creation, deletion, configuration, and listing.
118
119
```java { .api }
120
CreateQueueResult createQueue(CreateQueueRequest request);
121
DeleteQueueResult deleteQueue(DeleteQueueRequest request);
122
GetQueueUrlResult getQueueUrl(GetQueueUrlRequest request);
123
GetQueueAttributesResult getQueueAttributes(GetQueueAttributesRequest request);
124
SetQueueAttributesResult setQueueAttributes(SetQueueAttributesRequest request);
125
ListQueuesResult listQueues(ListQueuesRequest request);
126
PurgeQueueResult purgeQueue(PurgeQueueRequest request);
127
```
128
129
[Queue Operations](./queue-operations.md)
130
131
### Message Operations
132
133
Send, receive, and delete messages with support for both individual and batch operations.
134
135
```java { .api }
136
SendMessageResult sendMessage(SendMessageRequest request);
137
SendMessageBatchResult sendMessageBatch(SendMessageBatchRequest request);
138
ReceiveMessageResult receiveMessage(ReceiveMessageRequest request);
139
DeleteMessageResult deleteMessage(DeleteMessageRequest request);
140
DeleteMessageBatchResult deleteMessageBatch(DeleteMessageBatchRequest request);
141
```
142
143
[Message Operations](./message-operations.md)
144
145
### Message Visibility
146
147
Control message visibility timeouts to manage processing time and prevent duplicate processing.
148
149
```java { .api }
150
ChangeMessageVisibilityResult changeMessageVisibility(ChangeMessageVisibilityRequest request);
151
ChangeMessageVisibilityBatchResult changeMessageVisibilityBatch(ChangeMessageVisibilityBatchRequest request);
152
```
153
154
[Message Visibility](./message-visibility.md)
155
156
### Queue Permissions
157
158
Manage queue access permissions for cross-account access and IAM integration.
159
160
```java { .api }
161
AddPermissionResult addPermission(AddPermissionRequest request);
162
RemovePermissionResult removePermission(RemovePermissionRequest request);
163
```
164
165
[Queue Permissions](./queue-permissions.md)
166
167
### Queue Tagging
168
169
Add, list, and remove cost allocation tags for queue organization and billing.
170
171
```java { .api }
172
TagQueueResult tagQueue(TagQueueRequest request);
173
ListQueueTagsResult listQueueTags(ListQueueTagsRequest request);
174
UntagQueueResult untagQueue(UntagQueueRequest request);
175
```
176
177
[Queue Tagging](./queue-tagging.md)
178
179
### Dead Letter Queues
180
181
Manage dead letter queue functionality and message redrive operations.
182
183
```java { .api }
184
ListDeadLetterSourceQueuesResult listDeadLetterSourceQueues(ListDeadLetterSourceQueuesRequest request);
185
StartMessageMoveTaskResult startMessageMoveTask(StartMessageMoveTaskRequest request);
186
CancelMessageMoveTaskResult cancelMessageMoveTask(CancelMessageMoveTaskRequest request);
187
ListMessageMoveTasksResult listMessageMoveTasks(ListMessageMoveTasksRequest request);
188
```
189
190
[Dead Letter Queues](./dead-letter-queues.md)
191
192
### Asynchronous Operations
193
194
Non-blocking operations with Future-based return types and callback support for improved performance.
195
196
```java { .api }
197
interface AmazonSQSAsync extends AmazonSQS {
198
Future<SendMessageResult> sendMessageAsync(SendMessageRequest request);
199
Future<SendMessageResult> sendMessageAsync(SendMessageRequest request,
200
AsyncHandler<SendMessageRequest, SendMessageResult> asyncHandler);
201
Future<ReceiveMessageResult> receiveMessageAsync(ReceiveMessageRequest request);
202
// All synchronous operations have async equivalents
203
}
204
```
205
206
[Asynchronous Operations](./async-operations.md)
207
208
### Buffered Client
209
210
High-performance client with automatic batching, prefetching, and configurable optimization settings.
211
212
```java { .api }
213
class AmazonSQSBufferedAsyncClient implements AmazonSQSAsync {
214
// Inherits all AmazonSQSAsync methods with optimized implementations
215
}
216
217
class QueueBufferConfig {
218
// Configuration for batching and prefetching behavior
219
QueueBufferConfig withMaxBatchSize(int maxBatchSize);
220
QueueBufferConfig withFlushTimeoutSeconds(long flushTimeoutSeconds);
221
QueueBufferConfig withVisibilityTimeoutSeconds(long visibilityTimeoutSeconds);
222
}
223
```
224
225
[Buffered Client](./buffered-client.md)
226
227
## Core Types
228
229
```java { .api }
230
// Primary message container
231
class Message {
232
String getMessageId();
233
String getReceiptHandle();
234
String getBody();
235
String getMD5OfBody();
236
Map<String, String> getAttributes();
237
Map<String, MessageAttributeValue> getMessageAttributes();
238
String getMD5OfMessageAttributes();
239
}
240
241
// Message attributes for additional metadata
242
class MessageAttributeValue {
243
String getStringValue();
244
java.nio.ByteBuffer getBinaryValue();
245
List<String> getStringListValues();
246
List<java.nio.ByteBuffer> getBinaryListValues();
247
String getDataType();
248
}
249
250
// Queue attribute enumeration
251
enum QueueAttributeName {
252
All, Policy, VisibilityTimeout, MaximumMessageSize, MessageRetentionPeriod,
253
ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible,
254
CreatedTimestamp, LastModifiedTimestamp, QueueArn, DelaySeconds,
255
ReceiveMessageWaitTimeSeconds, RedrivePolicy, FifoQueue, ContentBasedDeduplication,
256
KmsMasterKeyId, KmsDataKeyReusePeriodSeconds, DeduplicationScope,
257
FifoThroughputLimit, RedriveAllowPolicy, SqsManagedSseEnabled
258
}
259
260
// Batch operation error details
261
class BatchResultErrorEntry {
262
String getId();
263
boolean isSenderFault();
264
String getCode();
265
String getMessage();
266
}
267
```