java - 如何监听 ehcache clear 事件?
解决方案
使用 aspectj 的解决方法:
@Aspect
public class CacheListenerAspect {
public Set<BiConsumer<CacheEventType, Cache<?, ?>>> listeners = new HashSet<>();
public enum CacheEventType {
CLEAR, CLOSE
}
public void listenCacheEvent( BiConsumer<CacheEventType, Cache<?, ?>> listener ) {
listeners.add( listener );
}
@Around("execution(* javax.cache.Cache.*(..))")
public Object around( ProceedingJoinPoint joinPoint ) throws Throwable {
getEventType( joinPoint.getSignature() )
.ifPresent( ev -> listeners.forEach(
l -> l.accept( ev, (Cache<?, ?>) joinPoint.getThis() ) ) );
return joinPoint.proceed();
}
public Optional<CacheEventType> getEventType( Signature signature ) {
CacheEventType res = null;
if (signature.getName().equalsIgnoreCase( "clear" ))
res = CacheEventType.CLEAR;
if (signature.getName().equalsIgnoreCase( "close" ))
res = CacheEventType.CLOSE;
return Optional.ofNullable( res );
}
}
用法:
Aspects.aspectOf( CacheListenerAspect.class ).listenCacheEvent( this::myMethod );
推荐阅读
- reactjs - 为什么我的模式没有出现在 react native iOS 中。但它在反应原生android中工作
- python - 在每个 URL 中创建一个新的 todolist 实例
- angular - TypeError: Jest: 转换必须导出一个 `process` 函数
- c# - C# - 通用事件处理程序,用于移动在运行时使用鼠标创建的图片框
- c - C中的算术异常
- python-3.x - 在每个项目的 PyCharm 中设置 PYTHONPATH
- mysql - 加入 3 个表并仅根据最新值获取记录
- jquery - $('.selectpicker').selectpicker('refresh'); 标题不起作用
- ios - UITextField中的borderRect、editingRect、placeholderRect、textRect是什么?
- edk2 - 如何使用 edk2 创建多进程?