Get Groovy with Metl and Scripting

Nathan Richardson
Data Weekly by Jumpmind
3 min readFeb 22, 2018

Metl provides many out-of-the-box components to address the various integration requirements seen in today’s data environments. Sometimes however, there are unique scenarios that don’t seem to have a specific component to handle them. At these times don’t forget about the Groovy Script component which provides the developer the ability to write custom logic using Groovy when the task doesn’t warrant the creation of a full blown custom component.

The Script component runs custom groovy scripts at different points in the component life-cycle. The ‘when’, is determined by the drop-down at the top of the advanced editor and the code entered under each.

The different run points are as follows:

  • onInit() — Upon component initialization. Use this if there are actions necessary at the time when a flow starts. For example, if there are variables that need to be initialized and maintained for the life of the flow or if there is some specific data that needs to be pulled or set before the main process runs put your logic here.
  • onHandleMessage() — This is the main place to put your groovy script that does some sort of action based on the data messages processed. This method is called for each message.
  • onSuccess() — Use this if there is an action you would like to perform when the flow completes successfully, such as updating a job status table.
  • onError() — Similar to onSuccess() this can be used to execute a groovy script if the flow ends in error. You could send an alert message or update a batch error table.
  • <Imports> — Sometimes there is a need to reference external methods. These can be referenced and included using this Imports section just like traditional java imports. For example:
import org.jumpmind.util.FormatUtils;import org.springframework.dao.EmptyResultDataAccessException;
  • <Methods> — At times you might want to use a method in your script to perform a specific task or if there is a repetitive action. The groovy script to accomplish this would be included here and is in turn made available for use by your other groovy code within this component.

To get you started with your scripting, templates have been provided to give some example uses. Templates can be found by opening the advanced editor page of a script component and clicking the Templates button on the upper right. There are templates that show how to put values in parameters that can be accessed in other components, to formatting an email message body, to using a jdbc resource to pull a value from a database table. Others show how to manipulate entity data objects by adding new or modifying existing attribute entries.

As a part of the Groovy Script component there are predefined variables and methods available for use from within your script via the ScriptHelper class. The ScriptHelper features are documented on GitHub.

For more details and code snippets on the templates, see the Script Templates Metl Wiki page on GitHub.

--

--