|
1 | 1 | ## Lambda Powertools for Java - SQS Batch Processing Example
|
2 | 2 |
|
3 |
| -Demos setup of SQS Batch processing via Powertools |
| 3 | +This project contains an example of Lambda function using the batch processing utilities module of Powertools for AWS Lambda (Java). |
| 4 | +For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda/java/utilities/batch/). |
| 5 | + |
| 6 | +The project contains two functions: |
| 7 | + |
| 8 | +* [SqsMessageSender](src/main/java/org/demo/sqs/SqsMessageSender.java) - Sends a set of messages to an SQS queue. |
| 9 | +This function is triggered every 5 minutes by an EventBridge schedule rule. |
| 10 | +* [SqsPoller](src/main/java/org/demo/sqs/SqsPoller.java) - Listens to the same queue, processing items off in batches |
| 11 | + |
| 12 | +The poller intentionally fails intermittently processing messages to demonstrate the replay behaviour of the batch |
| 13 | +module: |
| 14 | + |
| 15 | +<details> |
| 16 | +<summary> |
| 17 | +<b>SqsPoller.java</b> |
| 18 | +</summary> |
| 19 | +[SqsPoller.java:43](src/main/java/org/demo/sqs/SqsPoller.java) |
| 20 | + |
| 21 | +```java |
| 22 | + public String process(SQSMessage message) { |
| 23 | + log.info("Processing message with id {}", message.getMessageId()); |
| 24 | + |
| 25 | + int nextInt = random.nextInt(100); |
| 26 | + |
| 27 | + if(nextInt <= 10) { |
| 28 | + log.info("Randomly picked message with id {} as business validation failure.", message.getMessageId()); |
| 29 | + throw new IllegalArgumentException("Failed business validation. No point of retrying. Move me to DLQ." + message.getMessageId()); |
| 30 | + } |
| 31 | + |
| 32 | + if(nextInt > 90) { |
| 33 | + log.info("Randomly picked message with id {} as intermittent failure.", message.getMessageId()); |
| 34 | + throw new RuntimeException("Failed due to intermittent issue. Will be sent back for retry." + message.getMessageId()); |
| 35 | + } |
| 36 | + |
| 37 | + return "Success"; |
| 38 | + } |
| 39 | +``` |
| 40 | + |
| 41 | +</details> |
| 42 | + |
| 43 | +## Deploy the sample application |
| 44 | + |
| 45 | +This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting |
| 46 | +started with SAM in [the examples directory](../README.md) |
| 47 | + |
| 48 | +## Test the application |
| 49 | + |
| 50 | +As the test is pushing through a batch every 5 minutes, we can simply watch the logs to see the batches being processed: |
| 51 | + |
| 52 | +```bash |
| 53 | + sam logs --tail --stack-name $MY_STACK |
| 54 | +``` |
| 55 | + |
| 56 | +As the handler intentionally introduces intermittent failures, we should expect to see error messages too! |
0 commit comments