Ehcache开启JMX支持

简介: Ehcache提供了基于JMX的监控支持,支持对以下几类信息的监控。 CacheManager Cache CacheConfiguration CacheStatistics 按照JMX的规范,为了支持对这几类信息的监控支持,Ehcache分别为它们建立了对应的MBean接口,这些接口都定义在net.sf.ehcache.management包中,分别是CacheManagerMBean、CacheMBean、CacheConfigurationMBean和CacheStatisticsMBean。

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的信息了。



 

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日)

目录
相关文章
|
8月前
|
缓存 NoSQL Java
【Spring】Cache 缓存配置详解
【Spring】Cache 缓存配置详解
296 0
|
存储 NoSQL 安全
Spring Security系列教程21--会话管理之实现集群会话
前言 现在我们已经掌握了如何防御会话固定攻击,处理会话过期,对会话进行并发控制等,但是这些会话处理手段都是针对单机环境下的,在现在的大型项目中,很多时候都是采用分布式开发方案。一旦涉及到分布式方案,就意味着我们的服务器可能会有多台,而我们的项目也可能会根据业务被拆分成了若干个子服务,每个服务又可能被部署在不同的服务器上。这时候问题就来了,以前单台服务器的时候,我们的会话很好管理,现在有多台服务器,那会话岂不是有多个了?这时候我们把服务器集群环境下的会话和单个用户关联起来? 啊啊啊...是不是感觉很复杂! 别害怕!Spring Security其实给我们提供了对应的解决方案,就是 一一哥 今
305 0
|
Java 数据库连接
springboot启动时禁用数据库连接检测
高高兴兴新建一个springboot项目准备写个demo,哟,没配置jdbc还不让我启动成功了 这是因为springboot在启动时会自动检测jdbc连接,关了就行
642 0
springboot启动时禁用数据库连接检测
|
运维 监控
Tomcat 开启JMX监控
如果需要使用Zabbix Java Gateway监控JMX,需要先打开JMX监控接口。
729 0
Tomcat 开启JMX监控
|
Java Spring 数据库
启用 Spring Data JPA 审计功能
突然发现 Spring Data JPA 有这么一个功能,英文是 Auditing JPA Audit 说明 在spring jpa中,支持在字段或者方法上进行注解@CreatedDate、@CreatedBy、 @LastModifiedDate、@LastModifiedBy,从字面意思可以很清楚的了解,这几个注解的用处。
2569 0
|
应用服务中间件
Tomcat开启Debug模式
在bin/catalina.sh中添加如下行,将tomcat重启即可。 注:以下标红的7002需将其改成对象的tomcat端口即可! JAVA_OPTS="-Xms4g -Xmx4g -XX:PermSize=512m -XX:MaxPermSize=1024m -Djava.
3501 0
|
监控 应用服务中间件 Apache
|
Java Spring
springboot jndi禁用
摘要:在实际项目开发中使用springboot的时候,可以使用jar包的方式运行项目,也可以将springboot项目打成war包使用。springboot war包运行可能会出现 [localhost-startStop-1] DEBUG org.
2274 0
|
SQL 关系型数据库 数据库连接
Tomcat数据源的原理,配置及使用(JNDI)
Tomcat数据源的原理,配置及使用 知识点: 1.数据源的作用及操作原理; 2.Tomcat中数据源的配置; 3.数据源的查找及使用     传统JDBC使用过程存在以下四个步骤: 1.加载驱动程序 2.
1385 0