|
|
JMX Builder
JmxBuilder is designed to work with the Java Management Extension (JMX) API. Jmx Builder uses the builder pattern to create an internal DSL in Groovy that facilitates the capturing and exposing of beans for runtime control and management via the MBean server.The Groovy JMX Builder DSL hides the complexity of creating and exporting management beans via trhe JMX API by providing a set of natural constructs to interact with the JMX infrastructure.
Features
- Ability to expose any class instance for management including Java and Groovy classes.
- No need to implement a specific management interface.
- Shield developer from complexity of JMX API
- Use familiar Groovy builder pattern
- Exposes attribute, constructors, operations, parameters
Example
The following example shows how you can use Groovy JMX Builder to expose a regular Java class for management into the MBean Server.
Suppose you have class Greeter that sends out greeting messages:
public class Greeter {
private String name;
private String language = "en";
// ...
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// ...
public String greet(){
String greet = (String) (langs.containsKey(getLanguage()) ?
langs.get(getLanguage()) : langs.get("en"));
return greet + " " + getName();
}
String greet(String person){
return langs.get(language) + " " + person
}
}
Now, to expose an instance of this class as a management bean:
def greeter = new Greeter()
JmxBuilder jmx = new JmxBuilder()
def server = jmx.export(svr){
bean(greeter){
name("greeterDemo:service=greeting")
desc("This service prints a greeting on console")
attribute(name:"name", desc:"name of person to greet", writable:true)
attribute(name:"language", desc:"the language to use", editable:true)
operation(name:"greet", desc:"Sends greeting to user")
operation(name:"greet", desc:"Send greeting to user"){
parameter(type:"java.lang.String", name:"Person", desc:"The person to send greeting to")
}
}
bean(object:greeter, name:"greeterDemo:service=greeting2")
}
And that's is it! Groovy Jmx Builder handles all the boring boilerplates.
Note:
- The export() node is the top outer most node and takes an optional MBean server instance as parameter
- The bean() takes the instance of the object being exposed as parameter
- In this context, the builder requires a valid JMX name
- Each line specifies either an attribute, constructor, and operation
- Both constructor and operation support parameter nodes to describe method params.
JConsole
When you use the JMX Builder to export your bean for management, you can view that information from JConsole. For our sample code above, we would see the following entries in JConsole.
