|
| 1 | +/* |
| 2 | + * Copyright 2024 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 org.demo.batch.sqs; |
| 16 | + |
| 17 | +import com.amazonaws.services.lambda.runtime.Context; |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Random; |
| 23 | +import org.demo.batch.model.Product; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 27 | +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; |
| 28 | +import software.amazon.awssdk.services.s3.S3Client; |
| 29 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 30 | +import software.amazon.lambda.powertools.tracing.Tracing; |
| 31 | +import software.amazon.lambda.powertools.tracing.TracingUtils; |
| 32 | + |
| 33 | +public class AbstractSqsBatchHandler { |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSqsBatchHandler.class); |
| 35 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 36 | + private final String bucket = System.getenv("BUCKET"); |
| 37 | + private final S3Client s3 = S3Client.builder().httpClient(UrlConnectionHttpClient.create()).build(); |
| 38 | + private final Random r = new Random(); |
| 39 | + |
| 40 | + /** |
| 41 | + * Simulate some processing (I/O + S3 put request) |
| 42 | + * @param p deserialized product |
| 43 | + * @param context Lambda context |
| 44 | + */ |
| 45 | + @Tracing |
| 46 | + protected void processMessage(Product p, Context context) { |
| 47 | + TracingUtils.putAnnotation("productId", p.getId()); |
| 48 | + LOGGER.info("Processing product {}", p); |
| 49 | + char c = (char)(r.nextInt(26) + 'a'); |
| 50 | + char[] chars = new char[1024 * 1000]; |
| 51 | + Arrays.fill(chars, c); |
| 52 | + p.setName(new String(chars)); |
| 53 | + try { |
| 54 | + File file = new File("/tmp/"+p.getId()+".json"); |
| 55 | + mapper.writeValue(file, p); |
| 56 | + s3.putObject( |
| 57 | + PutObjectRequest.builder().bucket(bucket).key(p.getId()+".json").build(), RequestBody.fromFile(file)); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments