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.

Using Eclipse Dali with Hibernate as Persistence Provider

Eclipse Ganymede comes with in built support for the EclipseLink JPA implementation. However, we’ll want to explore how to use Hibernate’s JPA implementation with Eclipse Dali for JPA operations

To begin with, please make sure you have the following

  1. Eclipse Ganymede for JavaEE

  2. Hibernate EntityManager

  3. Hibernate Tools

  4. MySQL connector (or a jdbc connector of your choice if you are not using MySQL)

You will need to install Hibernate Tools as an Eclipse plugin. Next, you’ll have to add MySQL to your list of driver definitions. This you do by going to the “Window” → “Preferences”. On the left tree, navigate to “Data Management” → “Connectivity” → “Driver Definitions”. Click on the “Add” button. You now have a “New Driver Definition” window with three tabs – Name/Type, Jar List and Properties.


In the Name/Type tab, select MySQL. A list of choices now appears of “MySQL JDBC Driver”. Select System Version 5.0, click on the “Jar List” tab and delete the default MySQL driver file specified there. Click on “Add Jar/Zip” and add your MySQL connector. Please ensure that there are no spaces in the path to your MySQL connector, or you may have trouble later on. Click “Ok” to finish the driver definition.

Next is to add the Hibernate EntityManager libraries as your JPA implementation. First, extract the Hibernate EntityManager zip that you downloaded into a location without spaces in the path. Still in the “Preferences” window, click on “JPA” on the left tree. In the “Default JPA Implementation Library”, click the “Configure user libraries” link. Click on the “New” button and give this a good name e.g. HibernateJPA. Click on “Add JARs” and add the “hibernate-entitymanager.jar” that is available in your extracted Hibernate EntityManager folder. Next, add all the jars in the lib folder of the extracted folder. These should be

  • slf4j-api.jar
  • dom4j.jar
  • ejb3-persistence.jar
  • hibernate-annotations.jar
  • hibernate-commons-annotations.jar
  • hibernate-core.jar
  • javassist.jar
  • jta.jar


Click on “Ok” to complete adding the JPA implementation.

Now, create a Java Project called “jpaproject”. In the project explorer view, right-click the project and select “JPA Tools” → “Convert To JPA Project”. Click on “Next” on the “Project Facets” screen that shows up. On the “Configure JPA Settings” window, select “Hibernate” as your platform. You will now add your connection to the database by clicking on “Add connection” link. In the “Connection Profile” window, type MySQL in the filter. Select MySQL and enter the name “jpaprojectDB” in the “Name” field. Click “Next”, and specify your database connection details. Click “Test connection” to be sure, and then “Finish” when completed.

You should now have something like below


Remember to uncheck the “Create orm.xml” option. Its useless overhead we don’t need now. You now have a project that supports JPA, with a META-INF/persistence.xml file generated in your src folder. Funny enough, after all this information provided, Dali still refuses to populate your persistence.xml file with these details. You will have to do them yourself (talk about DRY).

Double-click on your persistence.xml. On the “General” tab, specify “org.hibernate.ejb.HibernatePersistence” as your Persistence Provider. In the “Connection” tab, choose “Resource Local” from the Transaction Type drop down list. Under “Hibernate” tab, select “MySQL” as Database dialect, “com.mysql.jdbc.Driver” as Driver class, “jdbc:mysql://localhost:3306/jaccra” as your connection URL, and then provided the username and password fields for the database connection. Click on “Source” and you should something close to the ff.


<?xml
version=“1.0”
encoding=“UTF-8”?>

<persistence version=“1.0” xmlns=http://java.sun.com/xml/ns/persistence&#8221;
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&#8221;
>

<persistence-unit name=“jpaproject” transaction-type=“RESOURCE_LOCAL”>

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

<property name=“hibernate.dialect” value=“org.hibernate.dialect.MySQLDialect”/>

<property name=“hibernate.connection.driver_class” value=“com.mysql.jdbc.Driver”/>

<property name=“hibernate.connection.url” value=“jdbc:mysql://localhost:3306/jaccra”/>

<property name=“hibernate.connection.username” value=“edem”/>

<property name=“hibernate.connection.password” value=“edem”/>

</properties>

</persistence-unit>

</persistence>

Now you are all set up for your JPA machinations. One last and very important thing to do is to add the MySQL connect jar to your build path. Now when you right-click on your project in the project explorer, you should see an option “JPA Tools”, under which there are a couple of other options. Explore them and see what you get.