17
17
18
18
import static java .util .Collections .singletonMap ;
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
20
21
21
22
import com .fasterxml .jackson .databind .ObjectMapper ;
22
23
import io .awspring .cloud .sqs .CompletableFutures ;
53
54
import java .util .Collections ;
54
55
import java .util .List ;
55
56
import java .util .UUID ;
57
+ import java .util .concurrent .BrokenBarrierException ;
56
58
import java .util .concurrent .CompletableFuture ;
57
59
import java .util .concurrent .CountDownLatch ;
60
+ import java .util .concurrent .CyclicBarrier ;
58
61
import java .util .concurrent .TimeUnit ;
59
62
import java .util .concurrent .atomic .AtomicBoolean ;
60
63
import java .util .stream .Collectors ;
@@ -116,6 +119,8 @@ class SqsIntegrationTests extends BaseSqsIntegrationTest {
116
119
117
120
static final String MANUALLY_CREATE_FACTORY_QUEUE_NAME = "manually_create_factory_test_queue" ;
118
121
122
+ static final String MAX_CONCURRENT_MESSAGES_QUEUE_NAME = "max_concurrent_messages_test_queue" ;
123
+
119
124
static final String LOW_RESOURCE_FACTORY = "lowResourceFactory" ;
120
125
121
126
static final String MANUAL_ACK_FACTORY = "manualAcknowledgementFactory" ;
@@ -139,7 +144,8 @@ static void beforeTests() {
139
144
createQueue (client , RESOLVES_PARAMETER_TYPES_QUEUE_NAME ,
140
145
singletonMap (QueueAttributeName .VISIBILITY_TIMEOUT , "20" )),
141
146
createQueue (client , MANUALLY_CREATE_CONTAINER_QUEUE_NAME ),
142
- createQueue (client , MANUALLY_CREATE_FACTORY_QUEUE_NAME )).join ();
147
+ createQueue (client , MANUALLY_CREATE_FACTORY_QUEUE_NAME ),
148
+ createQueue (client , MAX_CONCURRENT_MESSAGES_QUEUE_NAME )).join ();
143
149
}
144
150
145
151
@ Autowired
@@ -275,6 +281,20 @@ void manuallyCreatesFactory() throws Exception {
275
281
assertThat (latchContainer .manuallyCreatedFactorySinkLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
276
282
}
277
283
284
+ @ Test
285
+ void maxConcurrentMessages () {
286
+ List <Message <String >> messages1 = IntStream .range (0 , 10 )
287
+ .mapToObj (index -> "maxConcurrentMessages-payload-" + index )
288
+ .map (payload -> MessageBuilder .withPayload (payload ).build ()).collect (Collectors .toList ());
289
+ List <Message <String >> messages2 = IntStream .range (10 , 20 )
290
+ .mapToObj (index -> "maxConcurrentMessages-payload-" + index )
291
+ .map (payload -> MessageBuilder .withPayload (payload ).build ()).collect (Collectors .toList ());
292
+ sqsTemplate .sendManyAsync (MAX_CONCURRENT_MESSAGES_QUEUE_NAME , messages1 );
293
+ sqsTemplate .sendManyAsync (MAX_CONCURRENT_MESSAGES_QUEUE_NAME , messages2 );
294
+ logger .debug ("Sent messages to queue {} with messages {} and {}" , MAX_CONCURRENT_MESSAGES_QUEUE_NAME , messages1 , messages2 );
295
+ assertDoesNotThrow (() -> latchContainer .maxConcurrentMessagesBarrier .await (10 , TimeUnit .SECONDS ));
296
+ }
297
+
278
298
static class ReceivesMessageListener {
279
299
280
300
@ Autowired
@@ -399,6 +419,18 @@ void listen(Message<String> message, MessageHeaders headers, Acknowledgement ack
399
419
}
400
420
}
401
421
422
+ static class MaxConcurrentMessagesListener {
423
+
424
+ @ Autowired
425
+ LatchContainer latchContainer ;
426
+
427
+ @ SqsListener (queueNames = MAX_CONCURRENT_MESSAGES_QUEUE_NAME , maxMessagesPerPoll = "10" , maxConcurrentMessages = "20" , id = "max-concurrent-messages" )
428
+ void listen (String message ) throws BrokenBarrierException , InterruptedException {
429
+ logger .debug ("Received message in Listener Method: " + message );
430
+ latchContainer .maxConcurrentMessagesBarrier .await ();
431
+ }
432
+ }
433
+
402
434
static class LatchContainer {
403
435
404
436
final CountDownLatch receivesMessageLatch = new CountDownLatch (1 );
@@ -421,6 +453,7 @@ static class LatchContainer {
421
453
final CountDownLatch acknowledgementCallbackSuccessLatch = new CountDownLatch (1 );
422
454
final CountDownLatch acknowledgementCallbackBatchLatch = new CountDownLatch (1 );
423
455
final CountDownLatch acknowledgementCallbackErrorLatch = new CountDownLatch (1 );
456
+ final CyclicBarrier maxConcurrentMessagesBarrier = new CyclicBarrier (21 );
424
457
425
458
}
426
459
@@ -612,6 +645,11 @@ ResolvesParameterTypesListener resolvesParameterTypesListener() {
612
645
return new ResolvesParameterTypesListener ();
613
646
}
614
647
648
+ @ Bean
649
+ MaxConcurrentMessagesListener maxConcurrentMessagesListener () {
650
+ return new MaxConcurrentMessagesListener ();
651
+ }
652
+
615
653
@ Bean
616
654
SqsListenerConfigurer customizer () {
617
655
return registrar -> {
0 commit comments