Dashboard > Grails > ... > Tutorials > MBean export in Grails how to
MBean export in Grails how to Log In | Sign Up   View a printable version of the current page.

Added by Federico Grilli , last edited by Federico Grilli on Mar 03, 2008  (view change)
Labels: 

Introduction

This short tutorial will show you how to export an MBean in Grails. The example uses Hibernate StatisticsService but can be applied to any MBean.

What is a MBean?

In a nutshell, an MBean or Managed Bean is a Java object that represents a resource to be managed. In our case this object exposes statistics for the Hibernate SessionFactory of our Grails application.
For an introduction to MBeans and the related JMX technology, you can have a look at this article.

Getting started

To export an MBean to an MBeanServer we will leverage Spring's JMX powerful support. As Grails is based on Spring, we have access, when needed, to Spring's configuration mechanism.

In grails-app/conf/spring/resources.xml add the following two bean definitions

grails-app/conf/spring/resources.xml
...
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
 	<property name="locateExistingServerIfPossible" value="true" />
 </bean>

  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
   <property name="server" ref="mbeanServer"/>
    <property name="beans">
      <map>
         <entry key="org.hibernate:name=statistics">
               <bean class="org.hibernate.jmx.StatisticsService">
                   <property name="statisticsEnabled" value="true" />
                   <property name="sessionFactory" ref="sessionFactory" />
               </bean>
           </entry>
      </map>
    </property>
  </bean>
...

MBeanServer

The first definition tells Spring to locate a running MBeanServer. If this server already exists, as it could be the case when your application is deployed in a container such as Tomcat, Spring will register the MBean with it. Else, Spring will create one for us at application start up.

MBeanExporter

The second definition tells Spring to instantiate an MBeanExporter object, which will automatically register our MBeans with the MBeanServer. As you can see we are
setting two properties:

The first one, server, refers to the MBeanServer defined above.

The second one, beans, is a map of MBeans. The key is a string representing MBean's name (ObjectName in JMX parlance). It has the following elements in order:

  • The domain
  • A colon :
  • A key property list
Info
The domain is used to avoid collisions between MBeans supplied by different vendors and follows the same convention as for Java-language package names.
In our case we have org.hibernate as the domain followed by a colon and a property name whose value is statistics.
Warning
You must specify the key properties, otherwise the MBean registration will fail with the following error 'javax.management.MalformedObjectNameException: Key properties cannot be empty'.
Refer to javax.management.ObjectName javadoc for further details.

The MBean is an instance org.hibernate.jmx.StatisticsService which should already be in the Grails classpath, as it is comes with Hibernate.
We need to set two properties there: statisticsEnabled and sessionFactory.
The latter refers to the Hibernate SessionFactory bean already defined by Grails with the id sessionFactory.

Done

To test our configuration and see Hibernate Statistics we just need to enable the MBeanServer (also called JMX Agent) when we start the JVM running our Grails application. This can be accomplished by setting the environment variable JAVA_OPTS=-Dcom.sun.management.jmxremote. This will enable local monitoring of your MBeans. To enable remote monitoring and to learn about other options concerning monitoring and management using JMX, please see http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html

Update
I added a page explaining how to do the same configuration the Groovy way, which has at least one advantage over the xml approach.
Read here to know more http://docs.codehaus.org/display/GRAILS/MBean+export+the+Groovy+way

See the MBean with jconsole

We can see our exported MBean with a JMX-compliant monitoring tool such as jconsole, which comes with JDK 5.0 or superior.

  1. Run jconsole at the command line. You should see something like this
  2. Connect to the JVM running your application and click on the MBeans tab. You should see Hibernate's MBean as in the figure below
  3. Click on operations and invoke the logSummary() method, to log the main statistics at info level. Here is an output example
Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators