Skip to content

Commit 860d464

Browse files
committed
rollback to AtomicBoolean
1 parent c5be568 commit 860d464

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

powertools-batch/src/main/java/software/amazon/lambda/powertools/batch/handler/SqsBatchMessageHandler.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Optional;
23+
import java.util.concurrent.atomic.AtomicBoolean;
2324
import java.util.function.BiConsumer;
2425
import java.util.function.Consumer;
2526
import java.util.stream.Collectors;
@@ -65,10 +66,10 @@ public SQSBatchResponse processBatch(SQSEvent event, Context context) {
6566
// If we are working on a FIFO queue, when any message fails we should stop processing and return the
6667
// rest of the batch as failed too. We use this variable to track when that has happened.
6768
// https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting
68-
final Boolean[] failWholeBatch = {false};
69+
final AtomicBoolean failWholeBatch = new AtomicBoolean(false);
6970

7071
int messageCursor = 0;
71-
for (; messageCursor < event.getRecords().size() && !failWholeBatch[0]; messageCursor++) {
72+
for (; messageCursor < event.getRecords().size() && !failWholeBatch.get(); messageCursor++) {
7273
SQSEvent.SQSMessage message = event.getRecords().get(messageCursor);
7374

7475
String messageGroupId = message.getAttributes() != null ?
@@ -77,15 +78,15 @@ public SQSBatchResponse processBatch(SQSEvent event, Context context) {
7778
processBatchItem(message, context).ifPresent(batchItemFailure -> {
7879
response.getBatchItemFailures().add(batchItemFailure);
7980
if (messageGroupId != null) {
80-
failWholeBatch[0] = true;
81+
failWholeBatch.set(true);
8182
LOGGER.info(
8283
"A message in a batch with messageGroupId {} and messageId {} failed; failing the rest of the batch too"
8384
, messageGroupId, message.getMessageId());
8485
}
8586
});
8687
}
8788

88-
if (failWholeBatch[0]) {
89+
if (failWholeBatch.get()) {
8990
// Add the remaining messages to the batch item failures
9091
event.getRecords()
9192
.subList(messageCursor, event.getRecords().size())
@@ -106,6 +107,7 @@ public SQSBatchResponse processBatchInParallel(SQSEvent event, Context context)
106107
List<SQSBatchResponse.BatchItemFailure> batchItemFailures = event.getRecords()
107108
.parallelStream() // Parallel processing
108109
.map(sqsMessage -> {
110+
109111
multiThreadMDC.copyMDCToThread(Thread.currentThread().getName());
110112
return processBatchItem(sqsMessage, context);
111113
})

0 commit comments

Comments
 (0)