(06)Ehcache监听器

x33g5p2x  于2021-12-25 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(262)

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_UNINITIALISEDSTATUS_ALIVESTATUS_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事件的监听。

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

相关文章