-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathSqsPoller.java
61 lines (49 loc) · 2.39 KB
/
SqsPoller.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
package org.demo.sqs;
import java.util.Random;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.sqs.SqsBatch;
import software.amazon.lambda.powertools.sqs.SqsMessageHandler;
import software.amazon.lambda.powertools.sqs.SqsUtils;
import java.security.SecureRandom;
import static com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;
/**
* Handler for requests to Lambda function.
*/
public class SqsPoller implements RequestHandler<SQSEvent, String> {
Logger log = LogManager.getLogger(SqsPoller.class);
Random random = new SecureRandom();
static {
// https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/lambda-optimize-starttime.html
SqsUtils.overrideSqsClient(SqsClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.build());
}
@SqsBatch(value = BatchProcessor.class, nonRetryableExceptions = {IllegalArgumentException.class})
@Logging(logEvent = true)
public String handleRequest(final SQSEvent input, final Context context) {
return "Success";
}
private class BatchProcessor implements SqsMessageHandler<Object> {
@Override
public String process(SQSMessage message) {
log.info("Processing message with id {}", message.getMessageId());
int nextInt = random.nextInt(100);
if(nextInt <= 10) {
log.info("Randomly picked message with id {} as business validation failure.", message.getMessageId());
throw new IllegalArgumentException("Failed business validation. No point of retrying. Move me to DLQ." + message.getMessageId());
}
if(nextInt > 90) {
log.info("Randomly picked message with id {} as intermittent failure.", message.getMessageId());
throw new RuntimeException("Failed due to intermittent issue. Will be sent back for retry." + message.getMessageId());
}
return "Success";
}
}
}