Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 28 Next »

Introduction

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:

The remainder of this page shows you how to:

  • Monitor the JVM using MXBeans
  • Monitor Apache Tomcat and display statistics
  • Monitor Oracle OC4J and display information
  • Monitor BEA WebLogic and display information
  • Leverage Spring's MBean annotation support to export your Groovy beans as MBeans

Note: many of the examples on this page use Java 5 which incorporates JMX 1.2 (more recent versions if JMX or Java will also work). In some cases, you can run some of these examples using Java 1.4 by including a version of JMX on your CLASSPATH. MX4J is bundled with the full distribution of Groovy. In most cases, you can delete this jar from your distribution lib directory if you are running with Java 5 or above (in fact you might have to - see the Troubleshooting section below).

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:


Monitoring Tomcat

First start up Tomcat with JMX monitoring enabled by setting the following:


You can do this in your startup script and may choose any available port - we used 9004.
The following code uses JMX to discover the available MBeans in the running Tomcat, determine which are web modules, extract the processing time for each web module and displays the result in a graph using JFreeChart:


When run, we will see a trace of progress being made:


The output will look like this:

OC4J Example

Here is some code to access OC4J and print out some information about the server, its runtime and (as an example) the configured JMS destinations:


Here is the result of running this script:


As a slight variation, this code displays a pie chart of memory usage using JFreeChart:


Which looks like:

WebLogic Example

This script prints out information about the server followed by information about JMS Destinations (as an example). Many other mbeans are available.

Here is the output:

Spring Example

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:

Further JMX Information

Links:

Troubleshooting

groovy.lang.MissingMethodException

If you get an error like this

groovy.lang.MissingMethodException: No signature of method:
javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.queryMBeans()
is applicable for argument types: (javax.management.ObjectName, null)
values: {Catalina:*, null}

you have to move away or delete "mx4j-*.jar" from "$GROOVY_HOME/lib". MX4J is designed to add javax.management classes to 1.4 JVMs. If you already have a newer JMX jar on your classpath or are using a Java 5 or higher JVM, the MX4J classes will be incompatible with the ones from the newer Sun JVMs or newer versions of JMX.

java.lang.SecurityException

If you get the following error, your container's JMX access is password protected:

 java.lang.SecurityException: Authentication failed! Credentials required

To fix that, add an environment with the credentials when connecting, like this (password has to be set before that):

def jmxEnv = null
if (password != null)
    jmxEnv = [(JMXConnector.CREDENTIALS): (String[])["monitor", password]]
def connector = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), jmxEnv)

If you still have trouble, the software you are trying to monitor might be using an older mbean server and you will have to consult its documentation for details on how to provide credentials.

  • No labels