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日)
相关推荐
在Ehcache的配置文件(通常是ehcache.xml)中,我们可以设置监控相关的参数,例如开启JMX注册、设置日志级别等。例如: ```xml <cacheManagerEventListenerFactory class="net.sf.ehcache.distribution....
8. **JMX支持**:Ehcache默认开启JMX功能,允许通过JMX管理工具监控和管理CacheManager、Cache和CacheConfiguration等MBean,实现远程管理和监控。 9. **安全性**:虽然在描述中未提及,但Ehcache还支持安全配置,...
8. **JMX 监控**:EHCache 默认开启了 JMX 功能,通过 JMX 可以监控和管理 `CacheManager`、`Cache`、`CacheConfiguration`、`CacheStatistics` 等 MBean,从而实现远程监控和管理缓存状态。 9. **分布式缓存**:从...
Ehcache提供了内存和磁盘存储、缓存分区、缓存复制等多种功能,同时支持JMX管理,使得在复杂的系统环境中可以方便地监控和调整缓存行为。 **MyBatis二级缓存** 是在一级缓存(SqlSession级别的缓存)基础上提供的一...
7. **监听器与JMX支持**:Ehcache内置了丰富的监听器功能,包括缓存管理器监听器与缓存事件监听器,分别用于监控缓存的增删改查及过期事件。同时,Ehcache默认开启了JMX功能,允许对CacheManager、Cache、Cache...
- 使用Ehcache提供的管理工具或JMX监控缓存的使用情况,如缓存命中率、大小、存取时间等。 - 在开发阶段,可以通过开启Ehcache的日志输出,以便调试和排查问题。 8. **异常处理** - 针对可能出现的缓存未命中或...
Ehcache是一种广泛使用的开源缓存解决方案,它提供了内存和磁盘存储,并且支持JMX管理。在Hibernate中配置Ehcache,需要在`hibernate.cfg.xml`配置文件中添加相应的provider类和缓存配置: ```xml ...
2. Keep-Alive处理:开启-Connector/keep-alive-timeout以控制空闲连接的存活时间,避免资源浪费。 3. 端口绑定与最大连接数:调整Connector/port和maxConnections参数,确保足够处理请求。 四、Web应用配置 1. ...
使用 Ehcache 或 Infinispan 等实现,注意数据同步与一致性问题。 - 查询缓存:缓存SQL查询结果,减少数据库查询,但更新操作可能导致缓存失效,需谨慎使用。 2. **批处理与连接池**: - 批量操作:批量插入、...