以tomcat为例,加入一个listner监听容器启动:
<listener> <description>ServletContextListener</description> <listener-class>com.my.listener.StartupListener</listener-class> </listener>
Listener中的内容:
@Override public void contextInitialized(ServletContextEvent sce) { SessionFactory sf = HibernateUtil.getSessionFactory(); MBeanServer ms = ManagementFactory.getPlatformMBeanServer(); Hashtable tb = new Hashtable(); tb.put("type", "statistics"); tb.put("sessionFactory", "myHibernateApp"); try { ObjectName on = new ObjectName("hibernate",tb); StatisticsService mBean = new StatisticsService(); mBean.setSessionFactory(sf); mBean.setStatisticsEnabled(true); ms.registerMBean(mBean, on); } catch (Exception ex) { Logger.getLogger(StartupListener.class.getName()).log(Level.SEVERE, null, ex); } }
tomcat启动需要添加一行jvm参数:-Dcom.sun.management.jmxremote.port=1090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
用来启动tomcat的jmx服务管理。
启动后通过jconsole访问tomcat的jmx监听端口。