Jolokia's Javascript client library allows for easy access to the Jolokia agent from within Javascript scripts. It encapsulates the JSON/HTTP access within a simple object layer.
The Javascript client binding provides the following features:
Documentation for the Javascript library can be found in the reference manual in the chapter Javascript Client Library.
The Jolokia Javascript library can be downloaded from the download page. It is divided into two scripts: jolokia.js contains the basic Jolokia object definition which provides the generic request() method. jolokia-simple.js adds to this definition methods for a simplified access, which are easier to use but less powerful. In addition these libs require jQuery and, if the target brosers don't support native JSON serialization, json2.js. A typical setup in an HTML page using the library looks like:
<head> <script type="text/javascript" src="jquery-1.7.2.js"></script> <script type="text/javascript" src="json2.js"></script> <script type="text/javascript" src="jolokia.js"></script> <script type="text/javascript" src="jolokia-simple.js"></script> </head>
For production setup, the minified versions (e.g. jolokia-min.js) are recommended since the original are blown up with inline documentation.
The following simple example shows how to fetch the used Heap Memory from a Jolokia agent:
// Create a new jolokia client accessing the agent on the same
// Host which serves this document:
var j4p = new Jolokia("/jolokia");
// Request the memory information asynchronously and print it on
// the console
j4p.request(
{
type: "read",
mbean: "java.lang:type=Memory",
attribute: "HeapMemoryUsage"
},
{
success: function(response) {
console.log(JSON.stringify(response));
console.log("Heap-Memory used: " + response.value.used);
}
});
// Same as above, but synchronously and with the simple API:
console.log("Heap-Memory used:" +
j4p.getAttribute("java.lang.type=Memory","HeapMemoryUsage","used"));Cubism is a d3.js plugin for plotting time series data. It can be easily be used with Jolokia for periodically reading JMX attributes and plotting them in the browser.
More details on the Jolokia-Cubism integration along with some demo can be found on an extra page.