Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reuse AsyncInitializationWrapper in SpringDelegatingLambdaContainerHandler #883

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.io.InputStream;
import java.io.OutputStream;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.AsyncInitializationWrapper;
import com.amazonaws.serverless.proxy.InitializationTypeHelper;
import com.amazonaws.serverless.proxy.internal.InitializableLambdaContainerHandler;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import org.springframework.cloud.function.serverless.web.FunctionClassUtils;
import org.springframework.cloud.function.serverless.web.ServerlessMVC;
Expand Down Expand Up @@ -38,25 +41,23 @@
*
*/
public class SpringDelegatingLambdaContainerHandler implements RequestStreamHandler {

private final Class<?>[] startupClasses;

private final ServerlessMVC mvc;

private final ObjectMapper mapper;

private final AwsProxyHttpServletResponseWriter responseWriter;

public SpringDelegatingLambdaContainerHandler() {
public SpringDelegatingLambdaContainerHandler() throws ContainerInitializationException {
this(new Class[] {FunctionClassUtils.getStartClass()});
}

public SpringDelegatingLambdaContainerHandler(Class<?>... startupClasses) {
this.startupClasses = startupClasses;
this.mvc = ServerlessMVC.INSTANCE(this.startupClasses);
public SpringDelegatingLambdaContainerHandler(final Class<?>... startupClasses) throws ContainerInitializationException {
SpringDelegatingInitHandler initHandler = new SpringDelegatingInitHandler(startupClasses);
if (InitializationTypeHelper.isAsyncInitializationDisabled()) {
mvc.waitForContext();
}
initHandler.initialize();
} else {
AsyncInitializationWrapper asyncInitWrapper = new AsyncInitializationWrapper();
asyncInitWrapper.start(initHandler);
}
this.mvc = initHandler.getMvc();
this.mapper = new ObjectMapper();
this.responseWriter = new AwsProxyHttpServletResponseWriter();
}
Expand All @@ -68,4 +69,23 @@ public void handleRequest(InputStream input, OutputStream output, Context lambda
AwsProxyResponse awsProxyResponse = AwsSpringHttpProcessingUtils.processRequest(httpServletRequest, mvc, responseWriter);
this.mapper.writeValue(output, awsProxyResponse);
}

private static final class SpringDelegatingInitHandler implements InitializableLambdaContainerHandler {
private ServerlessMVC mvc;
private final Class<?>[] startupClasses;

public SpringDelegatingInitHandler(final Class<?>... startupClasses) {
this.startupClasses = startupClasses;
}

@Override
public void initialize() throws ContainerInitializationException {
this.mvc = ServerlessMVC.INSTANCE(this.startupClasses);
this.mvc.waitForContext();
}

public ServerlessMVC getMvc() {
return this.mvc;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.cloud.function.serverless.web.ServerlessServletContext;
Expand Down Expand Up @@ -200,7 +201,7 @@ public class SpringDelegatingLambdaContainerHandlerTests {

private ObjectMapper mapper = new ObjectMapper();

public void initServletAppTest() {
public void initServletAppTest() throws ContainerInitializationException {
this.handler = new SpringDelegatingLambdaContainerHandler(ServletApplication.class);
}

Expand Down
Loading