-
Notifications
You must be signed in to change notification settings - Fork 90
feat: SQS Large message handling #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6892f45
first working version
msailes d714d73
Minor refactoring for java8 specific constructs
1e71bdc
Fix logs for deletion
e3edebe
Initial test cases to remove remote interaction
3ce9166
Fail hard when failed downloading any message from batch in event
8405b81
Fail hard when failed downloading any message from batch in event
d1b2369
Extract junit version as prop
4a04908
Remove unused log4j deps
dd176b3
Remove dependency of args on methods for reuse
987bf86
added java docs and a new feature to disable deletion of message payl…
msailes 190d592
Merge remote-tracking branch 'origin/large-sqs-messages' into large-s…
msailes 2ca2a42
refactored tests.
msailes f02356c
Remove unused test resources
a5f5687
rename
msailes 4b699f0
rename
msailes d694c57
intial docs page
msailes 3c2f694
updated docs page
msailes 5d54bf7
typo
msailes 26a7b0e
Merge branch 'master' into large-sqs-messages
8c18a75
Fix example app version for powertools
563f43a
Set env var for sdk to use during remote builds
9d15a23
Fix parent module version for sqs
9eab98b
documentation improvements.
msailes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
title: SQS Large Message Handling | ||
description: Utility | ||
--- | ||
|
||
The large message handling utility handles SQS messages which have had their payloads | ||
offloaded to S3 due to them being larger than the SQS maximum. | ||
|
||
The utility automatically retrieves messages which have been offloaded to S3 using the | ||
[amazon-sqs-java-extended-client-lib](https://github.com/awslabs/amazon-sqs-java-extended-client-lib) | ||
client library. Once the message payloads have been processed successful the | ||
utility can delete the message payloads from S3. | ||
|
||
This utility is compatible with versions *1.1.0+* of amazon-sqs-java-extended-client-lib.</p> | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>amazon-sqs-java-extended-client-lib</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
``` | ||
|
||
## Install | ||
|
||
To install this utility, add the following dependency to your project. | ||
|
||
```xml | ||
<dependency> | ||
<groupId>software.amazon.lambda</groupId> | ||
<artifactId>powertools-sqs</artifactId> | ||
<version>0.1.0-beta</version> | ||
</dependency> | ||
``` | ||
|
||
And configure the aspectj-maven-plugin to compile-time weave (CTW) the | ||
aws-lambda-powertools-java aspects into your project. You may already have this | ||
plugin in your pom. In that case add the depenedency to the `aspectLibraries` | ||
section. | ||
|
||
```xml | ||
<build> | ||
<plugins> | ||
... | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>aspectj-maven-plugin</artifactId> | ||
<version>1.11</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<complianceLevel>1.8</complianceLevel> | ||
<aspectLibraries> | ||
... | ||
<aspectLibrary> | ||
<groupId>software.amazon.lambda</groupId> | ||
<artifactId>powertools-sqs</artifactId> | ||
</aspectLibrary> | ||
... | ||
</aspectLibraries> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
... | ||
</plugins> | ||
</build> | ||
``` | ||
|
||
The annotation `@LargeMessageHandler` should be used with the handleRequest method of a class | ||
which implements `com.amazonaws.services.lambda.runtime.RequestHandler` with | ||
`com.amazonaws.services.lambda.runtime.events.SQSEvent` as the first parameter. | ||
|
||
```java | ||
public class SqsMessageHandler implements RequestHandler<SQSEvent, String> { | ||
|
||
@Override | ||
@LargeMessageHandler | ||
public String handleRequest(SQSEvent sqsEvent, Context context) { | ||
// process messages | ||
|
||
return "ok"; | ||
} | ||
``` | ||
|
||
`@LargeMessageHandler` creates a default S3 Client `AmazonS3 amazonS3 = AmazonS3ClientBuilder.defaultClient()`. | ||
When the Lambda function is invoked with an event from SQS, each record received | ||
in the SQSEvent will be checked to see if it's body contains a payload which has | ||
been offloaded to S3. If it does then `getObject(bucket, key)` will be called, | ||
and the payload retrieved. If there is an error during this process then the | ||
function will fail with a `FailedProcessingLargePayloadException` exception. | ||
|
||
If the request handler method returns without error then each payload will be | ||
deleted from S3 using `deleteObject(bucket, key)` | ||
|
||
To disable deletion of payloads setting the following annotation parameter: | ||
|
||
```java | ||
@LargeMessageHandler(deletePayloads=false) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>powertools-sqs</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<artifactId>powertools-parent</artifactId> | ||
<groupId>software.amazon.lambda</groupId> | ||
<version>0.1.0-beta</version> | ||
</parent> | ||
|
||
<name>AWS Lambda Powertools Java library SQS</name> | ||
<description> | ||
A suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier. | ||
</description> | ||
<url>https://aws.amazon.com/lambda/</url> | ||
<issueManagement> | ||
<system>GitHub Issues</system> | ||
<url>https://github.com/awslabs/aws-lambda-powertools-java/issues</url> | ||
</issueManagement> | ||
<scm> | ||
<url>https://github.com/awslabs/aws-lambda-powertools-java.git</url> | ||
</scm> | ||
<developers> | ||
<developer> | ||
<name>AWS Lambda Powertools team</name> | ||
<organization>Amazon Web Services</organization> | ||
<organizationUrl>https://aws.amazon.com/</organizationUrl> | ||
</developer> | ||
</developers> | ||
|
||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>ossrh</id> | ||
<url>https://aws.oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
</distributionManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>software.amazon.lambda</groupId> | ||
<artifactId>powertools-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-lambda-java-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-lambda-java-events</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>software.amazon.payloadoffloading</groupId> | ||
<artifactId>payloadoffloading-common</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjrt</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjweaver</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
68 changes: 68 additions & 0 deletions
68
powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/LargeMessageHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package software.amazon.lambda.powertools.sqs; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@code LargeMessageHandler} is used to signal that the annotated method | ||
* should be extended to handle large SQS messages which have been offloaded | ||
* to S3 | ||
* | ||
* <p>{@code LargeMessageHandler} automatically retrieves and deletes messages | ||
* which have been offloaded to S3 using the {@code amazon-sqs-java-extended-client-lib} | ||
* client library.</p> | ||
* | ||
* <p>This version of the {@code LargeMessageHandler} is compatible with version | ||
* 1.1.0+ of {@code amazon-sqs-java-extended-client-lib}.</p> | ||
* | ||
* <pre> | ||
* <dependency> | ||
* <groupId>com.amazonaws</groupId> | ||
* <artifactId>amazon-sqs-java-extended-client-lib</artifactId> | ||
* <version>1.1.0</version> | ||
* </dependency> | ||
* </pre> | ||
* | ||
* <p>{@code LargeMessageHandler} should be used with the handleRequest method of a class | ||
* which implements {@code com.amazonaws.services.lambda.runtime.RequestHandler} with | ||
* {@code com.amazonaws.services.lambda.runtime.events.SQSEvent} as the first parameter.</p> | ||
* | ||
* <pre> | ||
* public class SqsMessageHandler implements RequestHandler<SQSEvent, String> { | ||
* | ||
* {@literal @}Override | ||
* {@literal @}LargeMessageHandler | ||
* public String handleRequest(SQSEvent sqsEvent, Context context) { | ||
* | ||
* // process messages | ||
* | ||
* return "ok"; | ||
* } | ||
* | ||
* ... | ||
* </pre> | ||
* | ||
* <p>Using the default S3 Client {@code AmazonS3 amazonS3 = AmazonS3ClientBuilder.defaultClient();} | ||
* each record received in the SQSEvent {@code LargeMessageHandler} will checked | ||
* to see if it's body contains a payload which has been offloaded to S3. If it | ||
* does then {@code getObject(bucket, key)} will be called and the payload | ||
* retrieved.</p> | ||
* | ||
* <p><b>Note</b>: Retreiving payloads from S3 will increase the duration of the | ||
* Lambda function.</p> | ||
* | ||
* <p>If the request handler method returns then each payload will be deleted | ||
* from S3 using {@code deleteObject(bucket, key)}</p> | ||
* | ||
* <p>To disable deletion of payloads setting the following annotation parameter | ||
* {@code @LargeMessageHandler(deletePayloads=false)}</p> | ||
* | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface LargeMessageHandler { | ||
|
||
boolean deletePayloads() default true; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be mention a bit explicitly that if download of any message fails from S3 while enriching, entire batch will fail?