-
Notifications
You must be signed in to change notification settings - Fork 108
/
AmazonSQSExtendedClient.java
1277 lines (1183 loc) · 57.1 KB
/
AmazonSQSExtendedClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.sqs.javamessaging;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.Map.Entry;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.BatchEntryIdsNotDistinctException;
import com.amazonaws.services.sqs.model.BatchRequestTooLongException;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequest;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequestEntry;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchResult;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest;
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityResult;
import com.amazonaws.services.sqs.model.DeleteMessageBatchRequest;
import com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry;
import com.amazonaws.services.sqs.model.DeleteMessageBatchResult;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteMessageResult;
import com.amazonaws.services.sqs.model.EmptyBatchRequestException;
import com.amazonaws.services.sqs.model.InvalidBatchEntryIdException;
import com.amazonaws.services.sqs.model.InvalidIdFormatException;
import com.amazonaws.services.sqs.model.InvalidMessageContentsException;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.MessageNotInflightException;
import com.amazonaws.services.sqs.model.OverLimitException;
import com.amazonaws.services.sqs.model.PurgeQueueRequest;
import com.amazonaws.services.sqs.model.PurgeQueueResult;
import com.amazonaws.services.sqs.model.ReceiptHandleIsInvalidException;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry;
import com.amazonaws.services.sqs.model.SendMessageBatchResult;
import com.amazonaws.services.sqs.model.SendMessageRequest;
import com.amazonaws.services.sqs.model.SendMessageResult;
import com.amazonaws.services.sqs.model.TooManyEntriesInBatchRequestException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import software.amazon.payloadoffloading.*;
/**
* Amazon SQS Extended Client extends the functionality of Amazon SQS client.
* All service calls made using this client are blocking, and will not return
* until the service call completes.
*
* <p>
* The Amazon SQS extended client enables sending and receiving large messages
* via Amazon S3. You can use this library to:
* </p>
*
* <ul>
* <li>Specify whether messages are always stored in Amazon S3 or only when a
* message size exceeds 256 KB.</li>
* <li>Send a message that references a single message object stored in an
* Amazon S3 bucket.</li>
* <li>Get the corresponding message object from an Amazon S3 bucket.</li>
* <li>Delete the corresponding message object from an Amazon S3 bucket.</li>
* </ul>
*/
public class AmazonSQSExtendedClient extends AmazonSQSExtendedClientBase implements AmazonSQS {
private static final Log LOG = LogFactory.getLog(AmazonSQSExtendedClient.class);
private static final String USER_AGENT_HEADER = Util.getUserAgentHeader(AmazonSQSExtendedClient.class.getSimpleName());
static final String LEGACY_RESERVED_ATTRIBUTE_NAME = "SQSLargePayloadSize";
static final List<String> RESERVED_ATTRIBUTE_NAMES = Arrays.asList(LEGACY_RESERVED_ATTRIBUTE_NAME,
SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
private ExtendedClientConfiguration clientConfiguration;
private PayloadStore payloadStore;
/**
* Constructs a new Amazon SQS extended client to invoke service methods on
* Amazon SQS with extended functionality using the specified Amazon SQS
* client object.
*
* <p>
* All service calls made using this new client object are blocking, and
* will not return until the service call completes.
*
* @param sqsClient
* The Amazon SQS client to use to connect to Amazon SQS.
*/
public AmazonSQSExtendedClient(AmazonSQS sqsClient) {
this(sqsClient, new ExtendedClientConfiguration());
}
/**
* Constructs a new Amazon SQS extended client to invoke service methods on
* Amazon SQS with extended functionality using the specified Amazon SQS
* client object.
*
* <p>
* All service calls made using this new client object are blocking, and
* will not return until the service call completes.
*
* @param sqsClient
* The Amazon SQS client to use to connect to Amazon SQS.
* @param extendedClientConfig
* The extended client configuration options controlling the
* functionality of this client.
*/
public AmazonSQSExtendedClient(AmazonSQS sqsClient, ExtendedClientConfiguration extendedClientConfig) {
super(sqsClient);
this.clientConfiguration = new ExtendedClientConfiguration(extendedClientConfig);
S3Dao s3Dao = new S3Dao(clientConfiguration.getAmazonS3Client(),
clientConfiguration.getSSEAwsKeyManagementParams(),
clientConfiguration.getCannedAccessControlList());
this.payloadStore = new S3BackedPayloadStore(s3Dao, clientConfiguration.getS3BucketName());
}
/**
* <p>
* Delivers a message to the specified queue and uploads the message payload
* to Amazon S3 if necessary.
* </p>
* <p>
* <b>IMPORTANT:</b> The following list shows the characters (in Unicode)
* allowed in your message, according to the W3C XML specification. For more
* information, go to http://www.w3.org/TR/REC-xml/#charsets If you send any
* characters not included in the list, your request will be rejected. #x9 |
* #xA | #xD | [#x20 to #xD7FF] | [#xE000 to #xFFFD] | [#x10000 to #x10FFFF]
* </p>
*
* <b>IMPORTANT:</b> The input object may be modified by the method. </p>
*
* @param sendMessageRequest
* Container for the necessary parameters to execute the
* SendMessage service method on AmazonSQS.
*
* @return The response from the SendMessage service method, as returned by
* AmazonSQS.
*
* @throws InvalidMessageContentsException
* @throws UnsupportedOperationException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public SendMessageResult sendMessage(SendMessageRequest sendMessageRequest) {
//TODO: Clone request since it's modified in this method and will cause issues if the client reuses request object.
if (sendMessageRequest == null) {
String errorMessage = "sendMessageRequest cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
sendMessageRequest.getRequestClientOptions().appendUserAgent(USER_AGENT_HEADER);
if (!clientConfiguration.isPayloadSupportEnabled()) {
return super.sendMessage(sendMessageRequest);
}
if (sendMessageRequest.getMessageBody() == null || "".equals(sendMessageRequest.getMessageBody())) {
String errorMessage = "messageBody cannot be null or empty.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
//Check message attributes for ExtendedClient related constraints
checkMessageAttributes(sendMessageRequest.getMessageAttributes());
if (clientConfiguration.isAlwaysThroughS3() || isLarge(sendMessageRequest)) {
sendMessageRequest = storeMessageInS3(sendMessageRequest);
}
return super.sendMessage(sendMessageRequest);
}
/**
* <p>
* Delivers a message to the specified queue and uploads the message payload
* to Amazon S3 if necessary.
* </p>
* <p>
* <b>IMPORTANT:</b> The following list shows the characters (in Unicode)
* allowed in your message, according to the W3C XML specification. For more
* information, go to http://www.w3.org/TR/REC-xml/#charsets If you send any
* characters not included in the list, your request will be rejected. #x9 |
* #xA | #xD | [#x20 to #xD7FF] | [#xE000 to #xFFFD] | [#x10000 to #x10FFFF]
* </p>
*
* @param queueUrl
* The URL of the Amazon SQS queue to take action on.
* @param messageBody
* The message to send. For a list of allowed characters, see the
* preceding important note.
*
* @return The response from the SendMessage service method, as returned by
* AmazonSQS.
*
* @throws InvalidMessageContentsException
* @throws UnsupportedOperationException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public SendMessageResult sendMessage(String queueUrl, String messageBody) {
SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, messageBody);
return sendMessage(sendMessageRequest);
}
/**
* <p>
* Retrieves one or more messages, with a maximum limit of 10 messages, from
* the specified queue. Downloads the message payloads from Amazon S3 when
* necessary. Long poll support is enabled by using the
* <code>WaitTimeSeconds</code> parameter. For more information, see <a
* href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html"
* > Amazon SQS Long Poll </a> in the <i>Amazon SQS Developer Guide</i> .
* </p>
* <p>
* Short poll is the default behavior where a weighted random set of
* machines is sampled on a <code>ReceiveMessage</code> call. This means
* only the messages on the sampled machines are returned. If the number of
* messages in the queue is small (less than 1000), it is likely you will
* get fewer messages than you requested per <code>ReceiveMessage</code>
* call. If the number of messages in the queue is extremely small, you
* might not receive any messages in a particular
* <code>ReceiveMessage</code> response; in which case you should repeat the
* request.
* </p>
* <p>
* For each message returned, the response includes the following:
* </p>
*
* <ul>
* <li>
* <p>
* Message body
* </p>
* </li>
* <li>
* <p>
* MD5 digest of the message body. For information about MD5, go to <a
* href="http://www.faqs.org/rfcs/rfc1321.html">
* http://www.faqs.org/rfcs/rfc1321.html </a> .
* </p>
* </li>
* <li>
* <p>
* Message ID you received when you sent the message to the queue.
* </p>
* </li>
* <li>
* <p>
* Receipt handle.
* </p>
* </li>
* <li>
* <p>
* Message attributes.
* </p>
* </li>
* <li>
* <p>
* MD5 digest of the message attributes.
* </p>
* </li>
*
* </ul>
* <p>
* The receipt handle is the identifier you must provide when deleting the
* message. For more information, see <a href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ImportantIdentifiers.html"
* > Queue and Message Identifiers </a> in the <i>Amazon SQS Developer
* Guide</i> .
* </p>
* <p>
* You can provide the <code>VisibilityTimeout</code> parameter in your
* request, which will be applied to the messages that Amazon SQS returns in
* the response. If you do not include the parameter, the overall visibility
* timeout for the queue is used for the returned messages. For more
* information, see <a href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html"
* > Visibility Timeout </a> in the <i>Amazon SQS Developer Guide</i> .
* </p>
* <p>
* <b>NOTE:</b> Going forward, new attributes might be added. If you are
* writing code that calls this action, we recommend that you structure your
* code so that it can handle new attributes gracefully.
* </p>
*
* @param receiveMessageRequest
* Container for the necessary parameters to execute the
* ReceiveMessage service method on AmazonSQS.
*
* @return The response from the ReceiveMessage service method, as returned
* by AmazonSQS.
*
* @throws OverLimitException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public ReceiveMessageResult receiveMessage(ReceiveMessageRequest receiveMessageRequest) {
//TODO: Clone request since it's modified in this method and will cause issues if the client reuses request object.
if (receiveMessageRequest == null) {
String errorMessage = "receiveMessageRequest cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
receiveMessageRequest.getRequestClientOptions().appendUserAgent(USER_AGENT_HEADER);
if (!clientConfiguration.isPayloadSupportEnabled()) {
return super.receiveMessage(receiveMessageRequest);
}
//Remove before adding to avoid any duplicates
receiveMessageRequest.getMessageAttributeNames().removeAll(RESERVED_ATTRIBUTE_NAMES);
receiveMessageRequest.getMessageAttributeNames().addAll(RESERVED_ATTRIBUTE_NAMES);
ReceiveMessageResult receiveMessageResult = super.receiveMessage(receiveMessageRequest);
List<Message> messages = receiveMessageResult.getMessages();
for (Message message : messages) {
// for each received message check if they are stored in S3.
Optional<String> largePayloadAttributeName = getReservedAttributeNameIfPresent(message.getMessageAttributes());
if (largePayloadAttributeName.isPresent()) {
String largeMessagePointer = message.getBody();
largeMessagePointer = largeMessagePointer.replace("com.amazon.sqs.javamessaging.MessageS3Pointer", "software.amazon.payloadoffloading.PayloadS3Pointer");
message.setBody(payloadStore.getOriginalPayload(largeMessagePointer));
// remove the additional attribute before returning the message
// to user.
message.getMessageAttributes().keySet().removeAll(RESERVED_ATTRIBUTE_NAMES);
// Embed s3 object pointer in the receipt handle.
String modifiedReceiptHandle = embedS3PointerInReceiptHandle(
message.getReceiptHandle(),
largeMessagePointer);
message.setReceiptHandle(modifiedReceiptHandle);
}
}
return receiveMessageResult;
}
/**
* <p>
* Retrieves one or more messages, with a maximum limit of 10 messages, from
* the specified queue. Downloads the message payloads from Amazon S3 when
* necessary. Long poll support is enabled by using the
* <code>WaitTimeSeconds</code> parameter. For more information, see <a
* href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html"
* > Amazon SQS Long Poll </a> in the <i>Amazon SQS Developer Guide</i> .
* </p>
* <p>
* Short poll is the default behavior where a weighted random set of
* machines is sampled on a <code>ReceiveMessage</code> call. This means
* only the messages on the sampled machines are returned. If the number of
* messages in the queue is small (less than 1000), it is likely you will
* get fewer messages than you requested per <code>ReceiveMessage</code>
* call. If the number of messages in the queue is extremely small, you
* might not receive any messages in a particular
* <code>ReceiveMessage</code> response; in which case you should repeat the
* request.
* </p>
* <p>
* For each message returned, the response includes the following:
* </p>
*
* <ul>
* <li>
* <p>
* Message body
* </p>
* </li>
* <li>
* <p>
* MD5 digest of the message body. For information about MD5, go to <a
* href="http://www.faqs.org/rfcs/rfc1321.html">
* http://www.faqs.org/rfcs/rfc1321.html </a> .
* </p>
* </li>
* <li>
* <p>
* Message ID you received when you sent the message to the queue.
* </p>
* </li>
* <li>
* <p>
* Receipt handle.
* </p>
* </li>
* <li>
* <p>
* Message attributes.
* </p>
* </li>
* <li>
* <p>
* MD5 digest of the message attributes.
* </p>
* </li>
*
* </ul>
* <p>
* The receipt handle is the identifier you must provide when deleting the
* message. For more information, see <a href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ImportantIdentifiers.html"
* > Queue and Message Identifiers </a> in the <i>Amazon SQS Developer
* Guide</i> .
* </p>
* <p>
* You can provide the <code>VisibilityTimeout</code> parameter in your
* request, which will be applied to the messages that Amazon SQS returns in
* the response. If you do not include the parameter, the overall visibility
* timeout for the queue is used for the returned messages. For more
* information, see <a href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html"
* > Visibility Timeout </a> in the <i>Amazon SQS Developer Guide</i> .
* </p>
* <p>
* <b>NOTE:</b> Going forward, new attributes might be added. If you are
* writing code that calls this action, we recommend that you structure your
* code so that it can handle new attributes gracefully.
* </p>
*
* @param queueUrl
* The URL of the Amazon SQS queue to take action on.
*
* @return The response from the ReceiveMessage service method, as returned
* by AmazonSQS.
*
* @throws OverLimitException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public ReceiveMessageResult receiveMessage(String queueUrl) {
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
return receiveMessage(receiveMessageRequest);
}
/**
* <p>
* Deletes the specified message from the specified queue and deletes the
* message payload from Amazon S3 when necessary. You specify the message by
* using the message's <code>receipt handle</code> and not the
* <code>message ID</code> you received when you sent the message. Even if
* the message is locked by another reader due to the visibility timeout
* setting, it is still deleted from the queue. If you leave a message in
* the queue for longer than the queue's configured retention period, Amazon
* SQS automatically deletes it.
* </p>
* <p>
* <b>NOTE:</b> The receipt handle is associated with a specific instance of
* receiving the message. If you receive a message more than once, the
* receipt handle you get each time you receive the message is different.
* When you request DeleteMessage, if you don't provide the most recently
* received receipt handle for the message, the request will still succeed,
* but the message might not be deleted.
* </p>
* <p>
* <b>IMPORTANT:</b> It is possible you will receive a message even after
* you have deleted it. This might happen on rare occasions if one of the
* servers storing a copy of the message is unavailable when you request to
* delete the message. The copy remains on the server and might be returned
* to you again on a subsequent receive request. You should create your
* system to be idempotent so that receiving a particular message more than
* once is not a problem.
* </p>
*
* @param deleteMessageRequest
* Container for the necessary parameters to execute the
* DeleteMessage service method on AmazonSQS.
*
* @return The response from the DeleteMessage service method, as returned
* by AmazonSQS.
*
* @throws ReceiptHandleIsInvalidException
* @throws InvalidIdFormatException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public DeleteMessageResult deleteMessage(DeleteMessageRequest deleteMessageRequest) {
if (deleteMessageRequest == null) {
String errorMessage = "deleteMessageRequest cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
deleteMessageRequest.getRequestClientOptions().appendUserAgent(USER_AGENT_HEADER);
if (!clientConfiguration.isPayloadSupportEnabled()) {
return super.deleteMessage(deleteMessageRequest);
}
String receiptHandle = deleteMessageRequest.getReceiptHandle();
String origReceiptHandle = receiptHandle;
// Update original receipt handle if needed
if (isS3ReceiptHandle(receiptHandle)) {
origReceiptHandle = getOrigReceiptHandle(receiptHandle);
// Delete pay load from S3 if needed
if (clientConfiguration.doesCleanupS3Payload()) {
String messagePointer = getMessagePointerFromModifiedReceiptHandle(receiptHandle);
payloadStore.deleteOriginalPayload(messagePointer);
}
}
deleteMessageRequest.setReceiptHandle(origReceiptHandle);
return super.deleteMessage(deleteMessageRequest);
}
/**
* <p>
* Deletes the specified message from the specified queue and deletes the
* message payload from Amazon S3 when necessary. You specify the message by
* using the message's <code>receipt handle</code> and not the
* <code>message ID</code> you received when you sent the message. Even if
* the message is locked by another reader due to the visibility timeout
* setting, it is still deleted from the queue. If you leave a message in
* the queue for longer than the queue's configured retention period, Amazon
* SQS automatically deletes it.
* </p>
* <p>
* <b>NOTE:</b> The receipt handle is associated with a specific instance of
* receiving the message. If you receive a message more than once, the
* receipt handle you get each time you receive the message is different.
* When you request DeleteMessage, if you don't provide the most recently
* received receipt handle for the message, the request will still succeed,
* but the message might not be deleted.
* </p>
* <p>
* <b>IMPORTANT:</b> It is possible you will receive a message even after
* you have deleted it. This might happen on rare occasions if one of the
* servers storing a copy of the message is unavailable when you request to
* delete the message. The copy remains on the server and might be returned
* to you again on a subsequent receive request. You should create your
* system to be idempotent so that receiving a particular message more than
* once is not a problem.
* </p>
*
* @param queueUrl
* The URL of the Amazon SQS queue to take action on.
* @param receiptHandle
* The receipt handle associated with the message to delete.
*
* @return The response from the DeleteMessage service method, as returned
* by AmazonSQS.
*
* @throws ReceiptHandleIsInvalidException
* @throws InvalidIdFormatException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public DeleteMessageResult deleteMessage(String queueUrl, String receiptHandle) {
DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(queueUrl, receiptHandle);
return deleteMessage(deleteMessageRequest);
}
/**
* Simplified method form for invoking the ChangeMessageVisibility
* operation.
*
* @see #changeMessageVisibility(ChangeMessageVisibilityRequest)
*/
public ChangeMessageVisibilityResult changeMessageVisibility(String queueUrl,
String receiptHandle,
Integer visibilityTimeout) {
ChangeMessageVisibilityRequest changeMessageVisibilityRequest =
new ChangeMessageVisibilityRequest(queueUrl, receiptHandle, visibilityTimeout);
return changeMessageVisibility(changeMessageVisibilityRequest);
}
/**
* <p>
* Changes the visibility timeout of a specified message in a queue to a new
* value. The maximum allowed timeout value you can set the value to is 12
* hours. This means you can't extend the timeout of a message in an
* existing queue to more than a total visibility timeout of 12 hours. (For
* more information visibility timeout, see <a href=
* "http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html"
* > Visibility Timeout </a> in the <i>Amazon SQS Developer Guide</i> .)
* </p>
* <p>
* For example, let's say you have a message and its default message
* visibility timeout is 30 minutes. You could call
* <code>ChangeMessageVisiblity</code> with a value of two hours and the
* effective timeout would be two hours and 30 minutes. When that time comes
* near you could again extend the time out by calling
* ChangeMessageVisiblity, but this time the maximum allowed timeout would
* be 9 hours and 30 minutes.
* </p>
* <p>
* <b>NOTE:</b> There is a 120,000 limit for the number of inflight messages
* per queue. Messages are inflight after they have been received from the
* queue by a consuming component, but have not yet been deleted from the
* queue. If you reach the 120,000 limit, you will receive an OverLimit
* error message from Amazon SQS. To help avoid reaching the limit, you
* should delete the messages from the queue after they have been processed.
* You can also increase the number of queues you use to process the
* messages.
* </p>
* <p>
* <b>IMPORTANT:</b>If you attempt to set the VisibilityTimeout to an amount
* more than the maximum time left, Amazon SQS returns an error. It will not
* automatically recalculate and increase the timeout to the maximum time
* remaining.
* </p>
* <p>
* <b>IMPORTANT:</b>Unlike with a queue, when you change the visibility
* timeout for a specific message, that timeout value is applied immediately
* but is not saved in memory for that message. If you don't delete a
* message after it is received, the visibility timeout for the message the
* next time it is received reverts to the original timeout value, not the
* value you set with the ChangeMessageVisibility action.
* </p>
*
* @param changeMessageVisibilityRequest
* Container for the necessary parameters to execute the
* ChangeMessageVisibility service method on AmazonSQS.
*
*
* @throws ReceiptHandleIsInvalidException
* @throws MessageNotInflightException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public ChangeMessageVisibilityResult changeMessageVisibility(ChangeMessageVisibilityRequest changeMessageVisibilityRequest)
throws AmazonServiceException, AmazonClientException {
if (isS3ReceiptHandle(changeMessageVisibilityRequest.getReceiptHandle())) {
changeMessageVisibilityRequest.setReceiptHandle(
getOrigReceiptHandle(changeMessageVisibilityRequest.getReceiptHandle()));
}
return amazonSqsToBeExtended.changeMessageVisibility(changeMessageVisibilityRequest);
}
/**
* <p>
* Delivers up to ten messages to the specified queue. This is a batch
* version of SendMessage. The result of the send action on each message is
* reported individually in the response. Uploads message payloads to Amazon
* S3 when necessary.
* </p>
* <p>
* If the <code>DelaySeconds</code> parameter is not specified for an entry,
* the default for the queue is used.
* </p>
* <p>
* <b>IMPORTANT:</b>The following list shows the characters (in Unicode)
* that are allowed in your message, according to the W3C XML specification.
* For more information, go to http://www.faqs.org/rfcs/rfc1321.html. If you
* send any characters that are not included in the list, your request will
* be rejected. #x9 | #xA | #xD | [#x20 to #xD7FF] | [#xE000 to #xFFFD] |
* [#x10000 to #x10FFFF]
* </p>
* <p>
* <b>IMPORTANT:</b> Because the batch request can result in a combination
* of successful and unsuccessful actions, you should check for batch errors
* even when the call returns an HTTP status code of 200.
* </p>
* <b>IMPORTANT:</b> The input object may be modified by the method. </p>
* <p>
* <b>NOTE:</b>Some API actions take lists of parameters. These lists are
* specified using the param.n notation. Values of n are integers starting
* from 1. For example, a parameter list with two elements looks like this:
* </p>
* <p>
* <code>&Attribute.1=this</code>
* </p>
* <p>
* <code>&Attribute.2=that</code>
* </p>
*
* @param sendMessageBatchRequest
* Container for the necessary parameters to execute the
* SendMessageBatch service method on AmazonSQS.
*
* @return The response from the SendMessageBatch service method, as
* returned by AmazonSQS.
*
* @throws BatchEntryIdsNotDistinctException
* @throws TooManyEntriesInBatchRequestException
* @throws BatchRequestTooLongException
* @throws UnsupportedOperationException
* @throws InvalidBatchEntryIdException
* @throws EmptyBatchRequestException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public SendMessageBatchResult sendMessageBatch(SendMessageBatchRequest sendMessageBatchRequest) {
if (sendMessageBatchRequest == null) {
String errorMessage = "sendMessageBatchRequest cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
sendMessageBatchRequest.getRequestClientOptions().appendUserAgent(USER_AGENT_HEADER);
if (!clientConfiguration.isPayloadSupportEnabled()) {
return super.sendMessageBatch(sendMessageBatchRequest);
}
List<SendMessageBatchRequestEntry> batchEntries = sendMessageBatchRequest.getEntries();
int index = 0;
for (SendMessageBatchRequestEntry entry : batchEntries) {
//Check message attributes for ExtendedClient related constraints
checkMessageAttributes(entry.getMessageAttributes());
if (clientConfiguration.isAlwaysThroughS3() || isLarge(entry)) {
batchEntries.set(index, storeMessageInS3(entry));
}
++index;
}
return super.sendMessageBatch(sendMessageBatchRequest);
}
/**
* <p>
* Delivers up to ten messages to the specified queue. This is a batch
* version of SendMessage. The result of the send action on each message is
* reported individually in the response. Uploads message payloads to Amazon
* S3 when necessary.
* </p>
* <p>
* If the <code>DelaySeconds</code> parameter is not specified for an entry,
* the default for the queue is used.
* </p>
* <p>
* <b>IMPORTANT:</b>The following list shows the characters (in Unicode)
* that are allowed in your message, according to the W3C XML specification.
* For more information, go to http://www.faqs.org/rfcs/rfc1321.html. If you
* send any characters that are not included in the list, your request will
* be rejected. #x9 | #xA | #xD | [#x20 to #xD7FF] | [#xE000 to #xFFFD] |
* [#x10000 to #x10FFFF]
* </p>
* <p>
* <b>IMPORTANT:</b> Because the batch request can result in a combination
* of successful and unsuccessful actions, you should check for batch errors
* even when the call returns an HTTP status code of 200.
* </p>
* <p>
* <b>NOTE:</b>Some API actions take lists of parameters. These lists are
* specified using the param.n notation. Values of n are integers starting
* from 1. For example, a parameter list with two elements looks like this:
* </p>
* <p>
* <code>&Attribute.1=this</code>
* </p>
* <p>
* <code>&Attribute.2=that</code>
* </p>
*
* @param queueUrl
* The URL of the Amazon SQS queue to take action on.
* @param entries
* A list of <a>SendMessageBatchRequestEntry</a> items.
*
* @return The response from the SendMessageBatch service method, as
* returned by AmazonSQS.
*
* @throws BatchEntryIdsNotDistinctException
* @throws TooManyEntriesInBatchRequestException
* @throws BatchRequestTooLongException
* @throws UnsupportedOperationException
* @throws InvalidBatchEntryIdException
* @throws EmptyBatchRequestException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public SendMessageBatchResult sendMessageBatch(String queueUrl, List<SendMessageBatchRequestEntry> entries) {
SendMessageBatchRequest sendMessageBatchRequest = new SendMessageBatchRequest(queueUrl, entries);
return sendMessageBatch(sendMessageBatchRequest);
}
/**
* <p>
* Deletes up to ten messages from the specified queue. This is a batch
* version of DeleteMessage. The result of the delete action on each message
* is reported individually in the response. Also deletes the message
* payloads from Amazon S3 when necessary.
* </p>
* <p>
* <b>IMPORTANT:</b> Because the batch request can result in a combination
* of successful and unsuccessful actions, you should check for batch errors
* even when the call returns an HTTP status code of 200.
* </p>
* <p>
* <b>NOTE:</b>Some API actions take lists of parameters. These lists are
* specified using the param.n notation. Values of n are integers starting
* from 1. For example, a parameter list with two elements looks like this:
* </p>
* <p>
* <code>&Attribute.1=this</code>
* </p>
* <p>
* <code>&Attribute.2=that</code>
* </p>
*
* @param deleteMessageBatchRequest
* Container for the necessary parameters to execute the
* DeleteMessageBatch service method on AmazonSQS.
*
* @return The response from the DeleteMessageBatch service method, as
* returned by AmazonSQS.
*
* @throws BatchEntryIdsNotDistinctException
* @throws TooManyEntriesInBatchRequestException
* @throws InvalidBatchEntryIdException
* @throws EmptyBatchRequestException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public DeleteMessageBatchResult deleteMessageBatch(DeleteMessageBatchRequest deleteMessageBatchRequest) {
if (deleteMessageBatchRequest == null) {
String errorMessage = "deleteMessageBatchRequest cannot be null.";
LOG.error(errorMessage);
throw new AmazonClientException(errorMessage);
}
deleteMessageBatchRequest.getRequestClientOptions().appendUserAgent(USER_AGENT_HEADER);
if (!clientConfiguration.isPayloadSupportEnabled()) {
return super.deleteMessageBatch(deleteMessageBatchRequest);
}
for (DeleteMessageBatchRequestEntry entry : deleteMessageBatchRequest.getEntries()) {
String receiptHandle = entry.getReceiptHandle();
String origReceiptHandle = receiptHandle;
// Update original receipt handle if needed
if (isS3ReceiptHandle(receiptHandle)) {
origReceiptHandle = getOrigReceiptHandle(receiptHandle);
// Delete s3 payload if needed
if (clientConfiguration.doesCleanupS3Payload()) {
String messagePointer = getMessagePointerFromModifiedReceiptHandle(receiptHandle);
payloadStore.deleteOriginalPayload(messagePointer);
}
}
entry.setReceiptHandle(origReceiptHandle);
}
return super.deleteMessageBatch(deleteMessageBatchRequest);
}
/**
* <p>
* Deletes up to ten messages from the specified queue. This is a batch
* version of DeleteMessage. The result of the delete action on each message
* is reported individually in the response. Also deletes the message
* payloads from Amazon S3 when necessary.
* </p>
* <p>
* <b>IMPORTANT:</b> Because the batch request can result in a combination
* of successful and unsuccessful actions, you should check for batch errors
* even when the call returns an HTTP status code of 200.
* </p>
* <p>
* <b>NOTE:</b>Some API actions take lists of parameters. These lists are
* specified using the param.n notation. Values of n are integers starting
* from 1. For example, a parameter list with two elements looks like this:
* </p>
* <p>
* <code>&Attribute.1=this</code>
* </p>
* <p>
* <code>&Attribute.2=that</code>
* </p>
*
* @param queueUrl
* The URL of the Amazon SQS queue to take action on.
* @param entries
* A list of receipt handles for the messages to be deleted.
*
* @return The response from the DeleteMessageBatch service method, as
* returned by AmazonSQS.
*
* @throws BatchEntryIdsNotDistinctException
* @throws TooManyEntriesInBatchRequestException
* @throws InvalidBatchEntryIdException
* @throws EmptyBatchRequestException
*
* @throws AmazonClientException
* If any internal errors are encountered inside the client
* while attempting to make the request or handle the response.
* For example if a network connection is not available.
* @throws AmazonServiceException
* If an error response is returned by AmazonSQS indicating
* either a problem with the data in the request, or a server
* side issue.
*/
public DeleteMessageBatchResult deleteMessageBatch(String queueUrl, List<DeleteMessageBatchRequestEntry> entries) {
DeleteMessageBatchRequest deleteMessageBatchRequest = new DeleteMessageBatchRequest(queueUrl, entries);
return deleteMessageBatch(deleteMessageBatchRequest);
}
/**
* Simplified method form for invoking the ChangeMessageVisibilityBatch
* operation.
*
* @see #changeMessageVisibilityBatch(ChangeMessageVisibilityBatchRequest)
*/
public ChangeMessageVisibilityBatchResult changeMessageVisibilityBatch(
String queueUrl,
java.util.List<ChangeMessageVisibilityBatchRequestEntry> entries) {
ChangeMessageVisibilityBatchRequest changeMessageVisibilityBatchRequest =
new ChangeMessageVisibilityBatchRequest(queueUrl, entries);
return changeMessageVisibilityBatch(changeMessageVisibilityBatchRequest);
}
/**
* <p>
* Changes the visibility timeout of multiple messages. This is a batch
* version of ChangeMessageVisibility. The result of the action on each