Ehcache(2.9.x) - API Developer Guide, Cache Exception Handlers
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Ehcache(2.9.x) - API Developer Guide, Cache Manager Event Listeners
About CacheManager Event Listeners CacheManager event listeners allow implementers to register callb ...
- Ehcache(2.9.x) - API Developer Guide, Cache Event Listeners
About Cache Event Listeners Cache listeners allow implementers to register callback methods that wil ...
- Ehcache(2.9.x) - API Developer Guide, Cache Extensions
About Cache Extensions Cache extensions are a general-purpose mechanism to allow generic extensions ...
- 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 ...
- 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 ...
随机推荐
- 在Android4.0中Contacts拨号盘界面剖析(源码)
通过在 ViewPager 的适配器对象中,发现过一下三行代码 private DialpadFragment mDialpadFragment; private CallLogFragment ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- F - Coins
F - Coins Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit St ...
- SQLserver锁和事务隔离级别的比较与使用(转)
SQLserver锁和事务隔离级别的比较与使用(转) http://www.cnblogs.com/chenlulouis/archive/2010/12/06/1898014.html http:/ ...
- C# Dictionary用法总结
转自:http://www.cnblogs.com/linlf03/archive/2011/12/09/2282574.html http://www.cnblogs.com/linzheng/ar ...
- time_t转换为DateTime
最近解析文华财经的日线数据. 取得的第一个字段是日期,为time_t格式(long)的. 因为是用C#来写解析程序,所以要转换为DateTime的. time_t是世界时间,要转换为本地时间,所以要加 ...
- 访问修饰符与可选修饰符static的使用
在Java中修饰符较多,在这里简单说一说几个访问修饰符的选择使用,和可选修饰符static的使用. 一.访问修饰符:这是Java里用来控制访问类及类的方法.变量的访问权限,从而达到只暴露接口,来隐藏内 ...
- 命令行创建maven模块工程
上一边文章,借助外部eclipse来创建模块项目,本文直接使用maven命令来创建 mvn archetype:generate -DgroupId=com.mycompany.demo -Darti ...
- 【android-cocos2d-X 环境配置】在Mac下搭建Cocos2d-X-android开发环境!
转自:http://blog.csdn.net/dingkun520wy/article/details/17097593 (1)下载 首先要下载好要用到的东西: 1.android-SDK 地址是 ...
- cocos2d-x 多线程以及线程同步
转自:http://blog.csdn.net/zhy_cheng/article/details/9116479 cocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作 ...