Java Management Extensions (JMX):
Monitoring Java enabled resources either in distributed or non-distributed environments would have been thorny without JMX. What JMX technology does is that, it exposes these Java resources to be monitored using MBeans or Managed Beans. MBeans are JavaBeans really which represent the Java resources to be monitored and which can be accessed in the J2EE server. Exposing these resources as MBeans allows instrumentation. Consider a typical scenario, where Java developers troubleshoot database connection leaks, which is hard to troubleshoot. But imagine how much relaxing a Java developer would feel if the developer could see total number of connections being opened and closed while the application is running using some graphical tool. JMX technology provides set of tools and APIs, using which one can monitor the JVM resources.
jconsole
If you have installed J2SE 5.0, you already have a graphical tool knows as jconsole. This tool allows us to monitor local and remote JVMs. As discussed already, the technology responsible for all this is the JMX technology.
As said already, MBeans represent JVM resources. First of all, an MBean needs to be registered in the MBean Server. Also, all the MBeans need to have an object name of type javax.management.ObjectName. The JVM application which needs to be monitored provides the MBean Server to the JMX client. Now a tool, such as jconsole (JMX Client) simply connects to the MBean Server, to allow us to monitor the JVM resources.
Database Connection Pooling
Database connection pooling allows us to reuse connections. Bugs in connections are common, when some thread that demands the connection does not return it to the pool, by failing to close the connection after it does it's job. Obviously we need a monitoring tool, which can show the information related to connections, and we can then monitor how the pool is being utilized, and hence find the defective parts of the program.
Hibernate session factory statistics
Hibernate 3 provides, lot of statistics related to the session factory. We need to enable the statistics for the session factory first. The MBeanServerFactory can allow us to access MBeanServer. After creating an instance of StatisticsService (the MBean you want) and giving it the MBeanName , you can register the MBean. Now once you have registered the MBean, you can call various methods on it and retrieve useful information which could range from number of sessions being opened or closed or total number of cached queries successfully or find the performance related to with your queries. All this information helps you to troubleshoot and optimize things in a great way.
How to monitor your web application running locally under Tomcat using jconsole
In Tomcat 5.0 and later versions, JMX support has been enhanced to a great extent. Apart for connection leakages, developers would want to monitor their application in terms of memory usage, garbage collection, total classes being loaded, and other vital information related to queries, which would allow developers to optimize their application. Below are simple steps how one would go about making the web application running under Tomcat to be monitored from jconsole in Windows environment.
1- To enable the local JMX agent, you can set CATALINA_OPTS for JMX (please see below Useful links) in the Tomcat startup script.
2- Start Tomcat and access your web application.
3- Run the jconsole utility and you will see that it has detected all Java applications running locally.
You can also use jconsole <pid> where pid is the process ID of the running JVM. For example see the Windows task manager for Tomcat process ID.
4- Now access different parts of your application to see how jconsole shows the information in various tabs.
5- To monitor the MBean registered in the MBeanServer click on the "MBean" tab of jconsole, and you can see how many sessions are being opened and closed as your application flows. You can also see statistics related to your queries. There is lot of statistics available there, and the scope for monitoring is enormous. Besides all these, you will surely want to see how your application utilizes memory by using the "Memory" tab in jconsole.
Useful links:
http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html
http://www.hibernate.org/216.html
http://irserver.ucd.ie/tomcat-docs/monitoring.html