Ehcache開啓JMX支持

Ehcache提供了基於JMX的監控支持,支持對以下幾類信息的監控。
* CacheManager
* Cache
* CacheConfiguration
* CacheStatistics

按照JMX的規範,爲了支持對這幾類信息的監控支持,Ehcache分別爲它們建立了對應的MBean接口,這些接口都定義在net.sf.ehcache.management包中,分別是CacheManagerMBean、CacheMBean、CacheConfigurationMBean和CacheStatisticsMBean。按照JMX的規範,JMX監控頁面只能查看MBean中定義的屬性(get方法)和執行MBean接口中定義的操作(方法)。比方說CacheManagerMBean的定義如下,它能看到的屬性就是status、name、cacheNames、caches、transactionCommittedCount、transactionRolledBackCount和transactionTimedOutCount。能進行的操作是shutdown()和clearAll()。

public interface CacheManagerMBean {

    /**
     * Gets the status attribute of the Ehcache
     *
     * @return The status value, as a String from the Status enum class
     */
    String getStatus();

    /**
     * Gets the name of the cache manager
     *
     * @return The name of the CacheManager
     */
    String getName();

     /**
     * Shuts down the CacheManager.
     * <p/>
     * If the shutdown occurs on the singleton, then the singleton is removed, so that if a singleton access method
     * is called, a new singleton will be created.
     */
    void shutdown();


    /**
     * Clears  the contents of all caches in the CacheManager, but without
     * removing any caches.
     * <p/>
     * This method is not synchronized. It only guarantees to clear those elements in a cache
     * at the time that the {@link net.sf.ehcache.Ehcache#removeAll()} mehod  on each cache is called.
     */
    void clearAll();


    /**
     * Returns a JMX Cache bean
     *
     */
    Cache getCache(String name);


    /**
     * Gets the cache names managed by the CacheManager
     */
    String[] getCacheNames() throws IllegalStateException;

    /**
     * Gets a list of caches in this CacheManager
     * @return a list of JMX Cache objects
     */
    List getCaches();

    /**
     * Get the committed transactions count
     * @return the committed transactions count
     */
    long getTransactionCommittedCount();

    /**
     * Get the rolled back transactions count
     * @return the rolled back transactions count
     */
    long getTransactionRolledBackCount();

    /**
     * Get the timed out transactions count. Note that only transactions which failed to
     * commit due to a timeout are taken into account
     * @return the timed out transactions count
     */
    long getTransactionTimedOutCount();

}

註冊這些MBean,Ehcache提供了一個工具類,ManagementService,可以通過它的registerMBeans方法進行註冊,該方法定義如下,後面對應的4個boolean類型的參數,表示是否需要註冊對應的MBean,依次表示CacheManager、Cache、CacheConfiguration和CacheStatistics。該工具方法最終會生成一個ManagementService實例,ManagementService實現了CacheManagerEventListener接口,所以它能感知到Cache的變化。

public static void registerMBeans(
    net.sf.ehcache.CacheManager cacheManager,
    MBeanServer mBeanServer,
    boolean registerCacheManager,
    boolean registerCaches,
    boolean registerCacheConfigurations,
    boolean registerCacheStatistics) throws CacheException {
//...
}

以下是一個註冊Ehcache對應的MBean的示例代碼:

CacheManager cacheManager = new CacheManager();
String cacheName = "test";
Ehcache cache = cacheManager.addCacheIfAbsent(cacheName);
cache.put(new Element("key is a object", "value is a object"));

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);

註冊之後我們就可以通過jconsole的JMX界面看到對應的MBean的信息了。

JMX

Ehcache相關的MBean的objectName的命名規範如下:
* CacheManager - “net.sf.ehcache:type=CacheManager,name=<CacheManager>”
* Cache - “net.sf.ehcache:type=Cache,CacheManager=<cacheManagerName>,name=<cacheName>”
* CacheConfiguration - “net.sf.ehcache:type=CacheConfiguration,CacheManager=<cacheManagerName>,name=<cacheName>”
* CacheStatistics - “net.sf.ehcache:type=CacheStatistics,CacheManager=<cacheManagerName>,name=<cacheName>”

參考文檔
http://www.ehcache.org/documentation/2.7/operations/jmx.html

(本文是基於Ehcache2.10.4所寫,由Elim寫於2017年10月8日)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章