This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new 2eaa713 Lambda processor doesn't work with JS routes #115 2eaa713 is described below commit 2eaa713b59dcb243ee25fc923b37e0f83206de1b Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Thu Jul 25 10:28:57 2019 +0200 Lambda processor doesn't work with JS routes #115 --- camel-k-loader-js/pom.xml | 5 ++++ .../camel/k/loader/js/JavaScriptRoutesLoader.java | 27 ++++++++++++++-------- .../camel/k/loader/js/dsl/IntegrationTest.java | 20 ++++++++++++++++ .../src/test/resources/routes-with-processors.js | 7 ++++++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/camel-k-loader-js/pom.xml b/camel-k-loader-js/pom.xml index 40edead..5712cac 100644 --- a/camel-k-loader-js/pom.xml +++ b/camel-k-loader-js/pom.xml @@ -92,6 +92,11 @@ <artifactId>camel-main</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-mock</artifactId> + <scope>test</scope> + </dependency> <!-- ******************************* --> <!-- test deps --> diff --git a/camel-k-loader-js/src/main/java/org/apache/camel/k/loader/js/JavaScriptRoutesLoader.java b/camel-k-loader-js/src/main/java/org/apache/camel/k/loader/js/JavaScriptRoutesLoader.java index ec5f298..c6ad4d2 100644 --- a/camel-k-loader-js/src/main/java/org/apache/camel/k/loader/js/JavaScriptRoutesLoader.java +++ b/camel-k-loader-js/src/main/java/org/apache/camel/k/loader/js/JavaScriptRoutesLoader.java @@ -27,6 +27,7 @@ import org.apache.camel.k.RoutesLoader; import org.apache.camel.k.Source; import org.apache.camel.k.loader.js.dsl.IntegrationConfiguration; import org.apache.camel.k.support.URIResolver; +import org.apache.camel.support.LifecycleStrategySupport; import org.apache.commons.io.IOUtils; import org.graalvm.polyglot.Context; import org.graalvm.polyglot.Value; @@ -44,24 +45,30 @@ public class JavaScriptRoutesLoader implements RoutesLoader { return new RouteBuilder() { @Override public void configure() throws Exception { - final CamelContext context = getContext(); + final Context context = Context.newBuilder("js").allowAllAccess(true).build(); - try (Context ctx = createPolyglotContext(); InputStream is = URIResolver.resolve(context, source)) { - Value bindings = ctx.getBindings(LANGUAGE_ID); + try (InputStream is = URIResolver.resolve(camelContext, source)) { + Value bindings = context.getBindings(LANGUAGE_ID); // configure bindings - bindings.putMember("dsl", new IntegrationConfiguration(this)); + bindings.putMember("__dsl", new IntegrationConfiguration(this)); final String script = IOUtils.toString(is, StandardCharsets.UTF_8); - final String wrappedScript = "with (dsl) { " + script + " }"; + final String wrappedScript = "with (__dsl) { " + script + " }"; - ctx.eval(LANGUAGE_ID, wrappedScript); + context.eval(LANGUAGE_ID, wrappedScript); + + // + // Close the polyglot context when the camel context stops + // + getContext().addLifecycleStrategy(new LifecycleStrategySupport() { + @Override + public void onContextStop(CamelContext camelContext) { + context.close(true); + } + }); } } }; } - - private static Context createPolyglotContext() { - return Context.newBuilder("js").allowAllAccess(true).build(); - } } diff --git a/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java b/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java index a881b3a..3f55455 100644 --- a/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java +++ b/camel-k-loader-js/src/test/java/org/apache/camel/k/loader/js/dsl/IntegrationTest.java @@ -18,6 +18,7 @@ package org.apache.camel.k.loader.js.dsl; import java.util.List; +import org.apache.camel.ProducerTemplate; import org.apache.camel.component.seda.SedaComponent; import org.apache.camel.k.Runtime; import org.apache.camel.k.listener.RoutesConfigurer; @@ -99,4 +100,23 @@ public class IntegrationTest { runtime.run(); } + + @Test + public void testProcessors() throws Exception { + ApplicationRuntime runtime = new ApplicationRuntime(); + runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes-with-processors.js")); + runtime.addListener(Runtime.Phase.Started, r -> { + ProducerTemplate template = r.getCamelContext().createProducerTemplate(); + + String a = template.requestBody("direct:arrow", "", String.class); + assertThat(a).isEqualTo("arrow"); + + String f = template.requestBody("direct:function", "", String.class); + assertThat(f).isEqualTo("function"); + + runtime.stop(); + }); + + runtime.run(); + } } diff --git a/camel-k-loader-js/src/test/resources/routes-with-processors.js b/camel-k-loader-js/src/test/resources/routes-with-processors.js new file mode 100644 index 0000000..9d35147 --- /dev/null +++ b/camel-k-loader-js/src/test/resources/routes-with-processors.js @@ -0,0 +1,7 @@ + +from('direct:function') + .process(function(e) { e.getMessage().setBody('function') }); + +from('direct:arrow') + .process(e => { e.getMessage().setBody('arrow') }); +