Ruby Component Reference
Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version. |
In spite of the rich selection of elements available in Mule, you may find yourself in need to put together some custom code to carry out part of a complex task, if you’re comfortable coding in Ruby, then the Ruby Component is the ideal versatile tool to fall back on for these cases. Mule also includes also equivalent scripting components for other languages. The Ruby Component is also handy for when you’re modernizing legacy systems, as you can simply throw the old lines of code into a component instead of having to reengineer the code’s behavior through a series of different Mule components (this is not ideal from a debugging and maintenance perspective, though).
Ruby also allows the developer to configure interceptors and alter the values or references of particular properties in a script. Interceptors are configured to provide additional services to a message as it flows through a component. For example, a developer can configure an interceptor to execute scheduling or logging of a particular event while a message is being processed. The Ruby component also includes a custom interceptor which allows you to configure settings for Spring elements. Use the Advanced tab to access Script Properties, then define the value and reference for a particular property in the script. When using Ruby, typing-in or appending a script to the component is the only configuration required.
Notes:
-
To access Mule objects from a Ruby script, prefix the objects with a dollar sign ($) because Mule objects are global variables from a Ruby perspective. For example:
$message.getInvocationProperty('pepe')
. For an example of a Ruby script in a Mule flow, browse to Anypoint Exchange > Tracking a Custom Business Event, click Download, and examine thecustom-business-events.xml
file. -
Placing a Ruby Component is actually the same as placing a Script component and then selecting the Ruby engine.
-
The use of
#{}
notations in Ruby is interpreted by Mule as being a Spring Expression Language expression, which causes Mule to not execute the Ruby code. To solve this, move your code into a separate file and use the "Script file" attribute instead of adding the script into the "Script Text" box.
Configuration
General Tab
To configure the Ruby component, from the Message Flow canvas double-click the Ruby icon to open the Properties Editor. Use the General tab to specify the file location of the script or simply type in the script on the script text window. The scripting engine is Ruby.
Panel | Description |
---|---|
Display |
Defaults to the generic component name. Change the Display Name, which must be alpha-numeric, to reflect the component’s specific role, such as, |
Script |
Script Text: Type the script that the component loads directly into this space. |
Advanced Tab
Use the Advanced tab to optionally configure interceptors and, depending on the interceptor, enter Spring values.You also have the option to specify script properties, which are key/value pairs used to alter or change properties in the script. See the Script Component Reference page for information on using the advanced tab.
Interceptors
Interceptors alter the values or references of particular properties in a script. They are configured to provide additional services to a message as it flows through a component. For example, you can configure an interceptor to execute scheduling or logging of a particular event while a message is being processed. The Script component also includes a custom interceptor which allows you to configure settings for Spring elements.
For example, you can add an interceptor that logs transactions and the time for each transaction. Use the Add Custom Interceptor to create a custom interceptor that can reference Spring objects. The Interceptor Stack enables you to bundle multiple interceptors. Use the Interceptor Stack to apply multiple interceptors on a Ruby component. The interceptors are applied in the order defined in the stack.
See Also
-
Refer to the generic Script Component Reference to see how a script works with one-way and request-response connectors.
-
For more information see the Mule ESB page Scripting Module.