为什么80%的码农都做不了架构师?>>>   

Ehcache中监听器有两种,监听CacheManager的CacheManagerEventListener和监听Cache的CacheEventListener。在Ehcache中,Listener是通过对应的监听器工厂来生产和发生作用的。下面我们将来介绍一下这两种类型的监听器。

1 CacheManager监听器

Ehcache中定义了一个CacheManagerEventListener接口来监听CacheManager的事件。CacheManagerEventListener可以监听的事件有CacheManager添加和移除Cache。其中定义有如下五个方法:

public interface CacheManagerEventListener {  void init() throws CacheException;  Status getStatus();  void dispose() throws CacheException;  void notifyCacheAdded(String cacheName);  void notifyCacheRemoved(String cacheName);  }

init方法会在CacheManagerEventListener实现类实例化后被调用,用于初始化CacheManagerEventListener。
  getStatus方法返回当前CacheManagerEventListener所处的状态,可选值有STATUS_UNINITIALISED、 STATUS_ALIVE和STATUS_SHUTDOWN。
  dispose方法用于释放资源。
  notifyCacheAdded方法会在往CacheManager中添加Cache时被调用。
  notifyCacheRemoved方法会在从CacheManager中移除Cache时被调用。

Ehcache是通过CacheManagerEventListenerFactory来获取当前CacheManager所使用的CacheManagerEventListener的。CacheManagerEventListenerFactory是一个抽象类,其定义如下:

public abstract class CacheManagerEventListenerFactory {  public abstract CacheManagerEventListener  createCacheManagerEventListener(CacheManager cacheManager, Properties properties);  }

在我们自己的CacheManagerEventListenerFactory子类中需要实现其抽象方法createCacheManagerEventListener,在生成对应的CacheManagerEventListener进行返回时我们可以使用当前的CacheManager以及在ehcache.xml文件中定义CacheManagerEventListenerFactory时指定的属性Properties。通过CacheManagerEventListenerFactory我们可以实现为不同的CacheManager使用不同的CacheManagerEventListener。

有了CacheManagerEventListener和CacheManagerEventListenerFactory之后,我们需要在对应的ehcache.xml文件中通过cacheManagerEventListenerFactory元素来指定当前ehcache.xml文件对应的CacheManager所使用的事件监听器工厂,每一个ehcache.xml文件中最多只能指定一个cacheManagerEventListenerFactory元素。

cacheManagerEventListenerFactory元素可以指定三个属性:class、properties和propertySeparator。

class属性必须指定,表示对应的CacheManagerEventListenerFactory实现类全名。
  properties属性可选,用来指定CacheManagerEventListenerFactory在创建CacheManagerEventListener时需要使用的属性,里面是键值对的形式,多个属性之间默认用逗号隔开。如“prop1=val1,prop2=val2”。
  propertySeparator属性可选,用来指定properties属性之间的分隔符。

下面给一个监听CacheManager事件的示例。

1、实现自己的CacheManagerEventListener。

public class MyCacheManagerEventListener implements CacheManagerEventListener {  private final CacheManager cacheManager;  public MyCacheManagerEventListener(CacheManager cacheManager) {  this.cacheManager = cacheManager;  }  @Override  public void init() throws CacheException {  System.out.println("init.....");  }  @Override  public Status getStatus() {  System.out.println("getStatus.....");  returnnull;  }  @Override  public void dispose() throws CacheException {  System.out.println("dispose......");  }  @Override  public void notifyCacheAdded(String cacheName) {  System.out.println("cacheAdded......." + cacheName);  System.out.println(cacheManager.getCache(cacheName));  }  @Override  public void notifyCacheRemoved(String cacheName) {  System.out.println("cacheRemoved......" + cacheName);  }  }  

2、实现自己的CacheManagerEventListenerFactory,根据条件创建对应的CacheManagerEventListener。

public class MyCacheManagerEventListenerFactory extends  CacheManagerEventListenerFactory {  @Override  public CacheManagerEventListener createCacheManagerEventListener(  CacheManager cacheManager, Properties properties) {  returnnew MyCacheManagerEventListener(cacheManager);  }  }  

3、在ehcache.xml文件中通过cacheManagerEventListenerFactory元素指定当前CacheManager所使用的CacheManagerEventListenerFactory为我们自己定义的CacheManagerEventListenerFactory。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  maxBytesLocalHeap="100M">  <diskStore path="d:\\ehcache" />  <cacheManagerEventListenerFactory class="xxx.MyCacheManagerEventListenerFactory"/>  <defaultCache/>  </ehcache>

