Skip to content

Commit 58b296f

Browse files
committed
refactor: extract InitializableLambdaContainerHandler interface to be used by SpringDelegatingLambdaContainerHandler later on
1 parent c26036f commit 58b296f

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/AsyncInitializationWrapper.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package com.amazonaws.serverless.proxy;
1414

1515
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
16-
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
16+
import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler;
1717
import com.amazonaws.services.lambda.runtime.Context;
1818
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1919
import org.slf4j.Logger;
@@ -26,10 +26,10 @@
2626

2727
/**
2828
* An async implementation of the <code>InitializationWrapper</code> interface. This initializer calls the
29-
* {@link LambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda
29+
* {@link InitializableLambdaContainerHandler#initialize()} in a separate thread. Then uses a latch to wait for the maximum Lambda
3030
* initialization time of 10 seconds, if the <code>initialize</code> method takes longer than 10 seconds to return, the
31-
* {@link #start(LambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in
32-
* the background. The {@link LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the
31+
* {@link #start(InitializableLambdaContainerHandler)} returns control to the caller and lets the initialization thread continue in
32+
* the background. The {@link com.amazonaws.serverless.proxy.internal.LambdaContainerHandler#proxy(Object, Context)} automatically waits for the latch of the
3333
* initializer to be released.
3434
*
3535
* 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() {
7272
}
7373

7474
@Override
75-
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
75+
public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException {
7676
if(ASYNC_INIT_DISABLED){
7777
log.info("Async init disabled due to \"{}\" initialization", INITIALIZATION_TYPE);
7878
super.start(handler);
@@ -107,18 +107,18 @@ public long getActualStartTimeMs() {
107107

108108
@Override
109109
public CountDownLatch getInitializationLatch() {
110-
if(ASYNC_INIT_DISABLED){
110+
if (ASYNC_INIT_DISABLED){
111111
return super.getInitializationLatch();
112112
}
113113
return initializationLatch;
114114
}
115115

116116
private static class AsyncInitializer implements Runnable {
117-
private LambdaContainerHandler handler;
117+
private final InitializableLambdaContainerHandler handler;
118118
private CountDownLatch initLatch;
119119
private Logger log = LoggerFactory.getLogger(AsyncInitializationWrapper.class);
120120

121-
AsyncInitializer(CountDownLatch latch, LambdaContainerHandler h) {
121+
AsyncInitializer(CountDownLatch latch, InitializableLambdaContainerHandler h) {
122122
initLatch = latch;
123123
handler = h;
124124
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/InitializationWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
package com.amazonaws.serverless.proxy;
1414

1515
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
16-
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
16+
import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler;
1717

1818
import java.util.concurrent.CountDownLatch;
1919

2020
/**
21-
* This class is in charge of initializing a {@link LambdaContainerHandler}.
22-
* In most cases, this means calling the {@link LambdaContainerHandler#initialize()} method. Some implementations may
21+
* This class is in charge of initializing a {@link InitializableLambdaContainerHandler}.
22+
* In most cases, this means calling the {@link InitializableLambdaContainerHandler#initialize()} method. Some implementations may
2323
* require additional initialization steps, in this case implementations should provide their own
2424
* <code>InitializationWrapper</code>. This library includes an async implementation of this class
2525
* {@link AsyncInitializationWrapper} for frameworks that are likely to take longer than 10 seconds to start.
@@ -31,7 +31,7 @@ public class InitializationWrapper {
3131
* @param handler The container handler to be initializer
3232
* @throws ContainerInitializationException If anything goes wrong during container initialization.
3333
*/
34-
public void start(LambdaContainerHandler handler) throws ContainerInitializationException {
34+
public void start(InitializableLambdaContainerHandler handler) throws ContainerInitializationException {
3535
handler.initialize();
3636
}
3737

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0/
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
package com.amazonaws.serverless.proxy.internal;
14+
15+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
16+
17+
/**
18+
* Interface to define initialization/ cold-start related methods.
19+
* See also the documentation for
20+
* <a href="https://docs.aws.amazon.com/lambda/latest/operatorguide/execution-environments.html">
21+
* AWS Lambda Execution Environments</a>.
22+
*/
23+
public interface InitializableLambdaContainerHandler {
24+
25+
/**
26+
* This method is called on the first (cold) invocation
27+
*
28+
* @throws ContainerInitializationException in case initialization fails
29+
*/
30+
void initialize() throws ContainerInitializationException;
31+
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/LambdaContainerHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
* @param <ContainerRequestType> The request type for the wrapped Java container
4646
* @param <ContainerResponseType> The response or response writer type for the wrapped Java container
4747
*/
48-
public abstract class LambdaContainerHandler<RequestType, ResponseType, ContainerRequestType, ContainerResponseType> {
48+
public abstract class LambdaContainerHandler<RequestType, ResponseType, ContainerRequestType, ContainerResponseType>
49+
implements InitializableLambdaContainerHandler {
4950

5051
//-------------------------------------------------------------
5152
// Constants

0 commit comments

Comments
 (0)