Relieving the pain of JavaScript Development for the IA and the xAgent

The Problem

Have you ever wanted to break things (as in doing violence to inanimate objects) when writing Javascript code for the IA or the xAgent?

We're all familiar with the steps:

  1. Open the JavaScript file in a text editor.
  2. Find some nice code that works fine and "improve" it.
  3. Open a command prompt and start the agent.
  4. Then
    1. Watch the Agent fail to start up due to "syntax error" messages;  or
    2. Log on to the management system
    3. Create a ticket and send it to the Agent
    4. Watch the console light up with syntax errors and wrong results.
  5. Repeat 1-4 until a) it works, b) you reach for your big hammer, or c) you develop a stress ulcer.

Fun as this is, there's a better way!

The Better Way

What if I told you that it's possible to get an interactive JavaScript command environment?  And what if I told you it uses the exact same JavaScript engine that the Agent uses? 

I know, right?

The IA Way

To get the IA's Javascript engine, just run these commands:


[user@tree bin]$ cd $IAHOME

[user@tree integrationagent-5.3.0]$ ./jre/bin/java -jar ./lib/mule-1.4.3/lib/user/js.jar

Rhino 1.6 release 5 2006 11 18


Actually, this just gets you the basic Rhino script engine.  If you need access to the IA's classes, throw these commands into a file, save to the IAHOME/bin directory, and set the execute flag (chmod +x

#! /bin/sh

# Set the base install directory. This basically just gets the current working directory, then goes up one level

INSTALL_DIR=$( cd "$( dirname "$0" )" && pwd )/..

# Here we set the class path, first to include the js.jar (rhino shell), then the IA jar files


# Finally, call java and pass the class path we created above. Then we invoke the Main method of the Rhino shell, stored in js.jar

$INSTALL_DIR/jre/bin/java -cp $CP

Here's the equivalent Windows Batch script:


@echo off

rem Keep environment variables local to this script (not supported in all versions of Windows)

IF (%OS%) == (Windows_NT) SETLOCAL

rem Get SFN path to install directory.

rem A bug in some versions of Windows makes %~dsp0 append the filename, so use this workaround.

rem Note: correct to append .. instead of \ since %~dsp0 ends with \

for %%i in (%~sf0) do set INSTALL_DIR=%%~dspi..


set CP=lib\mule-1.4.3\lib\user\js.jar;lib\com.alarmpoint.apex.integrationagent.jar;lib\integrationagent-utils.jar;conf\cli;lib\mule-1.4.3\lib\mule\*;lib\mule-1.4.3\lib\user\*;lib\mule-1.4.3\lib\opt\*

jre\bin\java -cp "%CP%"

Once this is running, you can import the necessary packages into your script using the importClass and importPackage functions that the integrations use. Also, you can use the "load" function to pull in other JavaScript files.

The Integration Builder / Flow Scripting Way

The Integration Builder (aka xAgent and Flow) uses Nashorn, which is essentially an enhanced version of Rhino.  A Google search will show the important differences between them, but the takeaway for IB script developers is that by testing your code snippets in Nashorn, you will be using the exact same JavaScript engine that the IB uses. 

To run the Nashorn interpreter on Windows (you'll need the xMatters Agent for this):

cd /d "%programfiles%\xa\windows\jdk-11.0.4\bin"
jjs.exe --language=es6
jjs> print("Hooray, it works!");
Hooray, it works!

Note that you will need to provide the "--language=es6" flag as shown above, to tell Nashorn to provide the same ECMAScript 6 compatibility as the Integration Builder.  Otherwise, some ES6 instructions won't work.

On Linux, the xAgent ls in /opt/xmatters/xa

If you don't have an xAgent installed already, you can get it from your xMatters instance (look for "Agents" in the Workflows menu.)

The "jjs" Nashorn shell will let you test your code snippets, and you can even use shared libraries.  For example, to incorporate moment.js in your Nashorn environment, download the code and save it in a file.  Then import it into your script with the "load" instruction: 

jjs> load("/path/to/moment.js");
jjs> print( moment().format() );

Now you can run, test, and fix your code snippet in a fraction of the time - no hammer required!

xMatters Reference

Blog- Originally created by Travis Depuy

Was this article helpful?
0 out of 0 found this helpful



Please sign in to leave a comment.