Skip to content

Commit 44cf89b

Browse files
docs(examples): add producer tool and address PR feedback
- Add SQS producer tool in examples/large-messages/tools - Update App.java to use SLF4J and @logging - Update README with usage instructions - Remove unused dependencies in pom.xml
1 parent 0be4bd8 commit 44cf89b

File tree

5 files changed

+167
-21
lines changed

5 files changed

+167
-21
lines changed

examples/powertools-examples-large-messages/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Powertools for AWS Lambda (Java) - Large Messages Example
22

3-
This project contains an example of a Lambda function using the **Large Messages** module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.powertools.aws.dev/lambda-java/utilities/large_messages/).
3+
This project contains an example of a Lambda function using the **Large Messages** module of Powertools for AWS Lambda (Java). For more information on this module, please refer to the [documentation](https://docs.aws.amazon.com/powertools/java/latest/utilities/large_messages/).
44

55
The example demonstrates an SQS listener that processes messages using the `LargeMessages` functional utility. It handles the retrieval of large payloads offloaded to S3 automatically.
66

@@ -17,11 +17,27 @@ Since this function is triggered by an SQS Queue, you can test it by sending a m
1717
Run the following command (replacing `LargeMessageExample` with the name of your deployed stack):
1818
```bash
1919
aws cloudformation describe-stacks --stack-name LargeMessageExample --query "Stacks[0].Outputs[?OutputKey=='QueueURL'].OutputValue" --output text
20-
20+
```
2121
2. **Send a Test Message:**
2222
Note: To test the actual "Large Message" functionality (payload offloading), you would typically use the SQS Extended Client in a producer application. However, you can verify the Lambda trigger with a standard message:
2323
```bash
2424
aws sqs send-message --queue-url [YOUR_QUEUE_URL] --message-body '{"message": "Hello from CLI"}'
25-
25+
```
2626
3. **Verify Logs:**
27-
Go to AWS CloudWatch Logs and check the Log Group for your function. You should see the processed message logged by the application.
27+
Go to AWS CloudWatch Logs and check the Log Group for your function. You should see the processed message logged by the application.
28+
29+
### Run the Large Message Producer
30+
31+
To test the handling of large messages involving S3 offloading, verify you have the SQS Queue URL and the S3 Bucket name created by the stack.
32+
33+
1. **Build the producer tool:**
34+
```bash
35+
cd examples/powertools-examples-large-messages/tools
36+
mvn compile
37+
```
38+
39+
2. **Run the producer:**
40+
```bash
41+
mvn exec:java -Dexec.args="<SQS_QUEUE_URL> <S3_BUCKET_NAME>"
42+
```
43+
Replace `<SQS_QUEUE_URL>` and `<S3_BUCKET_NAME>` with the output values from the deployment.

examples/powertools-examples-large-messages/pom.xml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,13 @@
77
<version>2.8.0</version>
88
<name>Powertools for AWS Lambda (Java) - Examples - Large Messages</name>
99

10-
<properties>
11-
<maven.compiler.source>11</maven.compiler.source>
12-
<maven.compiler.target>11</maven.compiler.target>
13-
<log4j.version>2.21.1</log4j.version>
14-
<aspectj.version>1.9.21</aspectj.version>
15-
<amazon-sqs-java-extended-client-lib.version>2.1.1</amazon-sqs-java-extended-client-lib.version>
16-
</properties>
17-
1810
<dependencies>
1911
<dependency>
2012
<groupId>software.amazon.lambda</groupId>
2113
<artifactId>powertools-large-messages</artifactId>
2214
<version>${project.version}</version>
2315
</dependency>
2416

25-
<dependency>
26-
<groupId>com.amazonaws</groupId>
27-
<artifactId>amazon-sqs-java-extended-client-lib</artifactId>
28-
<version>${amazon-sqs-java-extended-client-lib.version}</version>
29-
</dependency>
30-
3117
<dependency>
3218
<groupId>com.amazonaws</groupId>
3319
<artifactId>aws-lambda-java-events</artifactId>
@@ -39,6 +25,7 @@
3925
<artifactId>powertools-logging-log4j</artifactId>
4026
<version>${project.version}</version>
4127
</dependency>
28+
4229
<dependency>
4330
<groupId>org.aspectj</groupId>
4431
<artifactId>aspectjrt</artifactId>

examples/powertools-examples-large-messages/src/main/java/helloworld/App.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@
1818
import com.amazonaws.services.lambda.runtime.RequestHandler;
1919
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
2020
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;
21-
import org.apache.logging.log4j.LogManager;
22-
import org.apache.logging.log4j.Logger;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2323
import software.amazon.lambda.powertools.largemessages.LargeMessages;
24+
import software.amazon.lambda.powertools.logging.Logging;
2425

2526
/**
2627
* Example handler showing how to use LargeMessageProcessor functionally.
2728
* This approach gives you more control than the @LargeMessage annotation.
2829
*/
2930
public final class App implements RequestHandler<SQSEvent, String> {
3031

31-
private static final Logger LOG = LogManager.getLogger(App.class);
32+
private static final Logger LOG = LoggerFactory.getLogger(App.class);
3233

34+
@Logging
3335
@Override
3436
public String handleRequest(final SQSEvent event, final Context context) {
3537
LOG.info("Received event with {} records", event.getRecords().size());
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>software.amazon.lambda.examples</groupId>
8+
<artifactId>powertools-examples-large-messages-tools</artifactId>
9+
<version>1.0.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<aws.sdk.version>2.29.0</aws.sdk.version>
15+
<sqs-extended.version>2.1.2</sqs-extended.version>
16+
</properties>
17+
18+
<dependencyManagement>
19+
<dependencies>
20+
<dependency>
21+
<groupId>software.amazon.awssdk</groupId>
22+
<artifactId>bom</artifactId>
23+
<version>${aws.sdk.version}</version>
24+
<type>pom</type>
25+
<scope>import</scope>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.amazonaws</groupId>
33+
<artifactId>amazon-sqs-java-extended-client-lib</artifactId>
34+
<version>${sqs-extended.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>software.amazon.awssdk</groupId>
38+
<artifactId>s3</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>software.amazon.awssdk</groupId>
42+
<artifactId>sqs</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-simple</artifactId>
47+
<version>2.0.16</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.codehaus.mojo</groupId>
55+
<artifactId>exec-maven-plugin</artifactId>
56+
<version>3.1.0</version>
57+
<configuration>
58+
<mainClass>software.amazon.lambda.powertools.largemessages.tools.LargeMessageProducer</mainClass>
59+
<classpathScope>compile</classpathScope>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
15+
package software.amazon.lambda.powertools.largemessages.tools;
16+
17+
import com.amazon.sqs.javamessaging.AmazonSQSExtendedClient;
18+
import com.amazon.sqs.javamessaging.ExtendedClientConfiguration;
19+
import software.amazon.awssdk.services.s3.S3Client;
20+
import software.amazon.awssdk.services.sqs.SqsClient;
21+
import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
22+
import software.amazon.awssdk.services.sqs.model.SendMessageResponse;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.util.Arrays;
27+
28+
public class LargeMessageProducer {
29+
private static final Logger LOG = LoggerFactory.getLogger(LargeMessageProducer.class);
30+
31+
public static void main(String[] args) {
32+
if (args.length < 2) {
33+
LOG.error("Usage: LargeMessageProducer <SQS_QUEUE_URL> <S3_BUCKET_NAME>");
34+
System.exit(1);
35+
}
36+
37+
String queueUrl = args[0];
38+
String bucketName = args[1];
39+
40+
LOG.info("Starting Large Message Producer...");
41+
LOG.info("Queue URL: {}", queueUrl);
42+
LOG.info("S3 Bucket: {}", bucketName);
43+
44+
// Initialize S3 and SQS clients
45+
S3Client s3Client = S3Client.create();
46+
SqsClient sqsClient = SqsClient.create();
47+
48+
// Configure Extended Client
49+
ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration()
50+
.withPayloadSupportEnabled(s3Client, bucketName);
51+
52+
SqsClient extendedSqsClient = new AmazonSQSExtendedClient(sqsClient, extendedClientConfig);
53+
54+
// Generate large payload (> 256KB). 300KB to be safe.
55+
String payload = generateLargePayload(300 * 1024);
56+
57+
try {
58+
SendMessageRequest request = SendMessageRequest.builder()
59+
.queueUrl(queueUrl)
60+
.messageBody(payload)
61+
.build();
62+
63+
SendMessageResponse response = extendedSqsClient.sendMessage(request);
64+
LOG.info("Message sent successfully. Message ID: {}", response.messageId());
65+
} catch (Exception e) {
66+
LOG.error("Failed to send message", e);
67+
System.exit(1);
68+
}
69+
}
70+
71+
private static String generateLargePayload(int sizeInBytes) {
72+
char[] chars = new char[sizeInBytes];
73+
Arrays.fill(chars, 'A');
74+
// create a simple JSON structure
75+
return String.format("{\"data\": \"%s\"}", new String(chars));
76+
}
77+
}

0 commit comments

Comments
 (0)