From f285e81d08384ebe48260e8e09dde4c32db7946b Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 1 Mar 2021 13:38:03 -0500 Subject: [PATCH] Refactor scripting module Since Nashorn JavaScript Engine is deprecated in Java 11 (https://openjdk.java.net/jeps/335) it is better do nothing with JavaScript in the project any more * Replace JS script tests to some other languages * Mentioned deprecation in the Docs * Rework `scripting.adoc` for code snippet switches --- .../jsr223/Jsr223RouterTests-context.xml | 18 ++- .../config/jsr223/Jsr223RouterTests.groovy | 1 + .../config/jsr223/Jsr223RouterTests.java | 56 +++++---- .../config/jsr223/Jsr223RouterTests.js | 4 - .../Jsr223ServiceActivatorTests-context.xml | 2 +- .../jsr223/Jsr223ServiceActivatorTests.java | 4 +- .../scripting/dsl/ScriptsTests.java | 27 ++-- ...riveLanguageFromExtensionTests-context.xml | 1 - .../DeriveLanguageFromExtensionTests.java | 16 ++- .../jsr223/Jsr223ScriptExecutorTests.java | 11 +- .../integration/scripting/jsr223/hello.rb | 1 - .../scripting/jsr223/print_message.rb | 2 +- .../resources/scripts/TestRouterScript.js | 3 - .../resources/scripts/TestRouterScript.py | 1 + src/reference/asciidoc/scripting.adoc | 115 +++++++++++++++--- 15 files changed, 162 insertions(+), 100 deletions(-) create mode 100644 spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.groovy delete mode 100644 spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js delete mode 100644 spring-integration-scripting/src/test/resources/scripts/TestRouterScript.js create mode 100644 spring-integration-scripting/src/test/resources/scripts/TestRouterScript.py diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests-context.xml b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests-context.xml index 47bcac0318..174dd88c26 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests-context.xml +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests-context.xml @@ -16,26 +16,22 @@ - + - 5 ? "longStrings" : "shortStrings"; - })(); - ]]> + + payload.length() > 5 ? 'longStrings' : 'shortStrings' + - 5 ? "longStrings" : "shortStrings"; - })(); - ]]> + + payload.length() > 5 ? 'longStrings' : 'shortStrings' + diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.groovy b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.groovy new file mode 100644 index 0000000000..48854116ae --- /dev/null +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.groovy @@ -0,0 +1 @@ +payload.length() > maxLen as int ? 'longStrings' : 'shortStrings' diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.java index ab53c43deb..2252789fdd 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,25 +18,27 @@ package org.springframework.integration.scripting.config.jsr223; import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.channel.QueueChannel; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Mark Fisher * @author David Turanski * @author Artem Bilan + * * @since 2.1 */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig +@DirtiesContext public class Jsr223RouterTests { @Autowired @@ -49,19 +51,25 @@ public class Jsr223RouterTests { private MessageChannel scriptRouterWithinChainInput; @Autowired - private PollableChannel longStrings; + private QueueChannel longStrings; @Autowired - private PollableChannel shortStrings; + private QueueChannel shortStrings; + @AfterEach + void cleanUp() { + this.longStrings.clear(); + this.shortStrings.clear(); + } + @Test public void referencedScript() { // long is > 3 - Message message1 = new GenericMessage("aardvark"); - Message message2 = new GenericMessage("bear"); - Message message3 = new GenericMessage("cat"); - Message message4 = new GenericMessage("dog"); - Message message5 = new GenericMessage("elephant"); + Message message1 = new GenericMessage<>("aardvark"); + Message message2 = new GenericMessage<>("bear"); + Message message3 = new GenericMessage<>("cat"); + Message message4 = new GenericMessage<>("dog"); + Message message5 = new GenericMessage<>("elephant"); this.referencedScriptInput.send(message1); this.referencedScriptInput.send(message2); this.referencedScriptInput.send(message3); @@ -78,11 +86,11 @@ public class Jsr223RouterTests { @Test public void inlineScript() { // long is > 5 - Message message1 = new GenericMessage("aardvark"); - Message message2 = new GenericMessage("bear"); - Message message3 = new GenericMessage("cat"); - Message message4 = new GenericMessage("dog"); - Message message5 = new GenericMessage("elephant"); + Message message1 = new GenericMessage<>("aardvark"); + Message message2 = new GenericMessage<>("bear"); + Message message3 = new GenericMessage<>("cat"); + Message message4 = new GenericMessage<>("dog"); + Message message5 = new GenericMessage<>("elephant"); this.inlineScriptInput.send(message1); this.inlineScriptInput.send(message2); this.inlineScriptInput.send(message3); @@ -99,11 +107,11 @@ public class Jsr223RouterTests { @Test public void testInt2893ScriptRouterWithinChain() { - Message message1 = new GenericMessage("aardvark"); - Message message2 = new GenericMessage("bear"); - Message message3 = new GenericMessage("cat"); - Message message4 = new GenericMessage("dog"); - Message message5 = new GenericMessage("elephant"); + Message message1 = new GenericMessage<>("aardvark"); + Message message2 = new GenericMessage<>("bear"); + Message message3 = new GenericMessage<>("cat"); + Message message4 = new GenericMessage<>("dog"); + Message message5 = new GenericMessage<>("elephant"); this.scriptRouterWithinChainInput.send(message1); this.scriptRouterWithinChainInput.send(message2); this.scriptRouterWithinChainInput.send(message3); diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js deleted file mode 100644 index dd4081c82e..0000000000 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223RouterTests.js +++ /dev/null @@ -1,4 +0,0 @@ -function route(max) { - return payload.length > max ? "longStrings" : "shortStrings"; -} -route(maxLen); diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests-context.xml b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests-context.xml index 8cd122a4b7..b1ac8fc7a9 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests-context.xml +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests-context.xml @@ -29,7 +29,7 @@ + class="org.springframework.integration.scripting.config.jsr223.Jsr223ServiceActivatorTests$SampleScriptVariableSource"/> diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.java index 3de32d33a5..24f64aad22 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/config/jsr223/Jsr223ServiceActivatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -160,7 +160,7 @@ public class Jsr223ServiceActivatorTests { .withMessageContaining("Duplicated variable: foo"); } - public static class SampleScriptVariSource implements ScriptVariableGenerator { + public static class SampleScriptVariableSource implements ScriptVariableGenerator { @Override public Map generateScriptVariables(Message message) { diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/dsl/ScriptsTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/dsl/ScriptsTests.java index 8b0e4d1910..dc9246bf78 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/dsl/ScriptsTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/dsl/ScriptsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,10 @@ import java.io.File; import java.io.IOException; import java.util.Date; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -47,6 +45,7 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.FileCopyUtils; @@ -55,12 +54,12 @@ import org.springframework.util.FileCopyUtils; * * @since 5.0 */ -@RunWith(SpringRunner.class) +@SpringJUnitConfig @DirtiesContext public class ScriptsTests { - @ClassRule - public static final TemporaryFolder FOLDER = new TemporaryFolder(); + @TempDir + public static File FOLDER; private static File SCRIPT_FILE; @@ -99,13 +98,13 @@ public class ScriptsTests { @Autowired private PollableChannel messageSourceChannel; - @BeforeClass + @BeforeAll public static void setup() throws IOException { - SCRIPT_FILE = FOLDER.newFile("script.py"); + SCRIPT_FILE = new File(FOLDER, "script.py"); FileCopyUtils.copy("1".getBytes(), SCRIPT_FILE); } - @After + @AfterEach public void clear() { ((QueueChannelOperations) this.results).clear(); } @@ -243,7 +242,7 @@ public class ScriptsTests { @Bean public IntegrationFlow scriptRouter() { - return f -> f.route(Scripts.processor("scripts/TestRouterScript.js")); + return f -> f.route(Scripts.processor("scripts/TestRouterScript.py")); } @Bean diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests-context.xml b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests-context.xml index 7e5c379ecb..ff5c74b97f 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests-context.xml +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests-context.xml @@ -7,7 +7,6 @@ - diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests.java index 8eae6a8eaf..f2367166ed 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/DeriveLanguageFromExtensionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,22 +21,21 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.test.util.TestUtils; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author David Turanski * @author Artem Bilan * */ -@RunWith(SpringRunner.class) +@SpringJUnitConfig public class DeriveLanguageFromExtensionTests { @Autowired @@ -44,20 +43,19 @@ public class DeriveLanguageFromExtensionTests { @Test public void testParseLanguage() { - String[] langs = { "ruby", "Groovy", "ECMAScript", "python", "kotlin" }; + String[] langs = { "ruby", "Groovy", "python", "kotlin" }; Class[] executors = { RubyScriptExecutor.class, DefaultScriptExecutor.class, - DefaultScriptExecutor.class, PythonScriptExecutor.class, KotlinScriptExecutor.class }; Map scriptProcessors = this.ctx.getBeansOfType(ScriptExecutingMessageProcessor.class); - assertThat(scriptProcessors.size()).isEqualTo(5); + assertThat(scriptProcessors.size()).isEqualTo(4); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 4; i++) { ScriptExecutingMessageProcessor processor = ctx.getBean( "org.springframework.integration.scripting.jsr223.ScriptExecutingMessageProcessor#" + i, ScriptExecutingMessageProcessor.class); diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutorTests.java b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutorTests.java index ad299329fe..f300235fbd 100644 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutorTests.java +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/Jsr223ScriptExecutorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException import java.util.HashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -58,13 +58,6 @@ public class Jsr223ScriptExecutorTests { assertThat(result).isNotNull().contains("payload modified"); } - @Test - public void testJs() { - ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("js"); - Object obj = executor.executeScript(new StaticScriptSource("function js(){ return 'js';} js();")); - assertThat(obj).isNotNull().isEqualTo("js"); - } - @Test public void testPython() { ScriptExecutor executor = ScriptExecutorFactory.getScriptExecutor("python"); diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/hello.rb b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/hello.rb index 826fe8c68b..a262a2c207 100755 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/hello.rb +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/hello.rb @@ -1,7 +1,6 @@ require 'java' class RubyHello - include_class 'com.dturanski.test.jruby.Hello' def say "hello,world" end diff --git a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb index cffae5fa89..677ed132a1 100755 --- a/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb +++ b/spring-integration-scripting/src/test/java/org/springframework/integration/scripting/jsr223/print_message.rb @@ -1,5 +1,5 @@ require "java" -include_class 'java.util.Date' +java_import 'java.util.Date' #payload and headers a global variable if payload payload = payload+" modified #{Date.new}" diff --git a/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.js b/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.js deleted file mode 100644 index 80a8fac8d7..0000000000 --- a/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.js +++ /dev/null @@ -1,3 +0,0 @@ -(function(){ - return payload.length > 5 ? "longStrings" : "shortStrings"; -})(); diff --git a/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.py b/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.py new file mode 100644 index 0000000000..0a9f486616 --- /dev/null +++ b/spring-integration-scripting/src/test/resources/scripts/TestRouterScript.py @@ -0,0 +1 @@ +'longStrings' if len(payload) > 5 else 'shortStrings' \ No newline at end of file diff --git a/src/reference/asciidoc/scripting.adoc b/src/reference/asciidoc/scripting.adoc index a89e10abcd..7264104f88 100644 --- a/src/reference/asciidoc/scripting.adoc +++ b/src/reference/asciidoc/scripting.adoc @@ -2,14 +2,17 @@ === Scripting Support Spring Integration 2.1 added support for the https://www.jcp.org/en/jsr/detail?id=223[JSR223 Scripting for Java specification], introduced in Java version 6. -It lets you use scripts written in any supported language (including Ruby, JRuby, Javascript, Groovy and Kotlin) to provide the logic for various integration components, similar to the way the Spring Expression Language (SpEL) is used in Spring Integration. +It lets you use scripts written in any supported language (including Ruby, JRuby, Groovy and Kotlin) to provide the logic for various integration components, similar to the way the Spring Expression Language (SpEL) is used in Spring Integration. For more information about JSR223, see the https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html[documentation]. +NOTE: Starting with Java 11, the Nashorn JavaScript Engine has been deprecated with possible removal in Java 15. +It is recommended to reconsider in favor of other scripting language from now on. + You need to include this dependency into your project: ==== +[source, xml, subs="normal", role="primary"] .Maven -[source, xml, subs="normal"] ---- org.springframework.integration @@ -17,9 +20,8 @@ You need to include this dependency into your project: {project-version} ---- - +[source, groovy, subs="normal", role="secondary"] .Gradle -[source, groovy, subs="normal"] ---- compile "org.springframework.integration:spring-integration-scripting:{project-version}" ---- @@ -30,8 +32,29 @@ In addition you need to add a script engine implementation, e.g. JRuby, Jython. Starting with version 5.2, Spring Integration provides a Kotlin Jsr223 support. You need to add these dependencies into your project to make it working: + ==== -[source, groovy] +[source, xml, subs="normal", role="primary"] +.Maven +---- + + org.jetbrains.kotlin + kotlin-script-util + runtime + + + org.jetbrains.kotlin + kotlin-compiler-embeddable + runtime + + + org.jetbrains.kotlin + kotlin-scripting-compiler-embeddable + runtime + +---- +[source, groovy, subs="normal", role="secondary"] +.Gradle ---- runtime 'org.jetbrains.kotlin:kotlin-script-util' runtime 'org.jetbrains.kotlin:kotlin-compiler-embeddable' @@ -41,10 +64,7 @@ runtime 'org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable' The `KotlinScriptExecutor` is selected by the provided `kotlin` language indicator or script file comes with the `.kts` extension. -IMPORTANT: Note that this feature requires Java 6 or higher. - In order to use a JVM scripting language, a JSR223 implementation for that language must be included in your class path. -Java 6 natively supports Javascript. The https://groovy-lang.org/[Groovy] and https://www.jruby.org[JRuby] projects provide JSR233 support in their standard distributions. IMPORTANT: Various JSR223 language implementations have been developed by third parties. @@ -61,9 +81,27 @@ To enable scripting support, Spring Integration defines a `ScriptExecutingMessag All you need to do is write a script that uses these variables. The following pair of examples show sample configurations that create filters: -.Filter ==== -[source,xml] +[source, java, role="primary"] +.Java DSL +---- +@Bean +public IntegrationFlow scriptFilter() { + return f -> f.filter(Scripts.processor("some/path/to/ruby/script/RubyFilterTests.rb")); +} +... +@Bean +public Resource scriptResource() { + return new ByteArrayResource("headers.type == 'good'".getBytes()); +} + +@Bean +public IntegrationFlow scriptFilter() { + return f -> f.filter(Scripts.processor(scriptResource()).lang("groovy")); +} +---- +[source, xml, role="secondary"] +.XML ---- @@ -80,7 +118,7 @@ The following pair of examples show sample configurations that create filters: ==== As the preceding examples show, the script can be included inline or can be included by reference to a resource location (by using the `location` attribute). -Additionally, the `lang` attribute corresponds to the language name (or its JSR223 alias) +Additionally, the `lang` attribute corresponds to the language name (or its JSR223 alias). Other Spring Integration endpoint elements that support scripting include `router`, `service-activator`, `transformer`, and `splitter`. The scripting configuration in each case would be identical to the above (besides the endpoint element). @@ -89,7 +127,14 @@ Another useful feature of scripting support is the ability to update (reload) sc To do so, specify the `refresh-check-delay` attribute on the `script` element, as the following example shows: ==== -[source,xml] +[source, java, role="primary"] +.Java DSL +---- +Scripts.processor(...).refreshCheckDelay(5000) +} +---- +[source, xml, role="secondary"] +.XML ---- ---- @@ -101,7 +146,14 @@ If the script is updated, any invocation that occurs later than 5 seconds since Consider the following example: ==== -[source,xml] +[source, java, role="primary"] +.Java DSL +---- +Scripts.processor(...).refreshCheckDelay(0) +} +---- +[source, xml, role="secondary"] +.XML ---- ---- @@ -113,7 +165,14 @@ This is the default behavior. The following example shows a script that never updates: ==== -[source,xml] +[source, java, role="primary"] +.Java DSL +---- +Scripts.processor(...).refreshCheckDelay(-1) +} +---- +[source, xml, role="secondary"] +.XML ---- ---- @@ -126,14 +185,22 @@ IMPORTANT: Inline scripts can not be reloaded. Variable bindings are required to enable the script to reference variables externally provided to the script's execution context. By default, `payload` and `headers` are used as binding variables. -You can bind additional variables to a script by using `` elements, as the following example shows: +You can bind additional variables to a script by using `` elements (or `ScriptSpec.variables()` option), as the following example shows: ==== -[source,xml] +[source, java, role="primary"] +.Java DSL ---- - - - +Scripts.processor("foo/bar/MyScript.py") + .variables(Map.of("var1", "thing1", "var2", "thing2", "date", date)) +} +---- +[source, xml, role="secondary"] +.XML +---- + + + ---- @@ -191,7 +258,15 @@ This method is called every time the script is executed for a message. The following example shows how to provide an implementation of `ScriptVariableGenerator` and reference it with the `script-variable-generator` attribute: ==== -[source,xml] +[source, java, role="primary"] +.Java DSL +---- +Scripts.processor("foo/bar/MyScript.groovy") + .variableGenerator(new foo.bar.MyScriptVariableGenerator()) +} +---- +[source, xml, role="secondary"] +.XML ----