Refactor CustomRuntimeEventLoop in AWS module to ensure it is spring aot native compatible
This commit is contained in:
@@ -28,9 +28,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.cloud.function.context.FunctionCatalog;
|
import org.springframework.cloud.function.context.FunctionCatalog;
|
||||||
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
@@ -48,7 +50,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
* @since 3.1.1
|
* @since 3.1.1
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
final class CustomRuntimeEventLoop {
|
public final class CustomRuntimeEventLoop implements CommandLineRunner {
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(CustomRuntimeEventLoop.class);
|
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_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 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")
|
@SuppressWarnings("unchecked")
|
||||||
static void eventLoop(ApplicationContext context) {
|
private static void eventLoop(ApplicationContext context, String... args) {
|
||||||
logger.info("Starting spring-cloud-function CustomRuntimeEventLoop");
|
logger.info("Starting spring-cloud-function CustomRuntimeEventLoop");
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("AWS LAMBDA ENVIRONMENT: " + System.getenv());
|
logger.debug("AWS LAMBDA ENVIRONMENT: " + System.getenv());
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class CustomRuntimeInitializer implements ApplicationContextInitializer<G
|
|||||||
if (!this.isWebExportEnabled(context) && isCustomRuntime()) {
|
if (!this.isWebExportEnabled(context) && isCustomRuntime()) {
|
||||||
if (context.getBeanFactory().getBeanNamesForType(CustomRuntimeEventLoop.class, false, false).length == 0) {
|
if (context.getBeanFactory().getBeanNamesForType(CustomRuntimeEventLoop.class, false, false).length == 0) {
|
||||||
context.registerBean(StringUtils.uncapitalize(CustomRuntimeEventLoop.class.getSimpleName()),
|
context.registerBean(StringUtils.uncapitalize(CustomRuntimeEventLoop.class.getSimpleName()),
|
||||||
CommandLineRunner.class, () -> args -> CustomRuntimeEventLoop.eventLoop(context));
|
CommandLineRunner.class, () -> new CustomRuntimeEventLoop(context));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (ContextFunctionCatalogInitializer.enabled
|
else if (ContextFunctionCatalogInitializer.enabled
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
org.springframework.cloud.function.adapter.aws.CustomRuntimeEventLoop
|
|
||||||
org.springframework.context.ApplicationContextInitializer=\
|
org.springframework.context.ApplicationContextInitializer=\
|
||||||
org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer
|
org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer
|
||||||
org.springframework.boot.env.EnvironmentPostProcessor=\
|
org.springframework.boot.env.EnvironmentPostProcessor=\
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ArrayBlockingQueue;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -85,6 +83,8 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public void testDefaultFunctionLookup() throws Exception {
|
public void testDefaultFunctionLookup() throws Exception {
|
||||||
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
|
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
|
||||||
|
this.getEnvironment().put("_HANDLER", "uppercase");
|
||||||
|
|
||||||
|
|
||||||
configuration.inputQueue.clear();
|
configuration.inputQueue.clear();
|
||||||
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
|
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
|
||||||
@@ -93,13 +93,6 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
.web(WebApplicationType.NONE).run(
|
.web(WebApplicationType.NONE).run(
|
||||||
"--logging.level.org.springframework.cloud.function=DEBUG",
|
"--logging.level.org.springframework.cloud.function=DEBUG",
|
||||||
"--spring.main.lazy-initialization=true")) {
|
"--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).size().isEqualTo(3);
|
||||||
assertThat(configuration.output.get(0)).isEqualTo("\"RICKY\"");
|
assertThat(configuration.output.get(0)).isEqualTo("\"RICKY\"");
|
||||||
@@ -112,6 +105,7 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public void testDefaultFunctionAsComponentLookup() throws Exception {
|
public void testDefaultFunctionAsComponentLookup() throws Exception {
|
||||||
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
|
this.getEnvironment().put("AWS_LAMBDA_RUNTIME_API", "localhost:" + port);
|
||||||
|
this.getEnvironment().put("_HANDLER", "personFunction");
|
||||||
|
|
||||||
configuration.inputQueue.clear();
|
configuration.inputQueue.clear();
|
||||||
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
|
configuration.inputQueue.addAll(Arrays.asList("\"ricky\"", "\"julien\"", "\"bubbles\""));
|
||||||
@@ -120,13 +114,6 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
.web(WebApplicationType.NONE).run(
|
.web(WebApplicationType.NONE).run(
|
||||||
"--logging.level.org.springframework.cloud.function=DEBUG",
|
"--logging.level.org.springframework.cloud.function=DEBUG",
|
||||||
"--spring.main.lazy-initialization=true")) {
|
"--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).size().isEqualTo(3);
|
||||||
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
||||||
@@ -148,13 +135,6 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
.web(WebApplicationType.NONE).run(
|
.web(WebApplicationType.NONE).run(
|
||||||
"--logging.level.org.springframework.cloud.function=DEBUG",
|
"--logging.level.org.springframework.cloud.function=DEBUG",
|
||||||
"--spring.main.lazy-initialization=true")) {
|
"--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).size().isEqualTo(3);
|
||||||
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
||||||
@@ -176,13 +156,6 @@ public class CustomRuntimeEventLoopTest {
|
|||||||
.web(WebApplicationType.NONE).run(
|
.web(WebApplicationType.NONE).run(
|
||||||
"--logging.level.org.springframework.cloud.function=DEBUG",
|
"--logging.level.org.springframework.cloud.function=DEBUG",
|
||||||
"--spring.main.lazy-initialization=true")) {
|
"--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).size().isEqualTo(3);
|
||||||
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
assertThat(configuration.output.get(0)).isEqualTo("{\"name\":\"RICKY\"}");
|
||||||
|
|||||||
Reference in New Issue
Block a user