JBoss Community Archive (Read Only)

RHQ 4.9

Modules in CLI

Overview

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.

Quick CommonJS primer

CommonJS modules are simple javascript files with one special feature. Anything that you want to be "exported" from the module, i.e. available to the callers, needs to be assigned to a special variable called exports. All other functions and objects that are defined in the module file are private to the module and cannot be used outside of it. Modules can also load another modules.

Below is a simple example module, let's call it "printedMath.js":

function privateFunction(x, y) {
    return x + y;
}

var privateVariable = "The sum of ";

//not used, but this is just to show you that other modules can be loaded inside
//a module.
var otherModule = require("otherModule.js");

exports.sum = function(x, y) {
    println(privateVariable + x + " and " + y + " = " + privateFunction(x, y));
}

Now to use the module in other scripts, you need to first "require" it and then use it:

var printedMath = require("printedMath.js");

printedMath.sum(1, 2);

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.

Module loading

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.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-13 08:04:08 UTC, last content change 2013-09-18 19:41:34 UTC.