-
Notifications
You must be signed in to change notification settings - Fork 12
/
MessageListeners.java
75 lines (69 loc) · 3.12 KB
/
MessageListeners.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
package com.jashmore.sqs.examples;
import com.jashmore.sqs.argument.payload.Payload;
import com.jashmore.sqs.spring.container.fifo.FifoQueueListener;
import com.jashmore.sqs.util.ExpectedTestException;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.sqs.model.Message;
import software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName;
@Slf4j
@Component
public class MessageListeners {
private static final int MINIMUM_PROCESSING_TIME_IN_MS = 500;
private static final int MAXIMUM_PROCESSING_TIME_IN_MS = 2000;
private final Map<String, Integer> messageGroups = new ConcurrentHashMap<>();
private final AtomicInteger totalMessagesProcessed = new AtomicInteger();
private final Set<String> currentGroupsProcessing = ConcurrentHashMap.newKeySet();
private final Random random = new Random();
@FifoQueueListener(
identifier = "fifo",
value = "test-queue.fifo",
concurrencyLevel = 10,
messageVisibilityTimeoutInSeconds = 60,
tryAndProcessAnyExtraRetrievedMessagesOnShutdown = true
)
public void fifoQueueListener(@Payload final String payload, final Message message) throws InterruptedException {
final int value = Integer.parseInt(payload);
final int processingTimeInMs =
MINIMUM_PROCESSING_TIME_IN_MS + random.nextInt(MAXIMUM_PROCESSING_TIME_IN_MS - MINIMUM_PROCESSING_TIME_IN_MS);
final String groupId = message.attributes().get(MessageSystemAttributeName.MESSAGE_GROUP_ID);
synchronized (this) {
if (currentGroupsProcessing.contains(groupId)) {
throw new RuntimeException("Error! Already processing a message with the same group ID: " + groupId);
}
final Integer previousValue = messageGroups.get(groupId);
if ((previousValue == null && value != 1) || (previousValue != null && previousValue != value - 1)) {
log.error(
"Error! Message with group ID is not processed in order: {}. Previous: {} Current: {}",
groupId,
previousValue,
value
);
throw new RuntimeException("Error! Incorrect order for group: " + groupId);
}
if (random.nextInt(10) < 1) {
throw new ExpectedTestException();
}
currentGroupsProcessing.add(groupId);
messageGroups.put(groupId, value);
log.info(
"Group: {} previous: {} value: {} concurrent: {} total {} in {}ms",
groupId,
previousValue,
value,
currentGroupsProcessing.size(),
totalMessagesProcessed.incrementAndGet(),
processingTimeInMs
);
}
Thread.sleep(processingTimeInMs);
synchronized (this) {
currentGroupsProcessing.remove(groupId);
}
}
}