1.3.1.6 Message Handling Script

The Message Handling Script processor allows you to specify your own processor logic using JavaScript or Groovy, entered as an option for the processor.

The Message Handling Script processor is closely related to the Script processor. The main difference between this processor and the Script processor is that the Message Handling Script processor is designed to handle messages, which may contain many records. The Message Handling Script processor is capable of performing operations over all the records in a message, and has access to message level attributes such as the message ID.

Use the message handling script processor to define processing logic that acts across all the records in a message, or that acts selectively on only the first record in the message.

The Message Handling Script processor presents no summary statistics on its processing.

In the Data view, each input attribute is shown with the output attributes to the right.

Script

The following notes are important to consider when writing a script for the Message Handling Script processor.

The Message Handling Script processor takes a message as its unit of work. A message may be composed of multiple records, which are modeled as an array named records within the Message Handling Script processor. Each record in the array has its own set of input attributes and its own output attribute, corresponding to the input1 and output1 attributes of the standard Script processor.

In addition, a pre-defined variable, messageid, contains the unique message ID, and a variable tags, which contains the message level tags.

Each record also has a cancel() function, which suppresses further processing of the record in the process.

Note:

It is possible to specify a function to be called for each record (as opposed to executing the entire script each time), and to change the language of the script.

To set a function include the following line at the top of the script (where 'doit' is the function name; change this to the function name you are using):

#! function : doit

To change the language of the script to groovy rather than Javascript, add the line:

#! language : groovy

Examples

The following script iterates across all the records in a message to calculate the sum of the values of the first input attribute for each record (which is assumed to be a number). It then iterates across the records a second time, and sets the value of the output attribute of each record to the sum calculated in the first step:

var sum = 0;
for (var i = 0; i < records.length; i++)
{
  sum += records[i].input1[0];
 
}
 
for (var i = 0; i < records.length; i++)
{
  records[i].output1 = sum;
 
}

The following script computes the sum as before, but all but the first record are suppressed using the cancel() function. This results in a single record - the first - being output per message, with the total of the first input attribute across all records as its output attribute.

var sum = 0;
for (var i = 0; i < records.length; i++)
{
  sum += records[i].input1[0];
 
}
 
for (var i = 0; i < records.length; i++)
{
  if (i == 0)
 
  {
 
    records[i].output1 = sum;
 
  }
 
  else
 
  {
 
    records[i].cancel();
 
  }
 
}