RHQ now supports CommonJS modules in its CLI and scripted alert notifications on the server. Using the modules, it is easy to create reusable functional parts that you can easily compose together. This is great for RHQ, whose API is a little bit chatty so various utility functions will therefore come in handy for any task you might want to automate.
Below is a simple example module, let's call it "printedMath.js":
Now to use the module in other scripts, you need to first "require" it and then use it:
Note that in the script that uses the printedMath module, you cannot access neither the privateVariable variable, nor the privateFunction. Because you can only access the module's exported features through a variable that the module has been assigned to, i.e. the privateMath variable in the example above, it is possible for two modules to require functions with the same names and they won't conflict in the script that would use both of those modules.
The modules can be loaded from various locations including filesystem and RHQ repositories. Especially the ability load the scripts from the repositories on the RHQ server lends itself to a clever script management. If you place your scripts in the server, any update you make to them is automatically picked up by any existing or future remote CLI - if you have multiple CLI installations, you don't need to update them one by one with the latest version of your script - it just gets picked up at the point in time you use it.
The loading of modules is pluggable. You can read about the default module loaders and how to provide new module loaders (aka script source providers) on the Script Sources page.