INT-3958: Groovy Compiler Customization Support
JIRA: https://jira.spring.io/browse/INT-3958 * Introduce `compileStatic` and `compilerConfiguration` properties for the `GroovyScriptExecutingMessageProcessor`, to allow to customize the Groovy Script compilation * Introduce `compile-static` and `compiler-configuration` options on the `<int-groovy:script>` component * Change `RefreshableResourceScriptSource.suggestedClassName()` to return the **full** file name together with the extension. That may be useful during the filtering phase for Compiler customization * Modify tests to demonstrate different Compiler customization tricks * Since Spring Boot 1.4 is already based on the SF-4.3, make an appropriate upgrade, too Doc Polishing More Docs Polishing
This commit is contained in:
committed by
Gary Russell
parent
82c2da8dc4
commit
15026062ac
@@ -2,7 +2,7 @@
|
||||
=== Groovy support
|
||||
|
||||
In Spring Integration 2.0 we added Groovy support allowing you to use the Groovy scripting language to provide the logic for various integration components similar to the way the Spring Expression Language (SpEL) is supported for routing, transformation and other integration concerns.
|
||||
For more information about Groovy please refer to the Groovy documentation which you can find on the http://groovy.codehaus.org[project website]
|
||||
For more information about Groovy please refer to the Groovy documentation which you can find on the http://www.groovy-lang.org/[project website].
|
||||
|
||||
[[groovy-config]]
|
||||
==== Groovy configuration
|
||||
@@ -32,7 +32,7 @@ Also note that the `lang` attribute on the `<script>` tag is not valid in this n
|
||||
_Groovy object customization_
|
||||
|
||||
If you need to customize the Groovy object itself, beyond setting variables, you can reference a bean that implements `org.springframework.scripting.groovy.GroovyObjectCustomizer` via the `customizer` attribute.
|
||||
For example, this might be useful if you want to implement a domain-specific language (DSL) by modifying the MetaClass and registering functions to be available within the script:
|
||||
For example, this might be useful if you want to implement a domain-specific language (DSL) by modifying the `MetaClass` and registering functions to be available within the script:
|
||||
[source,xml]
|
||||
----
|
||||
<int:service-activator input-channel="groovyChannel">
|
||||
@@ -42,11 +42,11 @@ For example, this might be useful if you want to implement a domain-specific lan
|
||||
<beans:bean id="groovyCustomizer" class="org.foo.MyGroovyObjectCustomizer"/>
|
||||
----
|
||||
|
||||
Setting a custom GroovyObjectCustomizer is not mutually exclusive with `<variable>` sub-elements or the `script-variable-generator` attribute.
|
||||
Setting a custom `GroovyObjectCustomizer` is not mutually exclusive with `<variable>` sub-elements or the `script-variable-generator` attribute.
|
||||
It can also be provided when defining an inline script.
|
||||
|
||||
With _Spring Integration 3.0_, in addition to the `variable` sub-element, the `variables` attribute has been introduced.
|
||||
Also, groovy scripts have the ability to resolve a variable to a bean in the`BeanFactory`, if a binding variable was not provided with the name:
|
||||
Also, groovy scripts have the ability to resolve a variable to a bean in the `BeanFactory`, if a binding variable was not provided with the name:
|
||||
[source,xml]
|
||||
----
|
||||
<int-groovy:script>
|
||||
@@ -61,6 +61,63 @@ where variable `entityManager` is an appropriate bean in the application context
|
||||
|
||||
For more information regarding `<variable>`, `variables`, and `script-variable-generator`, see the paragraph '_Script variable bindings_' of <<scripting-config>>.
|
||||
|
||||
_Groovy Script Compiler Customization_
|
||||
|
||||
The `@CompileStatic` hint is the most popular Groovy compiler customization option,
|
||||
which can be used on the class or method level.
|
||||
See more information in the Groovy
|
||||
http://docs.groovy-lang.org/latest/html/documentation/index.html#_static_compilation[Reference Manual] and,
|
||||
specifically, http://docs.groovy-lang.org/latest/html/documentation/index.html#compilestatic-annotation[@CompileStatic].
|
||||
To utilize this feature for short scripts (in integration scenarios), we are forced to change a simple script like this
|
||||
(a `<filter>` script):
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
headers.type == 'good'
|
||||
----
|
||||
|
||||
to more Java-like code:
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
@groovy.transform.CompileStatic
|
||||
String filter(Map headers) {
|
||||
headers.type == 'good'
|
||||
}
|
||||
|
||||
filter(headers)
|
||||
----
|
||||
|
||||
With that, the `filter()` method will be transformed and compiled to static Java code, bypassing the Groovy
|
||||
dynamic phases of invocation, like `getProperty()` factories and `CallSite` proxies.
|
||||
|
||||
Starting with _version 4.3_, Spring Integration Groovy components can be configured with the `compile-static` `boolean`
|
||||
option, specifying that `ASTTransformationCustomizer` for `@CompileStatic` should be added to the internal
|
||||
`CompilerConfiguration`.
|
||||
With that in place, we can omit the method declaration with `@CompileStatic` in our script code and still get compiled
|
||||
plain Java code.
|
||||
In this case our script can still be short but still needs to be a little more verbose than interpreted script:
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
binding.variables.headers.type == 'good'
|
||||
----
|
||||
Where we can access the `headers` and `payload` (or any other) variables only through the `groovy.lang.Script`
|
||||
`binding` property since, with `@CompileStatic`, we don't have the dynamic `GroovyObject.getProperty()` capability.
|
||||
|
||||
In addition, the `compiler-configuration` bean reference has been introduced.
|
||||
With this attribute, you can provide any other required Groovy compiler customizations, e.g. `ImportCustomizer`.
|
||||
For more information about this feature, please, refer to the Groovy Documentation:
|
||||
http://groovy.jmiguel.eu/groovy.codehaus.org/Advanced+compiler+configuration.html[Advanced compiler configuration].
|
||||
|
||||
NOTE: Using `compilerConfiguration` does not automatically add a `ASTTransformationCustomizer` for `@CompileStatic`
|
||||
and overrides the `compileStatic` option.
|
||||
If `CompileStatic` is still requirement, a `new ASTTransformationCustomizer(CompileStatic.class)` should be manually
|
||||
added into the `CompilationCustomizers` of that custom `compilerConfiguration`.
|
||||
|
||||
NOTE: The Groovy compiler customization does not have any effect to the `refresh-check-delay` option
|
||||
and reloadable scripts can be statically compiled, too.
|
||||
|
||||
[[groovy-control-bus]]
|
||||
==== Control Bus
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ Sun developed a JSR223 reference implementation which works with Java 5 but it i
|
||||
|
||||
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 http://groovy.codehaus.org[Groovy] and http://jruby.org/[JRuby] projects provide JSR233 support in their standard distribution.
|
||||
The http://www.groovy-lang.org/[Groovy] and http://jruby.org/[JRuby] projects provide JSR233 support in their standard distribution.
|
||||
Other language implementations may be available or under development.
|
||||
Please refer to the appropriate project website for more information.
|
||||
|
||||
|
||||
@@ -136,3 +136,8 @@ See <<router>> for more information.
|
||||
AMQP, WS and XMPP header mappings (e.g. `request-header-mapping`, `reply-header-mapping`) now support negated
|
||||
patterns.
|
||||
See <<amqp-message-headers>>, <<ws-message-headers>>, and <<xmpp-message-headers>> for more information.
|
||||
|
||||
==== Groovy Scripts
|
||||
|
||||
Groovy scripts can now be configured with the `compile-static` hint or any other `CompilerConfiguration` options.
|
||||
See <<groovy-config>> for more information.
|
||||
|
||||
Reference in New Issue
Block a user