The rest4jmx is a simple service that exposes a Java MBeanServer as a restful API with JSON or JSONP as data protocol. With the service is is possible to:
The service either generates JSON data as application/json or JSONP data as application/x-javascript. To get JSONP use a callback parameter named "callback" or make sure the Accept header is set to application/x-javascript (if no callback parameter is given a callback will be done against the method named "callback").
The latest version of rest4jmx is available at github.
There are two examples available here.
The MBean Server Tree viewer is both an example and a way to view which mbeans are available. Login as guest:guest or admin:admin if running the embedded Jetty.
The demo shows how to use the API to plot a data sample.
URL: /mbeans/domains[?callback=FUNCTION_NAME], for example mbeans/domains
Returns an array of domain names, for example:
["JMImplementation","com.sun.management","java.lang","java.util.logging"]
URL: /mbeans/domains/DOMAIN[?callback=FUNCTION_NAME], for example mbeans/domains/java.lang
Returns a domain object containing the domain name and a list of mbean names, for example:
{"domain":"java.lang","mbeans":["java.lang:type=MemoryPool,name=PS Eden Space"]}
URL: /mbeans/MBEAN_NAME[?callback=FUNCTION_NAME], for example mbeans/java.lang:type=MemoryPool,name=PS Eden Space
Returns an mbean object containing the name of the mbean and an attribute object containing all attributes and their values, for example:
{"name":"java.lang:type=MemoryPool,name=PS Eden Space", "attributes":{"Name":"PS Eden Space", "Type":"HEAP", "Valid":true}, "operations":[{"operation":"long getThreadAllocatedBytes(long)", "name":"getThreadAllocatedBytes", "returns":"long", "params":["long"]}] }
Only primitive values and their wrapper objects are supported. Otherwise toString will be used on the object.
URL: mbeans/MBEAN_NAME/ATTRIBUTE_NAME[?callback=FUNCTION_NAME], for example mbeans/java.lang:type=MemoryPool,name=PS%20Eden Space/Type
Returns and attribute object with a value, for example:
{"name":"java.lang:type=MemoryPool,name=PS Eden Space", "attribute":"Type", "value":"HEAP"})
Only primitive values and their wrapper objects are supported.
URL: mbeans/MBEAN_NAME/ATTRIBUTE_NAME[?callback=FUNCTION_NAME], for example mbeans/java.lang:type=Memory/Verbose
PUT a text value againts url above. For example to change the Verbose attribute to false with curl:
url -v -i -H "Content-type: text/plain" -X PUT --data-binary "false" \ 'http://localhost:8080/mbeans/java.lang:type=Memory/Verbose'
The returned object will look like a normal attribute return:
{"name":"java.lang:type=Memory","attribute":"Verbose","value":false}
To invoke the PUT method from jquery the following code might be used:
$.ajax({ url: "mbeans/java.lang:type=Memory/Verbose", type: "PUT", data: "false", processData: false, contentType: "text/plain", dataType: "json", success: function(msg){ if(typeof(msg.value) != "undefined") { input.attr("value", msg.value); } else { log("Did not get an attribute JSON back " + msg); } }, error: function(msg, textStatus, errorThrown) { alert("Could not put attribute for " + url + ": " + msg.statusText + "(" + msg.status +") "+ textStatus + " " + errorThrown ); } });
Only primitive values and their wrapper objects are supported as parameters. Returned objects may also be a List or Map with before mentioned types.
URL: mbeans/MBEAN_NAME/ops/METHOD_NAME[?callback=FUNCTION_NAME], for example mbeans/java.lang:type=Threading/ops/getThreadAllocatedBytes'
POST against a method, possibly with a list of simple types, for example:
curl -v -i -H "Content-type: application/json" -X POST --data-binary "{'params': ['1']}" \ 'http://localhost:8080/mbeans/java.lang:type=Threading/ops/getThreadAllocatedBytes'
The returned object will contain the name of the method and any returned value. Simple types and List and Maps with simple types should be handled.
{"name":"java.lang:type=Threading","operation":"getThreadAllocatedBytes","return":598757792}
To invoke the POST method from jquery the following code might be used:
$.ajax({ url: url, type: "POST", data: JSON.stringify({"params": params}), processData: false, contentType: "application/json", dataType: "json", success: function(msg){ alert(msg.return); }, error: function(msg, textStatus, errorThrown) { alert("Could not invoke method " + url + ": " + msg.statusText + "(" + msg.status +") "+ textStatus + " " + errorThrown ); } });