Skip to content

Commit 9e2c4ab

Browse files
committed
Added CRaC to Powertools Large Messages module and modified protobuf version examples
1 parent 5ed540a commit 9e2c4ab

7 files changed

Lines changed: 8043 additions & 27 deletions

File tree

examples/powertools-examples-kafka/src/main/java/org/demo/kafka/protobuf/ProtobufProduct.java

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/powertools-examples-kafka/src/main/java/org/demo/kafka/protobuf/ProtobufProductOrBuilder.java

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/powertools-examples-kafka/src/main/java/org/demo/kafka/protobuf/ProtobufProductOuterClass.java

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

powertools-large-messages/pom.xml

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
-->
1515

1616
<project xmlns="http://maven.apache.org/POM/4.0.0"
17-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1919
<modelVersion>4.0.0</modelVersion>
2020

21-
<description>A suite of utilities for AWS Lambda Functions that makes handling large messages in SQS and SNS easier.</description>
21+
<description>A suite of utilities for AWS Lambda Functions that makes handling large messages in SQS and SNS
22+
easier.
23+
</description>
2224

2325
<parent>
2426
<groupId>software.amazon.lambda</groupId>
@@ -87,6 +89,10 @@
8789
<artifactId>url-connection-client</artifactId>
8890
<version>${aws.sdk.version}</version>
8991
</dependency>
92+
<dependency>
93+
<groupId>org.crac</groupId>
94+
<artifactId>crac</artifactId>
95+
</dependency>
9096

9197
<!-- Test dependencies -->
9298
<dependency>
@@ -141,6 +147,27 @@
141147
</dependency>
142148
</dependencies>
143149

150+
<profiles>
151+
<profile>
152+
<id>generate-classesloaded-file</id>
153+
<build>
154+
<plugins>
155+
<plugin>
156+
<groupId>org.apache.maven.plugins</groupId>
157+
<artifactId>maven-surefire-plugin</artifactId>
158+
<configuration>
159+
<argLine>
160+
-Xlog:class+load=info:classesloaded.txt
161+
--add-opens java.base/java.util=ALL-UNNAMED
162+
--add-opens java.base/java.lang=ALL-UNNAMED
163+
</argLine>
164+
</configuration>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
</profile>
169+
</profiles>
170+
144171
<build>
145172
<plugins>
146173
<plugin>

powertools-large-messages/src/main/java/software/amazon/lambda/powertools/largemessages/LargeMessageConfig.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
import static software.amazon.lambda.powertools.common.internal.LambdaConstants.AWS_REGION_ENV;
1818

19+
import org.crac.Context;
20+
import org.crac.Core;
21+
import org.crac.Resource;
1922
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
2023
import software.amazon.awssdk.regions.Region;
2124
import software.amazon.awssdk.services.s3.S3Client;
2225
import software.amazon.awssdk.services.s3.S3ClientBuilder;
2326

