Contact Us 1-800-596-4880

Ruby Component Reference

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 the custom-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.

ruby_general
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, Ruby Script Home Page.

Script

Script Text: Type the script that the component loads directly into this space.
Script File: Enter the location of the script to be loaded by the component. The file can reside on the classpath or the local file system.
Bean Reference
: Lets you add a Java bean, which encapsulates multiple objects into a single bean. The Script component can then store and re-use the bean when applicable.

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.

ruby_advanced

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.

Script Properties

Configure these parameters to define the attribute keys and their associated values. This enables the script component to quickly look up a value associated with a key.

See Also