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:
- Open the JavaScript file in a text editor.
- Find some nice code that works fine and "improve" it.
- Open a command prompt and start the agent.
- Then
- Watch the Agent fail to start up due to "syntax error" messages; or
- Log on to the management system
- Create a ticket and send it to the Agent
- Watch the console light up with syntax errors and wrong results.
- 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:
Code |
---|
[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 js> |
Actually, this just gets you the basic Rhino script engine. If you need access to the IA's classes, throw these commands into a rhino.sh file, save to the IAHOME/bin directory, and set the execute flag (chmod +x rhino.sh):
rhino.sh |
---|
#! /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 CP=$INSTALL_DIR/lib/mule-1.4.3/lib/user/js.jar:$INSTALL_DIR/lib/com.alarmpoint.apex.integrationagent.jar:$INSTALL_DIR/lib/integrationagent-utils.jar # 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 org.mozilla.javascript.tools.shell.Main |
Here's the equivalent Windows Batch script:
rhino.bat |
---|
@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.. cd %INSTALL_DIR% 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%" org.mozilla.javascript.tools.shell.Main |
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!
jjs>
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() );
2020-02-27T12:18:57-08:00
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
Comments
0 commentsPlease sign in to leave a comment.