diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java index b6b9b037d..57f4e8a06 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java @@ -13,7 +13,7 @@ package com.amazonaws.serverless.proxy; import com.amazonaws.serverless.exceptions.ContainerInitializationException; -import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler; +import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler; import com.amazonaws.services.lambda.runtime.Context; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.slf4j.Logger; @@ -26,10 +26,10 @@ /** * An async implementation of the InitializationWrapper interface. This initializer calls the - * {@link LambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda + * {@link InitializableLambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda * initialization time of 10 seconds, if the initialize method takes longer than 10 seconds to return, the - * {@link #start(LambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in - * the background. The {@link LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the + * {@link #start(InitializableLambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in + * the background. The {@link com.amazonaws.serverless.proxy.internal.LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the * initializer to be released. * * The constructor of this class expects an epoch long. This is meant to be as close as possible to the time the Lambda @@ -72,7 +72,7 @@ public AsyncInitializationWrapper() { } @Override - public void start(LambdaContainerHandler handler) throws ContainerInitializationException { + public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException { if(ASYNC_INIT_DISABLED){ log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE); super.start(handler); @@ -107,18 +107,18 @@ public long getActualStartTimeMs() { @Override public CountDownLatch getInitializationLatch() { - if(ASYNC_INIT_DISABLED){ + if (ASYNC_INIT_DISABLED){ return super.getInitializationLatch(); } return initializationLatch; } private static class AsyncInitializer implements Runnable { - private LambdaContainerHandler handler; + private final InitializableLambdaContainerHandler handler; private CountDownLatch initLatch; private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class); - AsyncInitializer(CountDownLatch latch, LambdaContainerHandler h) { + AsyncInitializer(CountDownLatch latch, InitializableLambdaContainerHandler h) { initLatch = latch; handler = h; } diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/InitializationWrapper.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/InitializationWrapper.java index f7c96f8f4..a261b6cc8 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/InitializationWrapper.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/InitializationWrapper.java @@ -13,13 +13,13 @@ package com.amazonaws.serverless.proxy; import com.amazonaws.serverless.exceptions.ContainerInitializationException; -import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler; +import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler; import java.util.concurrent.CountDownLatch; /** - * This class is in charge of initializing a {@link LambdaContainerHandler}. - * In most cases, this means calling the {@link LambdaContainerHandler#initialize()} method. Some implementations may + * This class is in charge of initializing a {@link InitializableLambdaContainerHandler}. + * In most cases, this means calling the {@link InitializableLambdaContainerHandler#initialize()} method. Some implementations may * require additional initialization steps, in this case implementations should provide their own * InitializationWrapper. This library includes an async implementation of this class * {@link AsyncInitializationWrapper} for frameworks that are likely to take longer than 10 seconds to start. @@ -31,7 +31,7 @@ public class InitializationWrapper { * @param handler The container handler to be initializer * @throws ContainerInitializationException If anything goes wrong during container initialization. */ - public void start(LambdaContainerHandler handler) throws ContainerInitializationException { + public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException { handler.initialize(); } diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/InitializableLambdaContainerHandler.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/InitializableLambdaContainerHandler.java new file mode 100644 index 000000000..daf4a69d9 --- /dev/null +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/InitializableLambdaContainerHandler.java @@ -0,0 +1,31 @@ +/* + * Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance + * with the License. A copy of the License is located at + * + * http://aws.amazon.com/apache2.0/ + * + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + */ +package com.amazonaws.serverless.proxy.internal; + +import com.amazonaws.serverless.exceptions.ContainerInitializationException; + +/** + * Interface to define initialization/ cold-start related methods. + * See also the documentation for + * + * AWS Lambda Execution Environments. + */ +public interface InitializableLambdaContainerHandler { + + /** + * This method is called on the first (cold) invocation + * + * @throws ContainerInitializationException in case initialization fails + */ + void initialize() throws ContainerInitializationException; +} diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java index f14251600..ff978456b 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java @@ -45,7 +45,8 @@ * @param The request type for the wrapped Java container * @param The response or response writer type for the wrapped Java container */ -public abstract class LambdaContainerHandler { +public abstract class LambdaContainerHandler + implements InitializableLambdaContainerHandler { //------------------------------------------------------------- // Constants