We're all familiar with the steps:
- Find some nice code that works fine and "improve" it.
- Open a command prompt and start the agent.
- 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
I know, right?
The IA Way
[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 rhino.sh file, save to the IAHOME/bin directory, and set the execute flag (chmod +x rhino.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
Here's the equivalent Windows Batch script:
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..
The Integration Builder / Flow Scripting Way
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> 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> print( moment().format() );
Now you can run, test, and fix your code snippet in a fraction of the time - no hammer required!
Blog- Originally created by Travis Depuy