Skip to content

Feature request: Support functional interface for Large Messages utility #2212

@phipag

Description

@phipag

Use case

The Large Messages utility currently only offers a benefit to developers when used with the AspectJ @LargeMessage annotation. The purpose of this feature request is to add a functional interface to initialize the Large message utility independent of AspectJ. For example, by wrapping it in a higher order function.

Solution/User Experience

The proposed design is below. We need ensure thread-safety here as well especially because this can be used with the parallel batch processor!

Usage with AspectJ

public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
    private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler;

    public SqsBatchHandler() {
        handler = new BatchMessageHandlerBuilder()
                .withSqsBatchHandler()
                .buildWithRawMessageHandler(this::processMessage);
    }

    @Override
    public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) {
        return handler.processBatch(sqsEvent, context);
    }

    @LargeMessage
    private void processMessage(SQSEvent.SQSMessage sqsMessage) {
        // do something with the message
    }
}

Usage without AspectJ

public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
    private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler;

    public SqsBatchHandler() {
        handler = new BatchMessageHandlerBuilder()
                .withSqsBatchHandler()
                .buildWithRawMessageHandler(this::processMessage);
    }

    @Override
    public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) {
        return handler.processBatch(sqsEvent, context);
    }

    private void processMessage(SQSEvent.SQSMessage sqsMessage) {
        PowertoolsLargeMessages.processLargeMessage(sqsMessage, this::handleProcessedMessage);
    }

    private void handleProcessedMessage(SQSEvent.SQSMessage processedMessage) {
        // do something with the message - S3 content already retrieved
    }
}

We can pass directly a Lambda function to the processRawMessage method as well:

public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
    private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler;

    public SqsBatchHandler() {
        handler = new BatchMessageHandlerBuilder()
                .withSqsBatchHandler()
                .buildWithRawMessageHandler(message -> 
                    PowertoolsLargeMessages.processLargeMessage(message, this::handleProcessedMessage)
                );
    }

    @Override
    public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) {
        return handler.processBatch(sqsEvent, context);
    }

    private void handleProcessedMessage(SQSEvent.SQSMessage processedMessage) {
        // do something with the message - S3 content already retrieved
    }
}

Alternative solutions

N/A

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions