Jolokia goes Javascript

Starting with release 0.82, Jolokia contains now a brand new Javascript client library. This blog post highlights the main features and gives some usage examples.

It supports the full Jolokia protocol stack, including bulk requests and HTTP POST and GET requests. Both, synchronous and asynchronous operation modes are available and support for JSONP in order to come around the same-origin-policy is build in, too.

jolokia.js uses jQuery as underlying framework and provides two API layers: A basic one with full control over the requests and a simplified API for ease of use, but slightly less powerful.

The following example shows a asynchronous Jolokia request for reading the head memory from an Jolokia agent deployed on the same server which serves this script:

var j4p = new Jolokia("/jolokia");
j4p.request(
  { type: "READ", mbean: "java.lang:type=Memory", attribute: "HeapMemoryUsage"},
  {
    success: function(response) {
       var value = response.value;
       alert("Memory used: " + value.used / value.max * 100 + "%");
  }
});

The same can be done with the simplified API, which is shown in the next snippet, but this time performed synchronously.

var j4p = new Jolokia("/jolokia");
var value = j4p.getAttribute("java.lang:type=Memory","HeapMemoryUsage");
alert("Memory used: " + value.used / value.max * 100 + "%");

A bulk Jolokia request in looks like

var j4p = new Jolokia("/jolokia");
j4p.request(
  [
     {type: "version"},
     {type: "read",mbean: "java.lang:type=Threading",attribute: "ThreadCount"}
  ],
  {
     success:
        [
           function(resp) {
              console.log("Agent-Version: " + resp.value.agent);
           },
           function(resp) {
              console.log("Total number of threads: " + resp.value);
           },
        ]
  }
);

The full documentation of this library can be found in the Jolokia Reference Manual.

Acknowledgments goes to Stephan Beal, who initially kicked of the Javascript project with a layer on top of his Javascript messaging framework JSONMessage and which influenced heavily of the current design.

Finally, here is a full code example using jolokia.js for plotting the memory usage with the fine jquery.flot.js Plugin. You can save this code in a local file and open it in a browser. This example uses JSONP for accessing a Jolokia agent running at http://localhost:8080/jolokia.

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Jolokia plot demo</title>
  <script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
  <script src="http://flot.googlecode.com/svn/trunk/jquery.flot.js"></script>
  <script src="http://www.jolokia.org/dist/0.82/js/jolokia-min.js"></script>
  <script src="http://www.jolokia.org/dist/0.82/js/jolokia-simple-min.js"></script>
  <script src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
</head>
<body>
<h1>Jolokia Memory Plot Demo</h1>

<div id="memory" style="width:600px; height:300px; margin-top: 20px;"></div>
<input type="submit" id="gc" value="Garbage Collection"/>

<script id="source" language="javascript" type="text/javascript">
  var j4p = new Jolokia({url: "http://localhost:8080/jolokia",jsonp: true});
  var data = [];

  function run() {
    j4p.request({
                  type: "read",
                  mbean: "java.lang:type=Memory",
                  attribute: "HeapMemoryUsage"
                },
                {
                  success: function(resp) {
                    var value = resp.value.used / (1024 * 1024);
                    var time = resp.timestamp * 1000;
                    data.push([time, value]);
                    $.plot($("#memory"),[data],{xaxis: { mode: "time" }});
                    setTimeout(run,1000);
                  }
                });
  }
  $("#gc").click(function() {
    j4p.execute("java.lang:type=Memory","gc", {
          success: function() {
              console.log("Garbage collection performed");
          }
     });
  });
  run();
</script>
</body>
</html>

Author: Roland Huß
Categories: jolokia