Refactor CustomRuntimeEventLoop in AWS module to ensure it is spring aot native compatible

This commit is contained in:
Oleg Zhurakousky
2021-09-21 16:08:09 +02:00
parent 48c8a6f925
commit c8f146a98a
4 changed files with 17 additions and 36 deletions

View File

@@ -28,9 +28,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
@@ -48,7 +50,7 @@ import org.springframework.web.client.RestTemplate;
* @since 3.1.1
*
*/
final class CustomRuntimeEventLoop {
public final class CustomRuntimeEventLoop implements CommandLineRunner {
private static Log logger = LogFactory.getLog(CustomRuntimeEventLoop.class);
@@ -56,11 +58,19 @@ final class CustomRuntimeEventLoop {
private static final String LAMBDA_RUNTIME_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/next";
private static final String LAMBDA_INVOCATION_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/{2}/response";
private CustomRuntimeEventLoop() {
private final ConfigurableApplicationContext applicationContext;
public CustomRuntimeEventLoop(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void run(String... args) throws Exception {
CustomRuntimeEventLoop.eventLoop(this.applicationContext, args);
}
@SuppressWarnings("unchecked")
static void eventLoop(ApplicationContext context) {
private static void eventLoop(ApplicationContext context, String... args) {
logger.info("Starting spring-cloud-function CustomRuntimeEventLoop");
if (logger.isDebugEnabled()) {
logger.debug("AWS LAMBDA ENVIRONMENT: " + System.getenv());

View File

@@ -44,7 +44,7 @@ public class CustomRuntimeInitializer implements ApplicationContextInitializer<G
if (!this.isWebExportEnabled(context) && isCustomRuntime()) {
if (context.getBeanFactory().getBeanNamesForType(CustomRuntimeEventLoop.class, false, false).length == 0) {
context.registerBean(StringUtils.uncapitalize(CustomRuntimeEventLoop.class.getSimpleName()),
CommandLineRunner.class, () -> args -> CustomRuntimeEventLoop.eventLoop(context));
CommandLineRunner.class, () -> new CustomRuntimeEventLoop(context));
}
}
else if (ContextFunctionCatalogInitializer.enabled

View File

@@ -1,5 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.adapter.aws.CustomRuntimeEventLoop
org.springframework.context.ApplicationContextInitializer=\
org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer
org.springframework.boot.env.EnvironmentPostProcessor=\

View File

@@ -24,8 +24,6 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -85,6 +83,8 @@ public class CustomRuntimeEventLoopTest {
@DirtiesContext
public void testDefaultFunctionLookup() throws Exception {
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
this.getEnvironment().put("_HANDLER", "uppercase");
configuration.inputQueue.clear();
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
@@ -93,13 +93,6 @@ public class CustomRuntimeEventLoopTest {
.web(WebApplicationType.NONE).run(
"--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true")) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
CustomRuntimeEventLoop.eventLoop(userContext);
});
executor.shutdown();
assertThat(executor.awaitTermination(2000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(configuration.output).size().isEqualTo(3);
assertThat(configuration.output.get(0)).isEqualTo("\"RICKY\"");
@@ -112,6 +105,7 @@ public class CustomRuntimeEventLoopTest {
@DirtiesContext
public void testDefaultFunctionAsComponentLookup() throws Exception {
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
this.getEnvironment().put("_HANDLER", "personFunction");
configuration.inputQueue.clear();
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
@@ -120,13 +114,6 @@ public class CustomRuntimeEventLoopTest {
.web(WebApplicationType.NONE).run(
"--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true")) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
CustomRuntimeEventLoop.eventLoop(userContext);
});
executor.shutdown();
assertThat(executor.awaitTermination(2000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(configuration.output).size().isEqualTo(3);
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
@@ -148,13 +135,6 @@ public class CustomRuntimeEventLoopTest {
.web(WebApplicationType.NONE).run(
"--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true")) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
CustomRuntimeEventLoop.eventLoop(userContext);
});
executor.shutdown();
assertThat(executor.awaitTermination(2000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(configuration.output).size().isEqualTo(3);
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
@@ -176,13 +156,6 @@ public class CustomRuntimeEventLoopTest {
.web(WebApplicationType.NONE).run(
"--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.main.lazy-initialization=true")) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
CustomRuntimeEventLoop.eventLoop(userContext);
});
executor.shutdown();
assertThat(executor.awaitTermination(2000, TimeUnit.MILLISECONDS)).isTrue();
assertThat(configuration.output).size().isEqualTo(3);
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");