Lightweight Application Monitoring with Hawtio and JMX.

In my previous life developing solutions for a biometrics company, we invested quite a bit in monitoring tools to know the status and performance of the cluster of servers on which our application was deployed. The tool of choice was RHQ 4.x, and I must say it served us well in many places. But it came with it’s own complexity, and having discovered the simple but quite efficient HawtIO, I think I have a new permanent member of my arsenal for software delivery and monitoring.

Hawtio is a modular web console that enables you to monitor any JVM based application via JMX, with the use of the Jolokia, a tool for exposing JMX via JSON to enable REST like invocation of JMX beans. All you need to do to get Hawtio working on Jboss7/Wildfly is to follow the instructions here.

My main interest was in profiling the performance of Hibernate within my JavaEE application deployed in JBoss 7, and Markus Eisele’s post on using HawtIO to display Hibernate statistics was just on point. Basically you can follow his instructions and make the following modifications for Jboss7/Wildfly.

1. Include these 2 files from Makus’s post in your project StatisticsService and DelegatingStatisticsService
2. Add a @Singleton @Startup EJB which does the following

@Startup
@Singleton
public class HibernateMBeanRegistrar {
    private static final Logger logger = LoggerFactory.getLogger(HibernateMBeanRegistrar.class);
  
    @javax.annotation.Resource(lookup = "java:jboss/MySessionFactory")
    private SessionFactory sessionFactory;
    
    @Inject 
    public void register() {
        try {
            try {
                MBeanServer mbeanServer
                        = ManagementFactory.getPlatformMBeanServer();
                ObjectName on
                        = new ObjectName("Hibernate:type=statistics,application=hibernatestatistics");

                StatisticsService mBean = new DelegatingStatisticsService(sessionFactory.getStatistics());
                mbeanServer.registerMBean(mBean, on);
                logger.info("Hibernate Statistics MBean registered successfully ...");
            } catch (MalformedObjectNameException ex) {
                logger.error("", ex);
            } catch (InstanceAlreadyExistsException ex) {
                logger.error("", ex);
            } catch (MBeanRegistrationException ex) {
                logger.error("", ex);
            } catch (NotCompliantMBeanException ex) {
                logger.error("", ex);
            }
        } catch (Exception e) {
            logger.error("Failure obtaining SessionFactory from EMF: "+e.getLocalizedMessage());
        }

    }
}

3. Add these to your persistence.xml

<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.session_factory_name" value="java:jboss/MySessionFactory" /> 

4. Deploy your application alongside the hawtio-no-slf4j-x.x.x.war as stated in the instructions on installing hawtio for JBoss7/Wildfly. I renamed the file to hawtio to make life easier.
5. Once both hawtio and your application are deployed, navigate to http://localhost:8080/hawtio. Click on JMX and you should see a Hibernate–>statistics–>hibernatestatistics node. Clicking on that node should show you something like Markus’s view here. You can even add it to the dasboard if you wish so you get up to the minute figures as your application runs.

 

HIbernate Statistics

For me it did show that although my entities were being cached in the 2nd level cache, it seemed somehow that query caching was not working. So I’ve got work to do.

Hawtio does have a lot of plugins to display content from other sources, from Elasticsearch to log files. You should definitely give it a shot. Thanks Markus for the Hawtio intro.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s