Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 2.24 KB

File metadata and controls

56 lines (39 loc) · 2.24 KB

Powertools for AWS Lambda (Java) - SQS Batch Processing Example

This project contains an example of Lambda function using the batch processing utilities module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the documentation.

The project contains two functions:

  • SqsMessageSender - Sends a set of messages to an SQS queue. This function is triggered every 5 minutes by an EventBridge schedule rule.
  • SqsPoller - Listens to the same queue, processing items off in batches

The poller intentionally fails intermittently processing messages to demonstrate the replay behaviour of the batch module:

SqsPoller.java [SqsPoller.java:43](src/main/java/org/demo/sqs/SqsPoller.java)
 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";
        }

Deploy the sample application

This sample is based on Serverless Application Model (SAM). To deploy it, check out the instructions for getting started with SAM in the examples directory

Test the application

As the test is pushing through a batch every 5 minutes, we can simply watch the logs to see the batches being processed:

 sam logs --tail --stack-name $MY_STACK    

As the handler intentionally introduces intermittent failures, we should expect to see error messages too!