Groovy JMX Builder is now included in the core Groovy distribution and so no longer requires a separate download. I am using Groovy 1.6.1 for the examples in this posting. The GroovyDoc-based documentation for the groovy.jmx.builder package is included with the Groovy 1.6.1 API documentation.
To demonstrate use of Groovy and Groovy JMX Builder to expose a POJO as a JMX MBean, I will first start with a POJO. This class is shown next (I've left out comments for brevity):
HelloJmx.java
package dustin.examples;
public class HelloJmx
{
private String salutation = "Hello";
public HelloJmx() {}
public String hello(final String addressee)
{
return this.salutation + ", " + addressee;
}
public String getSalutation()
{
return this.salutation;
}
public void setSalutation(final String newSalutation)
{
this.salutation = newSalutation;
}
}
This simple Java POJO can be exposed easily as a JMX MBean via Groovy as shown in this Groovy code sample.
exportHelloWorldViaBasicGroovyJmx.groovy
import dustin.examples.HelloJmx
import groovy.jmx.builder.JmxBuilder
JmxBuilder jmxBuilder = new JmxBuilder()
def hello = new HelloJmx()
jmxBuilder.export { bean(hello) }
println "Use JConsole or VisualVM to see exposed Calculator"
println "Press ENTER to exit."
Console console = System.console();
console.readLine()
With Groovy installed on my machine, running this script is easy as shown in the next screen snapshot:

As the screen snapshot indicates, running the Groovy script is very similar to running the Java launcher to run a Java application.
I can run a general JMX client such as JConsole or VisualVM against this Groovy-enabled JMX-exposed application. The next screen snapshot shows how to connect to the Groovy enabled JMX-exposed application on the local machine (it lacks an obvious label, but it is the process that is not JEdit and not JConsole itself).

Once I connect to this Groovy/JMX-enabled application, I can view its MBeans as shown in the next screen snapshot.

Although the
HelloJmx
class provided a setSalutation
method, the Salutation attribute is read-only as shown in the above screen snapshot. This is the default behavior of Groovy's JMX Builder. In addition, the ObjectName of the bean is the default-based jmx.builder.ExportedObject:type=dustin.examples.HelloJmx@11845181
. It would be nice to customize the JMX support a little more. This is easily done with Groovy JMX Builder as I will demonstrate in the remainder of this blog post.
To expose the "setter" for the Salutation attribute in the HelloJmx example, I need to make it "writable." Editing the Groovy script to explicitly state descriptive information about how the Salutation attribute should be exposed makes this possible and is demonstrated in the next code listing.
exportHelloWorldViaAdvanceGroovyJmx.groovy
import dustin.examples.HelloJmx
import groovy.jmx.builder.JmxBuilder
JmxBuilder jmxBuilder = new JmxBuilder()
def hello = new HelloJmx()
jmxBuilder.export
{
bean(target:hello,
name:"dustin.example.hello:type=GroovyJmx",
attributes: [
"Salutation":[desc: "Salutation to use with name.",
readable: true,
writable: true,
defaultValue:"Hello"] ],
operations: "*")
}
println "Use JConsole or VisualVM to see exposed Hello World"
println "Press ENTER to exit."
Console console = System.console();
console.readLine()
In the above Groovy script, I overrode the default ObjectName with the new name of "dustin.example.hello:type=GroovyJmx" ("type" gets special treatment in JConsole). I also expressed more details about the Salutation attribute, including specifying that it is writable. Two screen snapshots showing the results of the attribute metadata description and the customized object name are shown next:


Conclusion
Groovy provides another handy approach for exposing Java objects via JMX for management and monitoring.
Không có nhận xét nào:
Đăng nhận xét