针对于上述监听器所进行的测试代码如下所示:

@Test
public void testAdd() {  CacheManager cacheManager = CacheManager.create(this.getClass().getResource("/ehcache-listener.xml"));  cacheManager.addCache("test1");  cacheManager.removeCache("test1");
}

2 Cache监听器

Ehcache中定义了一个CacheEventListener接口来监听Cache的事件。其能监听到Cache中元素的添加、删除、更新等。CacheEventListener中主要定义有以下方法:

public interface CacheEventListener extends Cloneable {  void notifyElementRemoved(Ehcache cache, Element element) throws CacheException;  void notifyElementPut(Ehcache cache, Element element) throws CacheException;  void notifyElementUpdated(final Ehcache cache, final Element element) throws CacheException;  void notifyElementExpired(final Ehcache cache, final Element element);  void notifyElementEvicted(final Ehcache cache, final Element element);  void notifyRemoveAll(final Ehcache cache);  public Object clone() throws CloneNotSupportedException;  void dispose();
} 

notifyElementRemoved方法会在往Cache中移除单个元素时被调用,即在调用Cache的remove方法之后被调用。
  notifyElementPut方法会在往Cache中添加元素时被调用。调用Cache的put方法添加元素时会被阻塞,直到对应的notifyElementPut方法返回之后。
  notifyElementUpdated方法,当往Cache中put一个已经存在的元素时就会触发CacheEventListener的notifyElementUpdated方法,此时put操作也会处于阻塞状态,直到notifyElementUpdated方法执行完毕。
  notifyElementExpired方法,当Ehcache检测到Cache中有元素已经过期的时候将调用notifyElementExpired方法。
  notifyElementEvicted方法将会在元素被驱除的时候调用。
  notifyRemoveAll方法将在调用Cache的removeAll方法之后被调用。
  dispose方法用于释放资源。

那我们在实现自己的CacheEventListener时就需要实现上述所有的方法。Ehcache为我们提供了一个默认的空实现CacheEventListenerAdapter,我们可以在实际应用中继承CacheEventListenerAdapter,然后重写其中的某些方法,以简化我们对CacheEventListener的实现。

跟CacheManagerEventListener一样,CacheEventListener不能单独起作用,它需要通过当前Cache相关联的CacheEventListenerFactory来构建一个当前Cache使用的CacheEventListener。CacheEventListenerFactory是一个抽象类,其中只定义了一个createCacheEventListener方法,该方法接收一个Properties对象作为参数。

在ehcahce.xml文件中通过cache元素下的子元素cacheEventListenerFactory可以指定当前Cache所使用的CacheEventListenerFactory。其可以指定四个属性:

class:指定当前CacheEventListenerFactory对应的Java类全名称。
  properties:指定在构建CacheEventListenerFactory时需传入的属性键值对,多个属性之间默认用逗号分开,如:“prop1=value1,prop2=value2”。
  propertySeparator:指定properties中多个属性之间的分隔符。
  listenFor:表示在集群环境下可以监听到的Cache事件的范围,可选值有local、remote和all。local代表只监听本节点的Cache事件,remote代表只监听其他节点的Cache事件,all代表监听所有的Cache事件。默认是all。

与CacheManagerEventListenerFactory不同的是一个Cache可以定义多个CacheEventListenerFactory。

下面来看一个使用Cache监听器的例子。

1、实现一个CacheEventListener。

public class MyCacheEventListener implements CacheEventListener {  @Override  public void notifyElementRemoved(Ehcache cache, Element element)  throws CacheException {  System.out.println("removed");  }  @Override  public void notifyElementPut(Ehcache cache, Element element)  throws CacheException {  System.out.println("put");  }  @Override  public void notifyElementUpdated(Ehcache cache, Element element)  throws CacheException {  System.out.println("updated");  }  @Override  public void notifyElementExpired(Ehcache cache, Element element) {  System.out.println("expired");  }  @Override  public void notifyElementEvicted(Ehcache cache, Element element) {  System.out.println("evicted");  }  @Override  public void notifyRemoveAll(Ehcache cache) {  System.out.println("removeAll");  }  @Override  public void dispose() {  }  public Object clone() throws CloneNotSupportedException {  thrownew CloneNotSupportedException();  }  }  

2、实现抽象工厂类CacheEventListenerFactory来生产前面已经定义好的CacheEventListener。

public class MyCacheEventListenerFactory extends CacheEventListenerFactory {  @Override  public CacheEventListener createCacheEventListener(Properties properties) {  returnnew MyCacheEventListener();  }  } 

