About Exception Handlers

By default, most cache operations will propagate a runtime CacheException on failure. An interceptor, using a dynamic proxy, may be configured so that a CacheExceptionHandler can be configured to intercept Exceptions. Errors are not intercepted.

Caches with ExceptionHandling configured are of type Ehcache. To get the exception handling behavior they must be referenced using CacheManager.getEhcache(), not CacheManager.getCache(), which returns the underlying undecorated cache.

Exception handlers are configured per cache. Each cache can have at most one exception handler. You can set CacheExceptionHandlers either declaratively in the ehcache.xml configuration file, or programmatically.

Declarative Configuration

To configure an exception handler declaratively, add the cacheExceptionHandlerFactory element to ehcache.xml as shown in the following example:

<cache ...>
  <cacheExceptionHandlerFactory
  class="net.sf.ehcache.exceptionhandler.CountingExceptionHandlerFactory"
  properties="logLevel=FINE"/>
</cache>

Implementing a Cache Exception Handler Factory and Cache Exception Handler

A CacheExceptionHandlerFactory is an abstract factory for creating cache exception handlers. Implementers should provide their own concrete factory, extending this abstract factory. It can then be configured in ehcache.xml.

Note: Your implementations need to be placed in the classpath accessible to Ehcache. For information about how class loading is handled, see Class Loading.

The factory class needs to be a concrete subclass of the abstract factory class CacheExceptionHandlerFactory, which is reproduced below.

/**
* An abstract factory for creating <code>CacheExceptionHandler</code>s at configuration time, in ehcache.xml.
* <p/>
* Extend to create a concrete factory
*
* @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
* @version $Id: CacheExceptionHandlerFactory.java 5594 2012-05-07 16:04:31Z cdennis $
*/
public abstract class CacheExceptionHandlerFactory { /**
* Create an <code>CacheExceptionHandler</code>
*
* @param properties implementation specific properties. These are configured as comma
* separated name value pairs in ehcache.xml
* @return a constructed CacheExceptionHandler
*/
public abstract CacheExceptionHandler createExceptionHandler(Properties properties); }

The factory creates a concrete implementation of the CacheExceptionHandler interface, which is reproduced below:

/**
* A handler which may be registered with an Ehcache, to handle exceptions on Cache operations.
* <p/>
* Handlers may be registered at configuration time in ehcache.xml, using a CacheExceptionHandlerFactory, or
* set at runtime (a strategy).
* <p/>
* If an exception handler is registered, the default behaviour of throwing the exception will not occur. The handler
* method <code>onException</code> will be called. Of course, if the handler decides to throw the exception, it will
* propagate up through the call stack. If the handler does not, it won't.
* <p/>
* Some common Exceptions thrown, and which therefore should be considered when implementing this class are listed below:
* <ul>
* <li>{@link IllegalStateException} if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE}
* <li>{@link IllegalArgumentException} if an attempt is made to put a null element into a cache
* <li>{@link net.sf.ehcache.distribution.RemoteCacheException} if an issue occurs in remote synchronous replication
* <li>
* <li>
* </ul>
*
* @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
* @version $Id: CacheExceptionHandler.java 5594 2012-05-07 16:04:31Z cdennis $
*/
public interface CacheExceptionHandler { /**
* Called if an Exception occurs in a Cache method. This method is not called
* if an <code>Error</code> occurs.
*
* @param ehcache the cache in which the Exception occurred
* @param key the key used in the operation, or null if the operation does not use a key or the key was null
* @param exception the Exception caught.
*/
void onException(Ehcache ehcache, Object key, Exception exception);
}

Programmatic Configuration

The following example shows how to add exception handling to a cache, and then add the cache back into cache manager so that all clients obtain the cache handling decoration.

CacheManager cacheManager = ...
Ehcache cache = cacheManger.getCache("exampleCache");
ExceptionHandler handler = new ExampleExceptionHandler(...);
cache.setCacheLoader(handler);
Ehcache proxiedCache = ExceptionHandlingDynamicCacheProxy.createProxy(cache);
cacheManager.replaceCacheWithDecoratedCache(cache, proxiedCache);

Ehcache(2.9.x) - API Developer Guide, Cache Exception Handlers的更多相关文章

  1. Ehcache(2.9.x) - API Developer Guide, Cache Decorators

    About Cache Decorators Ehcache uses the Ehcache interface, of which Cache is an implementation. It i ...

  2. Ehcache(2.9.x) - API Developer Guide, Cache Loaders

    About Cache Loaders A CacheLoader is an interface that specifies load() and loadAll() methods with a ...

  3. Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms

    About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evi ...

  4. Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns

    There are several common access patterns when using a cache. Ehcache supports the following patterns ...

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

    About CacheManager Event Listeners CacheManager event listeners allow implementers to register callb ...

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

    About Cache Event Listeners Cache listeners allow implementers to register callback methods that wil ...

  7. Ehcache(2.9.x) - API Developer Guide, Cache Extensions

    About Cache Extensions Cache extensions are a general-purpose mechanism to allow generic extensions ...

  8. Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches

    About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...

  9. Ehcache(2.9.x) - API Developer Guide, Searching a Cache

    About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...

随机推荐

  1. 斯特灵数 (Stirling数)

    @维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...

  2. ASP.NET MVC4中用 BundleCollection

    来源:http://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: &l ...

  3. Castle框架中的IOC和AOP机制

    反转控制(IOC)和面向切面编程(AOP)技术作为当前比较流行的技术,其优势已受到广泛关注,但是这两项新技术在实际项目上的应用研究却很落后,而且在.NET平台下实现这两项技术没有形成可以广泛套用的框架 ...

  4. Spring MVC 的视图转发

    Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作 1.使用RedirectView public ModelAndView login(){ RedirectView ...

  5. [1.1]Environment preset on a Windows server

    1. Python 3.5.1 (also on your personal computer) 2. Django 1.10.1 (also on your personal computer) 3 ...

  6. sgu - 269 - Rooks

    题意:给出一个n行的棋盘,每行的长度任意,问在该棋盘中放k个车(不能同行或者同列)有多少种放法(n <= 250, 每行的长度 <= 250). 题目链接:http://acm.sgu.r ...

  7. tomcat启动很慢的原因

    启动后tomcat显示的代码如下: 2014-4-3 10:50:15 org.apache.catalina.core.AprLifecycleListener init 信息: The APR b ...

  8. BZOJ 2733: [HNOI2012]永无乡 启发式合并treap

    2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  9. phpcms v9和discuz X3.1实现同步登陆退出论坛(已实现)

    网络上文章很多,按步骤配置好了之后phpcms可以同步登录dz,但是dz登录后状态却无法同步到phpcms,网络上找了很多资料都大同小异,头大.只能自己调试了,废话不多说了.       以下网络上抄 ...

  10. [AngularJS] New in Angular 1.3 - bindToController

    If you want to use controllers, instead of a link function, you can use bindToController. <!DOCTY ...