Ehcache(06)——监听器
http://haohaoxuexi.iteye.com/blog/2119353
监听器
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);
- }
l init方法会在CacheManagerEventListener实现类实例化后被调用,用于初始化CacheManagerEventListener。
l getStatus方法返回当前CacheManagerEventListener所处的状态,可选值有STATUS_UNINITIALISED、STATUS_ALIVE和STATUS_SHUTDOWN。
l dispose方法用于释放资源。
l notifyCacheAdded方法会在往CacheManager中添加Cache时被调用。
l 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。
l class属性必须指定,表示对应的CacheManagerEventListenerFactory实现类全名。
l properties属性可选,用来指定CacheManagerEventListenerFactory在创建CacheManagerEventListener时需要使用的属性,里面是键值对的形式,多个属性之间默认用逗号隔开。如“prop1=val1,prop2=val2”。
l 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();
- }
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。
- 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事件的监听。
(注:本文是基于ehcache2.8.1所写)
Ehcache(06)——监听器的更多相关文章
- 网站缓存技术总结( ehcache、memcache、redis对比)
网站技术高速发展的今天,缓存技术已经成为大型网站的一个关键技术,缓存设计好坏直接关系的一个网站访问的速度,以及购置服务器的数量,甚至影响到用户的体验. 网站缓存按照存放的地点不同,可以分为客户端缓存. ...
- ehcache memcache redis 三大缓存
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...
- EhCache RMI 分布式缓存/缓存集群
EhCache 系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点. EhCache 的主要特性有: 快速.精干 简单: 多种缓存策略: 缓存数据有两级:内存和磁盘, ...
- 简单的使用ehcache
之前一直感觉缓存是高上大的东西,没有心思去研究.做了之后发现,简单的使用还是很容易的.这里记录ehcache在jfinal中的简单使用. 1.ehcahe简介 EhCache 是一个纯Java的进程内 ...
- spring ehcache 页面、对象缓存
一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...
- Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...
- ehcache memcache redis 三大缓存男高音
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...
- (转)Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- 缓存插件 EHCache 对象缓存(Spring)
对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询. 对象缓存一般是针对方法.类而来的,结合Spring的Aop对象.方法缓存就很简单.这里需要用到切面编程 ...
随机推荐
- poj 2409(polya定理模板)
题意:给你n种颜色和m个小球,问你有多少种不同的方案! 分析:作为模板.. 代码实现: #include <iostream> #include <cstdio> #inclu ...
- ylbtech-QQ(腾讯)-群空间-数据库设计
ylbtech-DatabaseDesgin:ylbtech-QQ(腾讯)-群空间-数据库设计 DatabaseName:QQ-群空间 Model:群相册.群共享.群论坛.群成员.留言板.公告.6个模 ...
- kinect 录制彩色和深度视频
安装 KinectSDK-v1.8-Setup.exe OpenNI-Windows-x86-2.1.0.msi Qt工程 拷贝 Redist 下内容到 编译的exe所在目录 #include < ...
- 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 ...
- fscanf的返回值未成功输入的元素个数 .xml
pre{ line-height:1; color:#38ede1; background-color:#5b2814; font-size:16px;}.sysFunc{color:#008080; ...
- leetcode刷题总结一
大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...
- 理解KMP
KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n),KMP匹配算法,可以证明它的时间复杂度为O(m+n).. 一.简单匹配算法 先来看一个 ...
- 星星字体 ps教程
本教程主要使用Photoshop制作绚丽星空装饰的艺术字教程,这个教程很简单,只需要一些简单技巧,即可做出海报.书籍杂志的封面效果.其中的字体.笔刷和背景均可以更换 教程所需要的素材链接:http:/ ...
- Linux 的 screen用法
screen可以将任务挂起,即将任务放在后台,一般5个任务左右. 1.新建screen会话:直接输入screen命令或者screen -S [会话名称] 2.退出会话:按下组合键Ctrl+a并松开,此 ...
- Protocol Buffer详解
1.Protocol Buffer 概念 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 ...