-
Notifications
You must be signed in to change notification settings - Fork 98
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
Changes from 2 commits
6e7e643
f06e692
ab4b667
850c155
4d0cd06
c4361a9
5758c71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
Check failure on line 33 in examples/powertools-examples-batch/src/main/java/org/demo/batch/dynamo/DynamoDBStreamBatchHandlerParallel.java
|
||
LOGGER.info("Processing DynamoDB Stream Record" + dynamodbStreamRecord); | ||
} | ||
|
||
} |
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) { | ||
Check failure on line 35 in examples/powertools-examples-batch/src/main/java/org/demo/batch/kinesis/KinesisBatchHandlerParallel.java
|
||
|
||
LOGGER.info("Processing product " + p); | ||
} | ||
|
||
} |
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); | ||
} | ||
} |
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