Given that Groovy sits directly on top of Java, Groovy can leverage the tremendous amount of work already done for JMX with Java. In addition, Groovy provides a
GroovyMBean class which makes an MBean look like a normal Groovy object. This simplifies Groovy code for interacting with MBeans. For example, the following code:
can be simplified to:
Monitoring the JVM
MBeans are not accessed directly by an application but are managed by a repository called an MBean server. Java 5 and above includes a special MBean server called the platform MBean server, which is built into the JVM. Platform MBeans are registered in this server using unique names.
You can monitor the JVM through its platform MBeans with the following code:
When run, you will see something like this:
You can also use Spring to automatically register beans as JMX aware.
Here is an example class (Calculator.groovy):
Here is the Spring configuration file (beans.xml):
Here is a script which uses this bean and configuration:
And here is the resulting output:
You can even attach to the process while it is running with jconsole. It will look something like:
We started the Groovy application with the
-Dcom.sun.management.jmxremote JVM argument using a Java 5 JVM.
See also: Leveraging Spring
- Monitoring the Java Virtual Machine
- Using Groovy for System Management
- Starting MX4J Server from Spring with registered HttpAdaptor
- XML Messaging Using JBoss
- Spring JMX Documentation
- JMX Scripts using JRuby - Part I
- JMX Scripts using JRuby - Part II
- Groovier jconsole!
- JMX Scripts with Eclipse Monkey
- Getting an MBeanServer reference with OC4J