Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up writing the following class</p> <p><strong>HibernateStatisticsJmxRegistration</strong> </p> <pre><code>import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; import javax.persistence.EntityManagerFactory; import org.hibernate.SessionFactory; import org.hibernate.ejb.HibernateEntityManagerFactory; import org.hibernate.jmx.StatisticsService; import org.springframework.beans.factory.annotation.Autowired; /** * Provides code to register Hibernate's statistics bean with a JMX MBean server. Assumes that both * the MBeanServer and the EntityManagerFactory are available as Spring-managed beans. Note that * while registering this class enables the collection of statistics even if that was previously * disabled. * &lt;p&gt; * May become obsolete once &lt;a href="https://hibernate.onjira.com/browse/HHH-6034"&gt;HHH-6034&lt;/a&gt; is * implemented. Even if not the confusing situation abround the meanwhile deprecated * {@link StatisticsService} should be clear then. */ @SuppressWarnings({"deprecation", "javadoc" }) public class HibernateStatisticsJmxRegistration { @Autowired private EntityManagerFactory entityManagerFactory; @Autowired private MBeanServer mbeanServer; private ObjectName objectName; private String jmxObjectName = "org.hibernate:name=HibernateStatistics"; /** * Registers the statistics MBean that wraps a Hibernate session factory. The bean is registered * under the name provided by {@link HibernateStatisticsJmxRegistration#getJmxObjectName()}. * * @throws JMException if anything fails.. * @see HibernateStatisticsJmxRegistration#unregister() */ public void register() throws JMException { final SessionFactory sessionFactory = ((HibernateEntityManagerFactory) entityManagerFactory).getSessionFactory(); objectName = new ObjectName(jmxObjectName); final StatisticsService statsMBean = new StatisticsService(); statsMBean.setSessionFactory(sessionFactory); statsMBean.setStatisticsEnabled(true); mbeanServer.registerMBean(statsMBean, objectName); } /** * Unregisters the MBean that was registered. * * @throws JMException if the de-registration fails * @see HibernateStatisticsJmxRegistration#register() */ public void unregister() throws JMException { mbeanServer.unregisterMBean(objectName); } /** * Override the default JMX object name. Obviously you need to call this method before * registration for it to have any effect. The string must comply to the rules described in * {@link ObjectName}. Suggested is {@code &lt;domain&gt;:name=&lt;name&gt;}. * * @param jmxObjectName the name to use during registration */ public void setJmxObjectName(String jmxObjectName) { this.jmxObjectName = jmxObjectName; } } </code></pre> <p><strong>Spring configuration</strong></p> <pre><code>&lt;!-- Setting up Ehcache manager for various caches (offer facade, images). --&gt; &lt;bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt; &lt;property name="configLocation" value="classpath:ehcache.xml" /&gt; &lt;/bean&gt; &lt;ehcache:annotation-driven cache-manager="ehCacheManager" /&gt; &lt;!-- Exposing cache statistics through JMX. --&gt; &lt;context:mbean-server /&gt; &lt;bean class="net.sf.ehcache.management.ManagementService" init-method="init"&gt; &lt;constructor-arg ref="ehCacheManager"/&gt; &lt;constructor-arg ref="mbeanServer"/&gt; &lt;constructor-arg value="true"/&gt; &lt;constructor-arg value="true"/&gt; &lt;constructor-arg value="true"/&gt; &lt;constructor-arg value="true"/&gt; &lt;/bean&gt; &lt;bean class="HibernateStatisticsJmxRegistration" init-method="register" destroy-method="unregister" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload