ruby snippet

Ruby Snippet

Version: 17.07

Supported Since: 17.07

What is a Ruby Snippet?

A Ruby Snippet processing element facilitates execution of scripts written in Ruby language within an integration flow. This means that you can delegate the processing of incoming messages to custom or pre-written Ruby scripts.

Ruby 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

ctx, context

the received message context, as an XMessageContext instance

msg, message

the internal XMessage instance of the received message context, obtainable via context.getMessage()

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.

outports ruby

Out Ports


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; note that based on the value of this parameter, either Path or Code must be specified


path of the file containing the script, in case Load from File is enabled; this can be a classpath resource (e.g. located in src/main/resources) or an external file at a known location (e.g. /home/username/esb/process-msg.js)


source code of the script to be evaluated, in case Load from File is disabled; for multiline scripts it is recommended to use the Path (script file) option instead, with Load from File enabled

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

Sample Use Case

What follows is a simple example which utilizes a Ruby 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 Ruby Snippet that accesses various fields of the received message context (ctx) and message (msg) in order to compute and populate the echo-back response.

echo back flow ruby

Following is the configuration used in the Ruby Snippet in the above flow:

Load from File






Source of echo-back.rb:

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('')    # blank line
msg.getTransportHeaders().each do |h|
    req.push(h[0] + ': ' + h[1])
In this topic
In this topic
Contact Us