14
14
package software .amazon .lambda .powertools .sqs ;
15
15
16
16
import java .util .List ;
17
- import java .util .function .Consumer ;
18
17
import java .util .function .Function ;
19
18
import java .util .stream .Collectors ;
20
19
@@ -39,13 +38,29 @@ public class PowertoolsSqs {
39
38
/**
40
39
* This is a utility method when you want to avoid using {@code LargeMessageHandler} annotation.
41
40
* Gives you access to enriched messages from S3 in the SQS event produced via extended client lib.
41
+ * If all the large S3 payload are successfully retrieved, it will delete them from S3 post success.
42
42
*
43
43
* @param sqsEvent Event received from SQS Extended client library
44
44
* @param messageFunction Function to execute you business logic which provides access to enriched messages from S3 when needed.
45
- * @return Return value from the function.
45
+ * @return Return value from the function.
46
46
*/
47
47
public static <R > R enrichedMessageFromS3 (final SQSEvent sqsEvent ,
48
48
final Function <List <SQSMessage >, R > messageFunction ) {
49
+ return enrichedMessageFromS3 (sqsEvent , true , messageFunction );
50
+ }
51
+
52
+ /**
53
+ * This is a utility method when you want to avoid using {@code LargeMessageHandler} annotation.
54
+ * Gives you access to enriched messages from S3 in the SQS event produced via extended client lib.
55
+ * if all the large S3 payload are successfully retrieved, Control if it will delete payload from S3 post success.
56
+ *
57
+ * @param sqsEvent Event received from SQS Extended client library
58
+ * @param messageFunction Function to execute you business logic which provides access to enriched messages from S3 when needed.
59
+ * @return Return value from the function.
60
+ */
61
+ public static <R > R enrichedMessageFromS3 (final SQSEvent sqsEvent ,
62
+ final boolean deleteS3Payload ,
63
+ final Function <List <SQSMessage >, R > messageFunction ) {
49
64
50
65
List <SQSMessage > sqsMessages = sqsEvent .getRecords ().stream ()
51
66
.map (PowertoolsSqs ::clonedMessage )
@@ -55,7 +70,9 @@ public static <R> R enrichedMessageFromS3(final SQSEvent sqsEvent,
55
70
56
71
R returnValue = messageFunction .apply (sqsMessages );
57
72
58
- s3Pointers .forEach (SqsMessageAspect ::deleteMessage );
73
+ if (deleteS3Payload ) {
74
+ s3Pointers .forEach (SqsMessageAspect ::deleteMessage );
75
+ }
59
76
60
77
return returnValue ;
61
78
}
0 commit comments