|
| 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; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.function.Function; |
| 19 | + |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessor; |
| 24 | +import software.amazon.lambda.powertools.largemessages.internal.LargeMessageProcessorFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * Functional API for processing large messages without AspectJ. |
| 28 | + * <p> |
| 29 | + * 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. |
| 31 | + * <p> |
| 32 | + * {@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} |
| 34 | + * client libraries. |
| 35 | + * <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}. |
| 37 | + * <p> |
| 38 | + * <u>SQS Example</u>: |
| 39 | + * <pre> |
| 40 | + * public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> { |
| 41 | + * private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler; |
| 42 | + * |
| 43 | + * public SqsBatchHandler() { |
| 44 | + * handler = new BatchMessageHandlerBuilder() |
| 45 | + * .withSqsBatchHandler() |
| 46 | + * .buildWithRawMessageHandler(this::processMessage); |
| 47 | + * } |
| 48 | + * |
| 49 | + * @Override |
| 50 | + * public SQSBatchResponse handleRequest(SQSEvent sqsEvent, Context context) { |
| 51 | + * return handler.processBatch(sqsEvent, context); |
| 52 | + * } |
| 53 | + * |
| 54 | + * private void processMessage(SQSEvent.SQSMessage sqsMessage) { |
| 55 | + * LargeMessages.processLargeMessage(sqsMessage, this::handleProcessedMessage); |
| 56 | + * } |
| 57 | + * |
| 58 | + * private void handleProcessedMessage(SQSEvent.SQSMessage processedMessage) { |
| 59 | + * // processedMessage.getBody() will contain the content of the S3 Object |
| 60 | + * } |
| 61 | + * } |
| 62 | + * </pre> |
| 63 | + * <p> |
| 64 | + * To disable the deletion of S3 objects: |
| 65 | + * <pre> |
| 66 | + * LargeMessages.processLargeMessage(sqsMessage, this::handleProcessedMessage, false); |
| 67 | + * </pre> |
| 68 | + * <p> |
| 69 | + * For multi-argument methods, use a lambda to pass additional parameters: |
| 70 | + * <pre> |
| 71 | + * public void handleRequest(SQSEvent event, Context context) { |
| 72 | + * event.getRecords().forEach(message -> |
| 73 | + * LargeMessages.processLargeMessage(message, processedMsg -> processMessage(processedMsg, context)) |
| 74 | + * ); |
| 75 | + * } |
| 76 | + * |
| 77 | + * private void processMessage(SQSMessage processedMessage, Context context) { |
| 78 | + * // processedMessage.getBody() will contain the content of the S3 Object |
| 79 | + * } |
| 80 | + * </pre> |
| 81 | + * <p> |
| 82 | + * <b>Note 1</b>: The message object (SQSMessage or SNSRecord) is modified in-place to avoid duplicating |
| 83 | + * the large blob in memory. The message body will be replaced with the S3 object content. |
| 84 | + * <p> |
| 85 | + * <b>Note 2</b>: Retrieving payloads and deleting objects from S3 will increase the duration of the Lambda function. |
| 86 | + * <p> |
| 87 | + * <b>Note 3</b>: Make sure to configure your function with enough memory to be able to retrieve S3 objects. |
| 88 | + * |
| 89 | + * @see LargeMessage |
| 90 | + */ |
| 91 | +public final class LargeMessages { |
| 92 | + |
| 93 | + private static final Logger LOG = LoggerFactory.getLogger(LargeMessages.class); |
| 94 | + |
| 95 | + private LargeMessages() { |
| 96 | + // utility class |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Process a large message and execute the function with the processed message. |
| 101 | + * <p> |
| 102 | + * The S3 object will be deleted after processing (default behavior). |
| 103 | + * To disable S3 object deletion, use {@link #processLargeMessage(Object, Function, boolean)}. |
| 104 | + * <p> |
| 105 | + * Example usage: |
| 106 | + * <pre> |
| 107 | + * String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, this::handleMessage); |
| 108 | + * String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, processedMsg -> processOrder(processedMsg, orderId, amount)); |
| 109 | + * </pre> |
| 110 | + * |
| 111 | + * @param message the message to process (SQSMessage or SNSRecord) |
| 112 | + * @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 |
| 115 | + * @return the result of the function execution |
| 116 | + */ |
| 117 | + public static <T, R> R processLargeMessage(T message, Function<T, R> function) { |
| 118 | + return processLargeMessage(message, function, true); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Process a large message and execute the function with the processed message. |
| 123 | + * <p> |
| 124 | + * Example usage: |
| 125 | + * <pre> |
| 126 | + * String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, this::handleMessage, false); |
| 127 | + * String returnValueOfFunction = LargeMessages.processLargeMessage(sqsMessage, processedMsg -> processOrder(processedMsg, orderId, amount), false); |
| 128 | + * </pre> |
| 129 | + * |
| 130 | + * @param message the message to process (SQSMessage or SNSRecord) |
| 131 | + * @param function the function to execute with the processed message |
| 132 | + * @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 |
| 135 | + * @return the result of the function execution |
| 136 | + */ |
| 137 | + public static <T, R> R processLargeMessage(T message, Function<T, R> function, boolean deleteS3Object) { |
| 138 | + Optional<LargeMessageProcessor<?>> processor = LargeMessageProcessorFactory.get(message); |
| 139 | + |
| 140 | + if (!processor.isPresent()) { |
| 141 | + LOG.warn("Unsupported message type [{}], proceeding without large message processing", |
| 142 | + message.getClass()); |
| 143 | + return function.apply(message); |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + @SuppressWarnings("unchecked") |
| 148 | + LargeMessageProcessor<T> typedProcessor = (LargeMessageProcessor<T>) processor.get(); |
| 149 | + return typedProcessor.process(message, function::apply, deleteS3Object); |
| 150 | + } catch (RuntimeException e) { |
| 151 | + throw e; |
| 152 | + } catch (Throwable t) { |
| 153 | + throw new LargeMessageProcessingException("Failed to process large message", t); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments