Skip to content

Commit 0a81cde

Browse files
committed
Performance improvement fixes for profile set and reload. Completing first two items in #142.
1 parent 5d82f78 commit 0a81cde

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/LambdaSpringApplicationInitializer.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.amazonaws.serverless.proxy.spring;
1414

15+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
1516
import com.amazonaws.serverless.proxy.internal.testutils.Timer;
1617

1718
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -66,7 +67,7 @@ public class LambdaSpringApplicationInitializer extends HttpServlet implements W
6667
private volatile boolean refreshContext = true;
6768
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
6869
private transient volatile List<ServletContextListener> contextListeners;
69-
private volatile ArrayList<String> springProfiles;
70+
private volatile List<String> springProfiles;
7071

7172
// Dynamically instantiated properties
7273
private volatile DispatcherServlet dispatcherServlet;
@@ -121,19 +122,19 @@ public DispatcherServlet getDispatcherServlet() {
121122
return dispatcherServlet;
122123
}
123124

124-
public List<String> getSpringProfiles() {
125-
return Collections.unmodifiableList(springProfiles);
126-
}
125+
public void setSpringProfiles(ServletContext ctx, String... springProfiles)
126+
throws ContainerInitializationException {
127+
this.springProfiles = Arrays.asList(springProfiles);
127128

128-
public void setSpringProfiles(List<String> springProfiles) {
129-
this.springProfiles = new ArrayList<>(springProfiles);
130-
applicationContext.stop();
129+
applicationContext.registerShutdownHook();
131130
applicationContext.close();
132-
applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0]));
133-
//applicationContext.start();
134-
applicationContext.refresh();
131+
contextListeners.clear();
132+
try {
133+
onStartup(ctx);
134+
} catch (ServletException e) {
135+
throw new ContainerInitializationException("Could not reload Spring context", e);
136+
}
135137

136-
//dispatcherServlet.refresh();
137138
}
138139

139140
@Override

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringBootLambdaContainerHandler.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse
7878
return newHandler;
7979
}
8080

81+
/**
82+
* Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and the given Spring profiles
83+
* @param springBootInitializer {@code SpringBootServletInitializer} class
84+
* @param profiles A list of Spring profiles to activate
85+
* @return An initialized instance of the `SpringLambdaContainerHandler`
86+
* @throws ContainerInitializationException If an error occurs while initializing the Spring framework
87+
*/
88+
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<? extends WebApplicationInitializer> springBootInitializer, String... profiles)
89+
throws ContainerInitializationException {
90+
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> newHandler = new SpringBootLambdaContainerHandler<>(
91+
AwsProxyRequest.class,
92+
AwsProxyResponse.class,
93+
new AwsProxyHttpServletRequestReader(),
94+
new AwsProxyHttpServletResponseWriter(),
95+
new AwsProxySecurityContextWriter(),
96+
new AwsProxyExceptionHandler(),
97+
springBootInitializer);
98+
newHandler.activateSpringProfiles(profiles);
99+
newHandler.initialize();
100+
return newHandler;
101+
}
102+
81103
/**
82104
* Creates a new container handler with the given reader and writer objects
83105
*
@@ -100,6 +122,7 @@ public SpringBootLambdaContainerHandler(Class<RequestType> requestTypeClass,
100122
super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
101123
Timer.start("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR");
102124
setServletContext(new SpringBootAwsServletContext());
125+
initialized = false;
103126
this.springBootInitializer = springBootInitializer;
104127
Timer.stop("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR");
105128
}

aws-serverless-java-container-spring/src/main/java/com/amazonaws/serverless/proxy/spring/SpringLambdaContainerHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ public void activateSpringProfiles(String... profiles) throws ContainerInitializ
160160
if (initializer == null) {
161161
throw new ContainerInitializationException(LambdaSpringApplicationInitializer.ERROR_NO_CONTEXT, null);
162162
}
163-
164-
initializer.setSpringProfiles(Arrays.asList(profiles));
163+
setServletContext(new AwsServletContext(this));
164+
initializer.setSpringProfiles(getServletContext(), profiles);
165165
}
166166

167167
@Override

aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringAwsProxyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public class SpringAwsProxyTest {
5555
@Autowired
5656
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
5757

58-
/*@Before
58+
@Before
5959
public void clearServletContextCache() {
6060
AwsServletContext.clearServletContextCache();
61-
}*/
61+
}
6262

6363
@Test
6464
public void controllerAdvice_invalidPath_returnAdvice() {

aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/profile/SpringProfileTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ public void profile_defaultProfile() throws Exception {
6262
public void profile_overrideProfile() throws Exception {
6363
AwsProxyRequest request = new AwsProxyRequestBuilder("/profile/spring-properties", "GET")
6464
.build();
65-
/*
66-
This change works
67-
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
68-
applicationContext.register(EchoSpringAppConfig.class);
69-
SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = new SpringLambdaContainerHandler<>(AwsProxyRequest.class,
70-
AwsProxyResponse.class,
71-
new AwsProxyHttpServletRequestReader(),
72-
new AwsProxyHttpServletResponseWriter(),
73-
new AwsProxySecurityContextWriter(),
74-
new AwsProxyExceptionHandler(), applicationContext);
75-
76-
handler.activateSpringProfiles("override");
77-
handler.initialize();*/
7865
SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = SpringLambdaContainerHandler.getAwsProxyHandler(EchoSpringAppConfig.class);
7966
handler.activateSpringProfiles("override");
8067
AwsProxyResponse output = handler.proxy(request, lambdaContext);

0 commit comments

Comments
 (0)