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. 关于EL表达式的大小写问题。谁来帮我解答?

    最近在学习ssh框架,今天遇到了一个非常奇怪的问题.我想在jsp页面中的到session中的数据.<%=s.getUserYes() %>这样写能得到数据, ${sessionScope. ...

  2. 织梦dedecms后台添加图片style全部都变成st<x>yle的解决办法

    可乐站长在建站的时候,上传缩略图喜欢输入图片路径,不喜欢上传图片,有几次我上传图片路径为:/style/image/**.jpg,然后返回修改后,图片为路径却为:/st<x>yle/ima ...

  3. 在数据库各种状态下查询DBID的五大类十种方法汇总

    关于DBID: DBID是DataBase IDentifier的缩写,意思就是数据库的唯一标识符. 这个DBID在数据文件头和控制文件都是存在的,可以用于标示数据文件的归属. 对于不同数据库来说,D ...

  4. C# 调用第三方DLL完整实例

    C# 调用第三方DLL完整实例 分类: C/C++ 以下代码为本人在实际项目中编写的调用第三方DLL接口程序的完整代码. public class ExecuteDLL : Form { ...//忽 ...

  5. sql2008“备份集中的数据库备份与现有数据库不同”解决方法

    因为是在另一台电脑对同名数据库做的备份,用常规方法还原,提示不是相同数据库,不让还原,在网上找到下面的方法解决了: 一.右击系统数据库master,新建查询 执行以下SQL代码: RESTORE DA ...

  6. Actions 动作集

    --> 移动鼠标到指定位置(先触发onMouseOver动作)        Actions action = new Actions(driver);        WebElement th ...

  7. PostgreSQL的 initdb 源代码分析之九

    继续:下面的是定义信号处理函数. /* * now we are starting to do real work, trap signals so we can clean up */ /* som ...

  8. 【Android应用开发】 OpenGL ES -- 透视投影 和 正交投影

    博客地址 : http://blog.csdn.net/shulianghan/article/details/46680803 源代码下载 : http://download.csdn.net/de ...

  9. 自己写一个jQuery垂直滚动栏插件(panel)

    html中原生的滚动栏比較难看,所以有些站点,会自己实现滚动栏,导航站点hao123在一个側栏中,就自己定义了垂直滚动栏,效果比較好看,截图例如以下: watermark/2/text/aHR0cDo ...

  10. HDU 5570 balls 期望 数学

    balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...