http://haohaoxuexi.iteye.com/blog/2119353

监听器

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

1       CacheManager监听器

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

  1. public interface CacheManagerEventListener {
  2.  
  3. void init() throws CacheException;
  4.  
  5. Status getStatus();
  6.  
  7. void dispose() throws CacheException;
  8.  
  9. void notifyCacheAdded(String cacheName);
  10.  
  11. void notifyCacheRemoved(String cacheName);
  12.  
  13. }

l  init方法会在CacheManagerEventListener实现类实例化后被调用,用于初始化CacheManagerEventListener。

l  getStatus方法返回当前CacheManagerEventListener所处的状态,可选值有STATUS_UNINITIALISEDSTATUS_ALIVESTATUS_SHUTDOWN

l  dispose方法用于释放资源。

l  notifyCacheAdded方法会在往CacheManager中添加Cache时被调用。

l  notifyCacheRemoved方法会在从CacheManager中移除Cache时被调用。

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

  1. public abstract class CacheManagerEventListenerFactory {
  2.  
  3. public abstract CacheManagerEventListener
  4. createCacheManagerEventListener(CacheManager cacheManager, Properties properties);
  5.  
  6. }

在我们自己的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。

l  class属性必须指定,表示对应的CacheManagerEventListenerFactory实现类全名。

l  properties属性可选,用来指定CacheManagerEventListenerFactory在创建CacheManagerEventListener时需要使用的属性,里面是键值对的形式,多个属性之间默认用逗号隔开。如“prop1=val1,prop2=val2”。

l  propertySeparator属性可选,用来指定properties属性之间的分隔符。

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

1、实现自己的CacheManagerEventListener。

  1. public class MyCacheManagerEventListener implements CacheManagerEventListener {
  2.  
  3. private final CacheManager cacheManager;
  4.  
  5. public MyCacheManagerEventListener(CacheManager cacheManager) {
  6. this.cacheManager = cacheManager;
  7. }
  8.  
  9. @Override
  10. public void init() throws CacheException {
  11. System.out.println("init.....");
  12. }
  13.  
  14. @Override
  15. public Status getStatus() {
  16. System.out.println("getStatus.....");
  17. returnnull;
  18. }
  19.  
  20. @Override
  21. public void dispose() throws CacheException {
  22. System.out.println("dispose......");
  23. }
  24.  
  25. @Override
  26. public void notifyCacheAdded(String cacheName) {
  27. System.out.println("cacheAdded......." + cacheName);
  28. System.out.println(cacheManager.getCache(cacheName));
  29. }
  30.  
  31. @Override
  32. public void notifyCacheRemoved(String cacheName) {
  33. System.out.println("cacheRemoved......" + cacheName);
  34. }
  35.  
  36. }

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

  1. public class MyCacheManagerEventListenerFactory extends
  2. CacheManagerEventListenerFactory {
  3.  
  4. @Override
  5. public CacheManagerEventListener createCacheManagerEventListener(
  6. CacheManager cacheManager, Properties properties) {
  7. returnnew MyCacheManagerEventListener(cacheManager);
  8. }
  9.  
  10. }

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

  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  3. maxBytesLocalHeap="100M">
  4.  
  5. <diskStore path="d:\\ehcache" />
  6.  
  7. <cacheManagerEventListenerFactory class="xxx.MyCacheManagerEventListenerFactory"/>
  8.  
  9. <defaultCache/>
  10.  
  11. </ehcache>

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

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

2       Cache监听器

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

  1. public interface CacheEventListener extends Cloneable {
  2.  
  3. void notifyElementRemoved(Ehcache cache, Element element) throws CacheException;
  4.  
  5. void notifyElementPut(Ehcache cache, Element element) throws CacheException;
  6.  
  7. void notifyElementUpdated(final Ehcache cache, final Element element) throws CacheException;
  8.  
  9. void notifyElementExpired(final Ehcache cache, final Element element);
  10.  
  11. void notifyElementEvicted(final Ehcache cache, final Element element);
  12.  
  13. void notifyRemoveAll(final Ehcache cache);
  14.  
  15. public Object clone() throws CloneNotSupportedException;
  16.  
  17. void dispose();
  18. }

l  notifyElementRemoved方法会在往Cache中移除单个元素时被调用,即在调用Cache的remove方法之后被调用。

l  notifyElementPut方法会在往Cache中添加元素时被调用。调用Cache的put方法添加元素时会被阻塞,直到对应的notifyElementPut方法返回之后。

l  notifyElementUpdated方法,当往Cache中put一个已经存在的元素时就会触发CacheEventListener的notifyElementUpdated方法,此时put操作也会处于阻塞状态,直到notifyElementUpdated方法执行完毕。

l  notifyElementExpired方法,当Ehcache检测到Cache中有元素已经过期的时候将调用notifyElementExpired方法。

l  notifyElementEvicted方法将会在元素被驱除的时候调用。

l  notifyRemoveAll方法将在调用Cache的removeAll方法之后被调用。

dispose方法用于释放资源。

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

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

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

l  class:指定当前CacheEventListenerFactory对应的Java类全名称。

l  properties:指定在构建CacheEventListenerFactory时需传入的属性键值对,多个属性之间默认用逗号分开,如:“prop1=value1,prop2=value2”。

l  propertySeparator:指定properties中多个属性之间的分隔符。

