Ehcache (2.9.x)-API Developer Guide, Cache Manager Event Listeners

Source: Internet
Author: User

About CacheManager Event Listeners

CacheManager event listeners allow implementers to register callback methods that'll be executed when a CacheManager eve NT occurs. CacheManager listeners implement the Cachemanagereventlistener interface. The events include:

    • Adding a Cache
    • Removing a Cache

Callbacks to these methods is synchronous and unsynchronized. It is the responsibility of the implementer to safely handle the potential performance and thread safety issues depending On what their listener is doing.

Configuring a Cache Manager Event Listener

One cachemanagereventlistenerfactory and hence one cachemanagereventlistener can be specified per CacheManager instance. The factory is configured as below:

<class= " properties=" "/>

The entry specifies a cachemanagereventlistenerfactory which would be used to create a cachemanagereventlistener, which is Notified when Caches is added or removed from the CacheManager. The attributes of a cachemanagereventlistenerfactory are:

    • class a fully qualified factory class name.
    • Properties -comma-separated properties have meaning only to the factory.

Callbacks to listener methods is synchronous and unsynchronized. It is the responsibility of the implementer to safely handle the potential performance and thread safety issues depending On what their listener is doing. If No class is specified, or there are no cachemanagereventlistenerfactory element, no listener is created. There is no default.

Implementing a CacheManager Event Listener Factory and CacheManager event Listener

Cachemanagereventlistenerfactory is a abstract factory for creating CacheManager listeners. Implementers should provide their own concrete factory extending this abstract factory. It can then is configured in Ehcache.xml.

The factory class needs to being a concrete subclass of the abstract factory cachemanagereventlistenerfactory, which is repro duced below:

/*** An abstract factory for creating {@linkCachemanagereventlistener}s. * Implementers should provide their own concrete factory extending this * factory. It can then is configured in Ehcache.xml. * */  Public Abstract classCachemanagereventlistenerfactory {/*** Create a cachemanagereventlistener * *@paramproperties implementation specific properties.     * These is configured as comma-separated name value pairs in Ehcache.xml.     * Properties may be null. * @returna constructed Cachemanagereventlistener*/      Public AbstractCachemanagereventlistener Createcachemanagereventlistener (Properties properties);}

The factory creates a concrete implementation of Cachemanagereventlistener, which is reproduced below:

/*** Allows implementers to register callback methods that'll be executed when a * <CODE>CACHEMANAGER</CODE&G T event occurs. * * The Lifecycle Events is: * <ol> * <li>init * <li>dispose * </ol> * * * CacheManager change EV Ents is: * <ol> * <li>adding a <code>Cache</code> * <li>removing a <code>cache</c ode> * </ol> * Note that the caches is part of the initial configuration is not considered "changes". * It is only caches added or removed beyond the initial config. * * Callbacks to these methods is synchronous and unsynchronized. It is the responsibility of * the implementer to safely handle the potential performance and thread safety issues * depend ing on what their listener is doing. * @authorGreg Luck *@version$Id: Cachemanagereventlistener.java 5594 2012-05-07 16:04:31z Cdennis $ *@since1.2 *@seeCacheeventlistener*/ Public InterfaceCachemanagereventlistener {/*** Call to start the listeners and does any other required initialisation.     * Init should also handle any work to does with the caches that is part of the initial configuration. * @throwsCacheexception-all exceptions is wrapped in cacheexception*/    voidInit ()throwscacheexception; /*** Returns the listener status. * @returnThe status at the time of the method is called*/Status getStatus (); /*** Stop the Listener and free any resources. * @throwsCacheexception-all exceptions is wrapped in cacheexception*/    voidDispose ()throwscacheexception; /*** Called immediately after a cache have been added and activated. * <p/> * Note that the CacheManager calls this method from a synchronized method.     Any attempt to * call a synchronized method on CacheManager from the This method would cause a deadlock. * <p/> * Note that activation would also cause a Cacheeventlistener status change notification * from {@linknet.sf.ehcache.status#status_uninitialised} to * {@linknet.sf.ehcache.status#status_alive}. Care should is taken on processing that * notification because: * <ul> * <li>the cache would not ye     T is accessible from the CacheManager. * <li>the addcaches methods which cause this notification is synchronized on the * CacheManager. An attempt -to-call {@linkNet.sf.ehcache.cachemanager#getehcache (String)} * would cause a deadlock.     * </ul> * The calling method would block until this method returns. * <p/> *@paramCacheName The name of the <code>Cache</code> the operation relates to *@seeCacheeventlistener*/    voidnotifycacheadded (String cachename); /*** Called immediately after a cache have been disposed and removed.     The calling method would * block until this method returns. * <p/> * Note that the CacheManager calls this method from a synchronized method.     Any attempt to * call a synchronized method on CacheManager from the This method would cause a deadlock. * <p/> * Note that a {@linkCacheeventlistener} status changed'll also be triggered.     Any * attempt from, notification to access CacheManager would also result in a deadlock. * @paramCacheName The name of the <code>Cache</code> the operation relates to*/    voidnotifycacheremoved (String cachename);}

The implementations need to being placed in the classpath accessible to Ehcache. Ehcache uses the ClassLoader returned by Thread.CurrentThread (). Getcontextclassloader () to load classes.

Ehcache (2.9.x)-API Developer Guide, Cache Manager Event Listeners

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.