2427
/**
25-
* Singleton instance for Large Message Config. We need this to provide a way to customize the S3 client configuration used by the annotation.
28+
* Singleton instance for Large Message Config. We need this to provide a way to customize the S3 client
29+
* configuration used by the annotation.
2630
* <br/>
27-
* Optional: Use it in your Lambda constructor to pass a custom {@link S3Client} to the {@link software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessor}
31+
* Optional: Use it in your Lambda constructor to pass a custom {@link S3Client} to the
32+
* {@link software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessor}
2833
* <br/>
2934
* If you don't use this, a default S3Client will be created.
3035
* <pre>
@@ -33,12 +38,13 @@
3338
* }
3439
* </pre>
3540
*/
36-
public class LargeMessageConfig {
41+
public class LargeMessageConfig implements Resource {
3742

3843
private static final LargeMessageConfig INSTANCE = new LargeMessageConfig();
3944
private S3Client s3Client;
4045

4146
private LargeMessageConfig() {
47+
Core.getGlobalContext().register(this);
4248
}
4349

4450
/**
@@ -80,4 +86,14 @@ public S3Client getS3Client() {
8086
}
8187
return this.s3Client;
8288
}
89+
90+
@Override
91+
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
92+
getS3Client();
93+
}
94+
95+
@Override
96+
public void afterRestore(Context<? extends Resource> context) throws Exception {
97+
// No action needed after restore
98+
}
8399
}

powertools-large-messages/src/main/java/software/amazon/lambda/powertools/largemessages/LargeMessages.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,30 @@
1717
import java.util.Optional;
1818
import java.util.function.Function;
1919

20+
import org.crac.Context;
21+
import org.crac.Core;
22+
import org.crac.Resource;
2023
import org.slf4j.Logger;
2124
import org.slf4j.LoggerFactory;
2225

26+
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
2327
import software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessor;
2428
import software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessorFactory;
2529

2630
/**
2731
* Functional API for processing large messages without AspectJ.
2832
* <p>
2933
* Use this class to handle large messages (> 1 MB) from SQS or SNS.
30-
* When large messages are sent to an SQS Queue or SNS Topic, they are offloaded to S3 and only a reference is passed in the message/record.
34+
* When large messages are sent to an SQS Queue or SNS Topic, they are offloaded to S3 and only a reference is passed
35+
* in the message/record.
3136
* <p>
3237
* {@code LargeMessages} automatically retrieves and optionally deletes messages
33-
* which have been offloaded to S3 using the {@code amazon-sqs-java-extended-client-lib} or {@code amazon-sns-java-extended-client-lib}
38+
* which have been offloaded to S3 using the {@code amazon-sqs-java-extended-client-lib} or {@code amazon-sns-java
39+
* -extended-client-lib}
3440
* client libraries.
3541
* <p>
36-
* This version is compatible with version 1.1.0+ and 2.0.0+ of {@code amazon-sqs-java-extended-client-lib} / {@code amazon-sns-java-extended-client-lib}.
42+
* This version is compatible with version 1.1.0+ and 2.0.0+ of {@code amazon-sqs-java-extended-client-lib} / {@code
43+
* amazon-sns-java-extended-client-lib}.
3744
* <p>
3845
* <u>SQS Example</u>:
3946
* <pre>
@@ -88,12 +95,16 @@
8895
*
8996
* @see LargeMessage
9097
*/
91-
public final class LargeMessages {
98+
public final class LargeMessages implements Resource {
9299

93100
private static final Logger LOG = LoggerFactory.getLogger(LargeMessages.class);
94101

95-
private LargeMessages() {
96-
// Utility class
102+
// Dummy instance to register LargeMessages with CRaC
103+
private static final LargeMessages INSTANCE = new LargeMessages();
104+
105+
// Static block to ensure CRaC registration happens at class loading time
106+
static {
107+
Core.getGlobalContext().register(INSTANCE);
97108
}
98109

99110
/**
@@ -108,10 +119,10 @@ private LargeMessages() {
108119
* String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, processedMsg -&gt; processOrder(processedMsg, orderId, amount));
109120
* </pre>
110121
*
111-
* @param message the message to process (SQSMessage or SNSRecord)
122+
* @param message the message to process (SQSMessage or SNSRecord)
112123
* @param function the function to execute with the processed message
113-
* @param <T> the message type
114-
* @param <R> the return type of the function
124+
* @param <T> the message type
125+
* @param <R> the return type of the function
115126
* @return the result of the function execution
116127
*/
117128
public static <T, R> R processLargeMessage(T message, Function<T, R> function) {
@@ -127,11 +138,11 @@ public static <T, R> R processLargeMessage(T message, Function<T, R> function) {
127138
* String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, processedMsg -&gt; processOrder(processedMsg, orderId, amount), false);
128139
* </pre>
129140
*
130-
* @param message the message to process (SQSMessage or SNSRecord)
131-
* @param function the function to execute with the processed message
141+
* @param message the message to process (SQSMessage or SNSRecord)
142+
* @param function the function to execute with the processed message
132143
* @param deleteS3Object whether to delete the S3 object after processing
133-
* @param <T> the message type
134-
* @param <R> the return type of the function
144+
* @param <T> the message type
145+
* @param <R> the return type of the function
135146
* @return the result of the function execution
136147
*/
137148
public static <T, R> R processLargeMessage(T message, Function<T, R> function, boolean deleteS3Object) {
@@ -153,4 +164,14 @@ public static <T, R> R processLargeMessage(T message, Function<T, R> function, b
153164
throw new LargeMessageProcessingException("Failed to process large message", t);
154165
}
155166
}
167+
168+
@Override
169+
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
170+
ClassPreLoader.preloadClasses();
171+
}
172+
173+
@Override
174+
public void afterRestore(Context<? extends Resource> context) throws Exception {
175+
// No action needed after restore
176+
}
156177
}

0 commit comments

Comments
 (0)