HibernateJMX


Here are some instructions for making the Hibernate Statistics JMX MBean available from AppFuse:

The example below has been tested with MX4J 3.0.1, JDK 1.5 Update 2 and Tomcat 5.5.7.

Note: Using JDK 1.5 with the JAVA_OPTS and JConsole is optional, most app servers already have a MBean server, and MC4J (http://mc4j.sourceforge.net/) provides a JMX Console for most of them.

Inside the tomcat startup script, ensure these JAVA_OPTS are set:

JAVA_OPTS=" -Dcom.sun.management.jmxremote.port=9002 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.awt.headless=true"

(Note: Under Windows, I started tomcat5w.exe and set the above properties in the Java tab, in the textfield entitled 'Java Options').

Then, if you are using Spring, add this to your applicationContext-hibernate.xml file:

    <bean id="jmxExporter"
        class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="Hibernate:name=statistics">
                    <ref local="statisticsBean" />
                </entry>
            </map>
        </property>
    </bean> 

    <bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled">
            <value>true</value>
        </property>
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

and then set the hibernate.generate_statistics property in your applicationContext-hibernate.xml file:

	<prop key="hibernate.generate_statistics">true</prop>

When you browse the MBeans (using JConsole.exe - which lives in the bin dir of your JDK 1.5 distribution), you should see Hibernate, if you double click on that node, you should see the Statistics Bean.

Tomcat 5 already contains the MX4J libraries and your AppFuse project should already have the Hibernate libraries in the runtime classpath. Below are a couple of screenshots.




If you are not using Spring, inside StartupListener.java (inside contextInitialized method) add:


importorg.hibernate.jmx.*;
importjavax.management.*;
importjava.lang.management.ManagementFactory;

try{
SessionFactorysessionFactory=(SessionFactory)applicationContext.getBean("sessionFactory");
MBeanServermbeanServer=
ManagementFactory.getPlatformMBeanServer();
ObjectNameon=
newObjectName("Hibernate:type=statistics,application=appfuse");
StatisticsServicemBean=newStatisticsService();
mBean.setStatisticsEnabled(true);
mBean.setSessionFactory(sessionFactory);
mbeanServer.registerMBean(mBean,on);
}
catch(Exceptione){
log.error("ErrorregisteringHibernateStatisticsService["+e.getMessage()+"]",e);
}

After starting up AppFuse, you should be able to attach to the server using JConsole (port 9002, as in the config above). From there, you should see the Hibernate MBean where you can view the statistics that have been collected.



不过我没跑过,如果你能跑起来,请留言,请教下,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值