Version: 17.07
Supported Since: 17.07
A Groovy Snippet processing element facilitates execution of scripts written in Groovy language within an integration flow. This means that you can delegate the processing of incoming messages to custom or pre-written Groovy scripts.
Groovy Snippet can accept either the source code of the script itself, or the path to a file containing
the actual script (which can either be bundled inside the src/main/resources
directory of the project,
or be pointed to a known external location).
With framework-level support, this processing element facilitates you to define and switch the actual script content,
statically (during development) as well as dynamically at ESB startup time
(via an externalized property,
or by using an external script file whose path can also be changed in the same way during deployment).
The following context parameters are made available within the scope of the executed script:
|
the default logger for the processing element instance |
|
the received message context, as an |
|
the internal |
The script can usually be precompiled at ESB startup (so that the runtime overhead of script evaluation is reduced),
by enabling the Precompile
option of the processing element configuration.
If precompilation fails, an appropriate warning is generated and the system falls back to runtime compilation
so that the script will be evaluated separately for each incoming message.
It is recommended to test whether precompilation works properly for the actual script you wish to evaluate at runtime,
however, as precompilation is known to cause the script to unexpectedly fail at runtime in some cases.
If the script returns a result upon successful execution, it can be stored into a variable to be utilized later
in the message flow, by specifying the required variable name via the Assign Result
option.
Next |
The message will be sent to this outport if the script evaluation succeeds, i.e. if there are no errors thrown outside the script evaluation engine |
On Exception |
The message will be sent to this outport if the script evaluation fails |
Load from File |
if enabled, the script will be loaded from a file (external or resource) specified by |
Path |
path of the file containing the script, in case |
Code |
source code of the script to be evaluated, in case |
Assign Result |
name of the variable to which (if specified) the return value of a successful script evaluation will be assigned |
Precompile |
whether the script should be precompiled at ESB startup; precompilation can enhance runtime performance, but may not be compatible with every arbitrary script, so it is recommended to test the actual script against this option before the actual deployment |
What follows is a simple example which utilizes a Groovy Snippet processing element to implement an HTTP "echo" service
that composes and returns a response body containing the request URL, headers and payload from the original request.
The flow utilizes a NIO HTTP Ingress Connector
and a Groovy Snippet that accesses various fields of the received message context (ctx
) and message (msg
)
in order to compute and populate the echo-back response.
Following is the configuration used in the Groovy Snippet in the above flow:
Load from File |
|
Path |
|
Precompile |
|
Source of echo-back.groovy
:
req = []
verb = msg.getStringMessageProperty('x.http.method').get() + ' ' + ctx.getStringContextProperty('x.http.service_url').get()
query = msg.getStringMessageProperty('x.http.query_string')
if (query.isPresent()) verb += '?' + query.get()
req.push(verb)
req.push('') //blank line
headers = msg.getTransportHeaders()
for (h in headers) req.push(h.getKey() + ': ' + h.getValue())
req.push('')
req.push(msg.getPayload().readAsString(-1))
msg.setPayload(new org.adroitlogic.x.base.format.StringFormat(req.join('\n')))