l  listenFor:表示在集群环境下可以监听到的Cache事件的范围,可选值有local、remote和all。local代表只监听本节点的Cache事件,remote代表只监听其他节点的Cache事件,all代表监听所有的Cache事件。默认是all。

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

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

1、实现一个CacheEventListener。

  1. public class MyCacheEventListener implements CacheEventListener {
  2.  
  3. @Override
  4. public void notifyElementRemoved(Ehcache cache, Element element)
  5. throws CacheException {
  6. System.out.println("removed");
  7. }
  8.  
  9. @Override
  10. public void notifyElementPut(Ehcache cache, Element element)
  11. throws CacheException {
  12. System.out.println("put");
  13. }
  14.  
  15. @Override
  16. public void notifyElementUpdated(Ehcache cache, Element element)
  17. throws CacheException {
  18. System.out.println("updated");
  19. }
  20.  
  21. @Override
  22. public void notifyElementExpired(Ehcache cache, Element element) {
  23. System.out.println("expired");
  24. }
  25.  
  26. @Override
  27. public void notifyElementEvicted(Ehcache cache, Element element) {
  28. System.out.println("evicted");
  29. }
  30.  
  31. @Override
  32. public void notifyRemoveAll(Ehcache cache) {
  33. System.out.println("removeAll");
  34. }
  35.  
  36. @Override
  37. public void dispose() {
  38.  
  39. }
  40.  
  41. public Object clone() throws CloneNotSupportedException {
  42. thrownew CloneNotSupportedException();
  43. }
  44.  
  45. }

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

  1. public class MyCacheEventListenerFactory extends CacheEventListenerFactory {
  2.  
  3. @Override
  4. public CacheEventListener createCacheEventListener(Properties properties) {
  5. returnnew MyCacheEventListener();
  6. }
  7.  
  8. }

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

  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
  3. maxBytesLocalHeap="100M">
  4.  
  5. <diskStore path="d:\\ehcache" />
  6.  
  7. <cache name="test">
  8. <cacheEventListenerFactory class="xxx.xxx.MyCacheEventListenerFactory"/>
  9. </cache>
  10.  
  11. <defaultCache/>
  12.  
  13. </ehcache>

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

(注:本文是基于ehcache2.8.1所写)

Ehcache(06)——监听器的更多相关文章

  1. 网站缓存技术总结( ehcache、memcache、redis对比)

    网站技术高速发展的今天,缓存技术已经成为大型网站的一个关键技术,缓存设计好坏直接关系的一个网站访问的速度,以及购置服务器的数量,甚至影响到用户的体验. 网站缓存按照存放的地点不同,可以分为客户端缓存. ...

  2. ehcache memcache redis 三大缓存

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  3. EhCache RMI 分布式缓存/缓存集群

    EhCache 系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点. EhCache 的主要特性有: 快速.精干 简单: 多种缓存策略: 缓存数据有两级:内存和磁盘, ...

  4. 简单的使用ehcache

    之前一直感觉缓存是高上大的东西,没有心思去研究.做了之后发现,简单的使用还是很容易的.这里记录ehcache在jfinal中的简单使用. 1.ehcahe简介 EhCache 是一个纯Java的进程内 ...

  5. spring ehcache 页面、对象缓存

    一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...

  6. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  7. ehcache memcache redis 三大缓存男高音

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  8. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  9. 缓存插件 EHCache 对象缓存(Spring)

    对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询. 对象缓存一般是针对方法.类而来的,结合Spring的Aop对象.方法缓存就很简单.这里需要用到切面编程 ...

随机推荐

  1. poj 2409(polya定理模板)

    题意:给你n种颜色和m个小球,问你有多少种不同的方案! 分析:作为模板.. 代码实现: #include <iostream> #include <cstdio> #inclu ...

  2. ylbtech-QQ(腾讯)-群空间-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-QQ(腾讯)-群空间-数据库设计 DatabaseName:QQ-群空间 Model:群相册.群共享.群论坛.群成员.留言板.公告.6个模 ...

  3. kinect 录制彩色和深度视频

    安装 KinectSDK-v1.8-Setup.exe OpenNI-Windows-x86-2.1.0.msi Qt工程 拷贝 Redist 下内容到 编译的exe所在目录 #include < ...

  4. call Kernelized Correlation Filters Tracker(Matab) in Qt(c++)

    recently, i need call the KCF tracker in my graduation project. the KCF tracker is fast and best per ...

  5. fscanf的返回值未成功输入的元素个数 .xml

    pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...

  6. leetcode刷题总结一

    大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...

  7. 理解KMP

    KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n),KMP匹配算法,可以证明它的时间复杂度为O(m+n).. 一.简单匹配算法 先来看一个 ...

  8. 星星字体 ps教程

    本教程主要使用Photoshop制作绚丽星空装饰的艺术字教程,这个教程很简单,只需要一些简单技巧,即可做出海报.书籍杂志的封面效果.其中的字体.笔刷和背景均可以更换 教程所需要的素材链接:http:/ ...

  9. Linux 的 screen用法

    screen可以将任务挂起,即将任务放在后台,一般5个任务左右. 1.新建screen会话:直接输入screen命令或者screen -S [会话名称] 2.退出会话:按下组合键Ctrl+a并松开,此 ...

  10. Protocol Buffer详解

    1.Protocol Buffer 概念 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 ...