Ehcache (06)--Listener

Source: Internet
Author: User
Tags event listener

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

Listener

There are two types of listeners in Ehcache, listening to the CacheManager Cachemanagereventlistener and listening to the cacheeventlistener of the cache. In Ehcache, listener is produced and played by the corresponding listener factory. Let's discuss these two types of listeners in the future.

1 CacheManager Listener

A Cachemanagereventlistener interface is defined in Ehcache to listen for CacheManager events. Cachemanagereventlistener can listen for events that have CacheManager add and remove caches. The following five methods are defined:

Public interface Cachemanagereventlistener {         void init () throws cacheexception;         Status getStatus ();         void Dispose () throws cacheexception;         void notifycacheadded (String cachename);         void notifycacheremoved (String cachename);     }  


The init method is called after the Cachemanagereventlistener implements the class instantiation and is used to initialize the Cachemanagereventlistener.

The L getStatus method returns the current state of the Cachemanagereventlistener, with an optional value of status_uninitialised,status_alive and Status_shutdown.

The dispose method is used to release resources.

The L notifycacheadded method is called when the cache is added to the CacheManager.

The L notifycacheremoved method is called when the cache is removed from the CacheManager.

Ehcache is the Cachemanagereventlistener used by Cachemanagereventlistenerfactory to get the current CacheManager. Cachemanagereventlistenerfactory is an abstract class, which is defined as follows:

Public abstract class Cachemanagereventlistenerfactory {public         abstract Cachemanagereventlistener              Createcachemanagereventlistener (CacheManager CacheManager, properties properties);     }  


In our own cachemanagereventlistenerfactory subclasses need to implement their abstract methods Createcachemanagereventlistener, When the corresponding Cachemanagereventlistener is generated, we can use the current CacheManager and the properties specified when Cachemanagereventlistenerfactory is defined in the Ehcache.xml file Propert ies With Cachemanagereventlistenerfactory we can implement different cachemanagereventlistener for different cachemanager.

With Cachemanagereventlistener and Cachemanagereventlistenerfactory, We need to use the cachemanagereventlistenerfactory element in the corresponding Ehcache.xml file to specify the event listener factory used by the CacheManager of the current Ehcache.xml file, and each Ehcache.xml file is the most More than one cachemanagereventlistenerfactory element can be specified.

The Cachemanagereventlistenerfactory element can specify three properties: Class, properties, and Propertyseparator.

The class property must be specified to represent the full name of the corresponding Cachemanagereventlistenerfactory implementation.

The Properties property is optional to specify the attributes that Cachemanagereventlistenerfactory needs to use when creating the Cachemanagereventlistener, which is the form of a key-value pair. The default between multiple attributes is separated by commas. such as "Prop1=val1,prop2=val2".

The L propertyseparator property is optional to specify the delimiter between the Properties property.

Here is an example of a listener CacheManager event.

       1, to achieve their own 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 ..." + CA        Chename);     System.out.println (Cachemanager.getcache (cachename));  } @Override public void notifycacheremoved (String cachename) {System.out.println ("cacheremoved ..." +     CacheName); }     }  

2, to achieve their own cachemanagereventlistenerfactory, according to the conditions to create the corresponding cachemanagereventlistener.

public class Mycachemanagereventlistenerfactory extends        cachemanagereventlistenerfactory {        @Override     Public Cachemanagereventlistener Createcachemanagereventlistener (           cachemanager CacheManager, Properties Properties) {        returnnew mycachemanagereventlistener (CacheManager);     }     

3. The cachemanagereventlistenerfactory element in the Ehcache.xml file specifies the cachemanagereventlistenerfactory that is used by the current CacheManager for our own definition Cachemanag Ereventlistenerfactory.

<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>  

The test code for the above listener is as follows:

@Test public  void Testadd () {     CacheManager CacheManager = Cachemanager.create (This.getclass (). GetResource ("/ Ehcache-listener.xml "));     Cachemanager.addcache ("Test1");     Cachemanager.removecache ("Test1");  

2 Cache Listener

A Cacheeventlistener interface is defined in Ehcache to listen for cache events. It can monitor the addition, deletion and update of elements in the cache. The following methods are primarily defined in 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 ();  


The L notifyelementremoved method is called when a single element is removed from the cache, which is called after the Remove method of the cache is called.

The L notifyelementput method is called when an element is added to the cache. Calls to the cache's put method are blocked when the element is added until the corresponding Notifyelementput method returns.

L notifyelementupdated method, when put an already existing element into the cache, will trigger the Cacheeventlistener notifyelementupdated method, The put operation is also blocked until the Notifyelementupdated method executes.

L notifyelementexpired method, the Notifyelementexpired method is called when Ehcache detects that an element in the cache has expired.

The L notifyelementevicted method will be called when the element is evicted.

The L Notifyremoveall method is called after the RemoveAll method of the cache is called.

The Dispose method is used to release resources.

Then we need to implement all of the above methods when we implement our own cacheeventlistener. Ehcache provides us with a default null implementation Cacheeventlisteneradapter, which we can inherit cacheeventlisteneradapter in real-world applications, and then rewrite some of these methods, To simplify our implementation of the Cacheeventlistener.

Like Cachemanagereventlistener, Cacheeventlistener can't work alone, It requires the cacheeventlistenerfactory associated with the current cache to build a cacheeventlistener that is currently used by the cache. Cacheeventlistenerfactory is an abstract class that defines only one Createcacheeventlistener method that receives a properties object as a parameter.

The cacheeventlistenerfactory used by the current cache can be specified in the Ehcahce.xml file through the child element cacheeventlistenerfactory under the cache element. It can specify four properties:

L class: Specifies the full name of the Java class that corresponds to the current cacheeventlistenerfactory.

L Properties: Specifies the property key-value pair to pass in when building cacheeventlistenerfactory, and the default between multiple attributes is separated by commas, such as: "Prop1=value1,prop2=value2".

L propertyseparator: Specifies the delimiter between multiple attributes in the properties.

L listenfor: Indicates the range of cache events that can be heard in a clustered environment, with the optional values local, remote, and all. The local representative listens only to the cache event for this node, and the remote represents only the cache events of the other nodes, all of which are listening for all cache events. The default is all.

Unlike Cachemanagereventlistenerfactory, a cache can define multiple cacheeventlistenerfactory.

Here's an example of using the cache listener.

1, realize a cacheeventlistener.

public class Mycacheeventlistener implements Cacheeventlistener {@Override public void notifyelementremoved (Eh     Cache 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 ("Expir     Ed "); } @Override public void notifyelementevicted (Ehcache cache, Element Element) {System.out.println ("evict     Ed ");     } @Override public void Notifyremoveall (Ehcache cache) {System.out.println ("RemoveAll"); } @Override public void Dispose () {} public objecT Clone () throws Clonenotsupportedexception {thrownew clonenotsupportedexception (); }     }

2, to achieve the abstract factory class Cacheeventlistenerfactory to produce the previously defined Cacheeventlistener.

public class Mycacheeventlistenerfactory extends Cacheeventlistenerfactory {        @Override     public Cacheeventlistener Createcacheeventlistener (Properties properties) {        returnnew mycacheeventlistener ();     }     }  


3. In the Ehcache.xml file, specify the cacheeventlistenerfactory that the current cache uses by cacheeventlistenerfactory the child element of the cache element.

<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>  


We have completed the monitoring of the cache event through the above three steps.

(Note: This article is written based on ehcache2.8.1)

Ehcache (06)--Listener

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.