2016-11-06 110 views
0

我在Spring + Hibernate中實現了應用程序。爲了優化ORM操作我都接着this教程,使監測的Ehcache:通過JMX監控Ehcache

@EnableWebMvc 
@EnableSpringDataWebSupport 
@EnableCaching 
... 
public class SpringWebConfig extends WebMvcConfigurerAdapter { 

    .... 

    @Bean 
    public EhCacheCacheManager ehCacheManager() { 
     EhCacheCacheManager cacheManager = new EhCacheCacheManager(); 
     cacheManager.setCacheManager((net.sf.ehcache.CacheManager) ehCacheManagerFactory().getObject()); 

     return cacheManager; 
    } 

    @Bean 
    public EhCacheManagerFactoryBean ehCacheManagerFactory() { 
     EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); 
     cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); 
     cmfb.setShared(true); 
     return cmfb; 
    } 

    @Bean 
    public ManagementService managementService() { 
     return new ManagementService(ehCacheManager().getCacheManager(), mBeanServer(), true, true, true, true); 
    } 


    @Bean 
    public MBeanServer mBeanServer() { 
     MBeanServer bean = ManagementFactory.getPlatformMBeanServer(); 
     return bean; 
    } 
} 

但是我看不到任何ehache豆在JConsole:

enter image description here

你知道,如果我錯過了什麼,以啓用統計?

回答

1

問題來自於您只是簡單地創建了ManagementService而不是初始化它。

您的managementService方法需要調用創建的ManagementService上的init()方法。

除非你切實需要訪問ManagementService用於其他目的,揭露它作爲一個bean不是必需的,你可以通過靜態ManagementService.registerMBeans(CacheManager, MBeanServer, boolean, boolean, boolean, boolean)無論是ehCacheManagerFactory()ehCacheManager()方法內使用替換此。

靜態方法創建ManagementService並初始化它。

+0

是的,它的工作。感謝您的解釋! – bontade