|
24 | 24 | using AWS.Lambda.Powertools.BatchProcessing.Kinesis; |
25 | 25 | using AWS.Lambda.Powertools.BatchProcessing.Sqs; |
26 | 26 | using AWS.Lambda.Powertools.Logging; |
| 27 | +using HelloWorld.Data; |
27 | 28 | using HelloWorld.DynamoDb; |
28 | 29 | using HelloWorld.Kinesis; |
29 | 30 | using HelloWorld.Sqs; |
@@ -77,8 +78,8 @@ public BatchItemFailuresResponse SqsHandlerUsingAttributeWithErrorPolicy(SQSEven |
77 | 78 | public BatchItemFailuresResponse HandlerUsingAttributeAndCustomRecordHandlerProvider(SQSEvent _) |
78 | 79 | { |
79 | 80 | return SqsBatchProcessor.Result.BatchItemFailuresResponse; |
80 | | - } |
81 | | - |
| 81 | + } |
| 82 | + |
82 | 83 | [Logging(LogEvent = true)] |
83 | 84 | [BatchProcessor(RecordHandler = typeof(CustomSqsRecordHandler), BatchProcessor = typeof(CustomSqsBatchProcessor))] |
84 | 85 | public BatchItemFailuresResponse HandlerUsingAttributeAndCustomBatchProcessor(SQSEvent _) |
@@ -114,4 +115,75 @@ public async Task<BatchItemFailuresResponse> HandlerUsingUtilityFromIoc(SQSEvent |
114 | 115 | } |
115 | 116 |
|
116 | 117 | #endregion |
| 118 | + |
| 119 | + #region Typed Batch Processing Handlers |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Example handler using typed batch processing for SQS. |
| 123 | + /// The TypedSqsBatchProcessor automatically deserializes the message body to the specified type. |
| 124 | + /// </summary> |
| 125 | + [Logging(LogEvent = true)] |
| 126 | + public async Task<BatchItemFailuresResponse> TypedSqsHandler(SQSEvent sqsEvent) |
| 127 | + { |
| 128 | + var handler = new TypedSqsProductHandler(); |
| 129 | + var result = await TypedSqsBatchProcessor.TypedInstance.ProcessAsync<Product>(sqsEvent, handler); |
| 130 | + return result.BatchItemFailuresResponse; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Example handler using typed batch processing for Kinesis. |
| 135 | + /// The TypedKinesisEventBatchProcessor automatically deserializes the record data to the specified type. |
| 136 | + /// </summary> |
| 137 | + [Logging(LogEvent = true)] |
| 138 | + public async Task<BatchItemFailuresResponse> TypedKinesisHandler(KinesisEvent kinesisEvent) |
| 139 | + { |
| 140 | + var handler = new TypedKinesisProductHandler(); |
| 141 | + var result = await TypedKinesisEventBatchProcessor.TypedInstance.ProcessAsync<Product>(kinesisEvent, handler); |
| 142 | + return result.BatchItemFailuresResponse; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Example handler using typed batch processing for DynamoDB Streams. |
| 147 | + /// The TypedDynamoDbStreamBatchProcessor automatically deserializes the stream record to the specified type. |
| 148 | + /// </summary> |
| 149 | + [Logging(LogEvent = true)] |
| 150 | + public async Task<BatchItemFailuresResponse> TypedDynamoDbHandler(DynamoDBEvent dynamoDbEvent) |
| 151 | + { |
| 152 | + var handler = new TypedDynamoDbProductHandler(); |
| 153 | + var result = await TypedDynamoDbStreamBatchProcessor.TypedInstance.ProcessAsync<DynamoDbProductStreamRecord>(dynamoDbEvent, handler); |
| 154 | + return result.BatchItemFailuresResponse; |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Example handler using typed batch processing with inline handler. |
| 159 | + /// Demonstrates using a lambda expression for simple processing logic. |
| 160 | + /// </summary> |
| 161 | + [Logging(LogEvent = true)] |
| 162 | + public async Task<BatchItemFailuresResponse> TypedSqsHandlerInline(SQSEvent sqsEvent) |
| 163 | + { |
| 164 | + var result = await TypedSqsBatchProcessor.TypedInstance.ProcessAsync<Product>( |
| 165 | + sqsEvent, |
| 166 | + new InlineTypedProductHandler()); |
| 167 | + return result.BatchItemFailuresResponse; |
| 168 | + } |
| 169 | + |
| 170 | + #endregion |
| 171 | +} |
| 172 | + |
| 173 | +/// <summary> |
| 174 | +/// Simple inline typed handler for demonstration purposes. |
| 175 | +/// </summary> |
| 176 | +public class InlineTypedProductHandler : ITypedRecordHandler<Product> |
| 177 | +{ |
| 178 | + public Task<RecordHandlerResult> HandleAsync(Product product, System.Threading.CancellationToken cancellationToken) |
| 179 | + { |
| 180 | + Logger.LogInformation($"[Inline Typed] Processing product: {product.Id} - {product.Name}"); |
| 181 | + |
| 182 | + if (product.Id == 4) |
| 183 | + { |
| 184 | + throw new System.ArgumentException("Error on id 4"); |
| 185 | + } |
| 186 | + |
| 187 | + return Task.FromResult(RecordHandlerResult.None); |
| 188 | + } |
117 | 189 | } |
0 commit comments