3、在ehcache.xml文件中通过cache元素的子元素cacheEventListenerFactory来指定当前Cache使用的CacheEventListenerFactory。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  maxBytesLocalHeap="100M">  <diskStore path="d:\\ehcache" />  <cache name="test">  <cacheEventListenerFactory class="xxx.xxx.MyCacheEventListenerFactory"/>  </cache>  <defaultCache/>  </ehcache>

经过以上三步我们就完成了对Cache事件的监听。

转载于:https://my.oschina.net/mywiki/blog/1542361

Ehcache(06)监听器相关推荐

  1. Ehcache(06)——监听器

    http://haohaoxuexi.iteye.com/blog/2119353 监听器 Ehcache中监听器有两种,监听CacheManager的CacheManagerEventListene ...

  2. ehcache导致Tomcat重启出错

    最近使用ehcache出现了问题,只要在配置文件中打开缓存,Tomcat在重启时就会报内存溢出异常.按说ehcache自己开启的资源,应该自己关闭才是.经查阅资料发现,需要在web.xml中配置一个监 ...

  3. EhCache的特性

    一.特性一览 来自官网,简单翻译一下: 1.快速轻量 过去几年,诸多测试表明Ehcache是最快的Java缓存之一. Ehcache的线程机制是为大型高并发系统设计的. 大量性能测试用例保证Ehcac ...

  4. android 实现自定义监听接口,Android在自定义类中实现自定义监听器方式

    Android在自定义类中实现自定义监听器方式 发布时间:2020-08-31 06:19:39 来源:脚本之家 阅读:203 作者:Simon_Qi 监听器可以说是Android开发中最常用的东西之 ...

  5. 三大缓存框架ehcache、memcache和redis的介绍

    三大缓存框架ehcache.memcache和redis的介绍 2016-04-12 架构说 4964 阅读 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存 ...

  6. Ehcache的基本概念

    基本介绍: Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servl ...

  7. Integrating Spring and EHCache

    为什么80%的码农都做不了架构师?>>>    Using Spring Modules and EHCache, one can transparently cache metho ...

  8. Oracle Lsnrctl - 关于oracle监听器的命令和解释

    转自:http://blog.csdn.net/zjliu1984/archive/2009/06/16/4273304.aspx 对于DBA来说,启动和关闭oracle监听器是很基础的任务,但是Li ...

  9. ehcache memcache redis 三大缓存

    2019独角兽企业重金招聘Python工程师标准>>> 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcach ...

最新文章

  1. 《SQL Server企业级平台管理实践》读书笔记——关于SQL Server数据库的备份方式...
  2. 源代码遭泄露,大疆员工被罚20万,判刑半年。
  3. RAID原理及其使用方法
  4. ITK:将样条曲线拟合到点集
  5. 升级.Net Core RC1的类库项目
  6. 阿里云混合云Apsara Stack 2.0发布 加速政企数智创新
  7. 四大跨平台的APP分析
  8. 支持5G的iPhone SE Plus或将于明年面世
  9. cv::Mat ptr 和 at 注意事项
  10. java的int和Integer的区别
  11. idea每次都要配置tomcat_午饭收藏夹里的c位石锅拌饭,每次来到都要等位......
  12. 聚合四方支付系统架构及所需配置
  13. 程序员一毕业就年薪 110 万竟然是靠……
  14. Error in cool_function[1] : object of type ‘closure‘ is not subsettable
  15. 服务器托管费用怎么计算?
  16. 额。。万恶之源就是c
  17. 数据结构 第2版 第二版 陈越_高中数学选学---人教A版选修2-1第二章第二节椭圆...
  18. windows如何查看内存条型号信息cpu型号信息 # 包括 内存条个数 和 cpu个数
  19. 强化学习入门 Q-learning与SARSA
  20. CSDN上传资源提示:资源上传中断

热门文章

  1. C/C++ 日期 时间 time_t与struct tm转换 收藏
  2. 开放中国农业-国际农民丰收节贸易会:谋定全球共同发展
  3. idea中修改代码大小设置
  4. HTML实现包含公共部分:通过ECMA6的模块化,纯前端实现类似jsp:include的功能
  5. Sqluldr2 libclntsh.so报错处理
  6. 网狐荣耀源码(含内核源码)可二次开发
  7. Android通过PHP服务器实现登录
  8. 谷歌雇程序员提升开源安全
  9. hdu 1175 连连看
  10. WinCE驱动编写小结(转载)