-
Notifications
You must be signed in to change notification settings - Fork 92
feat: add support for batch execution in parallel with custom Executor #1900
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
phipag
merged 7 commits into
aws-powertools:main
from
visheshruparelia:parallel_execution_custom_executor
Jul 11, 2025
+425
−69
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e7e643
feat: add support for batch execution in parallel with custom Executor
visheshruparelia f06e692
Merge branch 'main' into parallel_execution_custom_executor
phipag ab4b667
Merge branch 'main' into parallel_execution_custom_executor
phipag 850c155
pmd_analyse fix
visheshruparelia 4d0cd06
add docs
visheshruparelia c4361a9
Merge remote-tracking branch 'parent/main' into parallel_execution_cu…
visheshruparelia 5758c71
Update docs/utilities/batch.md
visheshruparelia 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
37 changes: 37 additions & 0 deletions
37
...xamples-batch/src/main/java/org/demo/batch/dynamo/DynamoDBStreamBatchHandlerParallel.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,37 @@ | ||
package org.demo.batch.dynamo; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent; | ||
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class DynamoDBStreamBatchHandlerParallel implements RequestHandler<DynamodbEvent, StreamsEventResponse> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDBStreamBatchHandlerParallel.class); | ||
private final BatchMessageHandler<DynamodbEvent, StreamsEventResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
public DynamoDBStreamBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withDynamoDbBatchHandler() | ||
.buildWithRawMessageHandler(this::processMessage); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Override | ||
public StreamsEventResponse handleRequest(DynamodbEvent ddbEvent, Context context) { | ||
return handler.processBatchInParallel(ddbEvent, context, executor); | ||
} | ||
|
||
private void processMessage(DynamodbEvent.DynamodbStreamRecord dynamodbStreamRecord, Context context) { | ||
LOGGER.info("Processing DynamoDB Stream Record" + dynamodbStreamRecord); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ools-examples-batch/src/main/java/org/demo/batch/kinesis/KinesisBatchHandlerParallel.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,39 @@ | ||
package org.demo.batch.kinesis; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.KinesisEvent; | ||
import com.amazonaws.services.lambda.runtime.events.StreamsEventResponse; | ||
import org.demo.batch.model.Product; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class KinesisBatchHandlerParallel implements RequestHandler<KinesisEvent, StreamsEventResponse> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KinesisBatchHandlerParallel.class); | ||
private final BatchMessageHandler<KinesisEvent, StreamsEventResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
|
||
public KinesisBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withKinesisBatchHandler() | ||
.buildWithMessageHandler(this::processMessage, Product.class); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Override | ||
public StreamsEventResponse handleRequest(KinesisEvent kinesisEvent, Context context) { | ||
return handler.processBatchInParallel(kinesisEvent, context, executor); | ||
} | ||
|
||
private void processMessage(Product p, Context c) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove /**
* Builds a BatchMessageHandler that can be used to process batches, given
* a user-defined handler to process each item in the batch. This variant
* takes a function that consumes the deserialized body of the given message
* If deserialization fails, it will be treated as
* failure of the processing of that item in the batch.
* Note: If you don't need the Lambda context, use the variant of this function
* that does not require it.
*
* @param handler Processes the deserialized body of the message
* @return A BatchMessageHandler for processing the batch
*/
public <M> BatchMessageHandler<E, R> buildWithMessageHandler(Consumer<M> handler, Class<M> messageClass) {
return buildWithMessageHandler((f, c) -> handler.accept(f), messageClass);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
LOGGER.info("Processing product " + p); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...s/powertools-examples-batch/src/main/java/org/demo/batch/sqs/SqsBatchHandlerParallel.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,37 @@ | ||
package org.demo.batch.sqs; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.amazonaws.services.lambda.runtime.events.SQSBatchResponse; | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent; | ||
import org.demo.batch.model.Product; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.lambda.powertools.batch.BatchMessageHandlerBuilder; | ||
import software.amazon.lambda.powertools.batch.handler.BatchMessageHandler; | ||
import software.amazon.lambda.powertools.logging.Logging; | ||
import software.amazon.lambda.powertools.tracing.Tracing; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class SqsBatchHandlerParallel extends AbstractSqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SqsBatchHandlerParallel.class); | ||
private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler; | ||
private final ExecutorService executor; | ||
|
||
public SqsBatchHandlerParallel() { | ||
handler = new BatchMessageHandlerBuilder() | ||
.withSqsBatchHandler() | ||
.buildWithMessageHandler(this::processMessage, Product.class); | ||
executor = Executors.newFixedThreadPool(2); | ||
} | ||
|
||
@Logging | ||
@Tracing | ||
@Override | ||
public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) { | ||
LOGGER.info("Processing batch of {} messages", sqsEvent.getRecords().size()); | ||
return handler.processBatchInParallel(sqsEvent, context, executor); | ||
} | ||
} |
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
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
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.
You can remove
Context c
here please. There is an overload for a message handler without context inAbstractBatchMessageHandlerBuilder.java
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.
The older examples in the package follow the same pattern as the new one. Do you want those changed as well?
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.
Thanks, this would be awesome if you can update those as well. Ideally, we have no Sonar or pmd_analyze findings before merging the PR. With the exception of the code duplication finding.
Uh oh!
There was an error while loading. Please reload this page.
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.
850c155