尝试删除所有缓存时 Ehcache 出现异常
Exception in Ehcache when trying to remove all the caches
我已经搜索了解决方案,因为其他问题也已发布 java.lang.NullPointerExcpetion
。但是我无法找到我的 problem.I 我正在使用 Ehcache 3,并且我已经实例化了一个缓存,它正在放置数据进入缓存。
作为缓存工作的class是:
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.ResourceType;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import javax.ejb.Stateless;
import javax.enterprise.inject.spi.CDI;
import java.io.File;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,GenericClassForMap> mapPerson;
private static Timer timer;
public CacheHelper() {
timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 2, 5000);
}
//initialziing cache
public void putInCacheFromDb() {
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
//for map
mapPerson = cacheManager
.createCache("cacheOfmap", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,GenericClassForMap.class,
ResourcePoolsBuilder.heap(100000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
TimeUnit.SECONDS))));
Map<Integer,Person> mp=new HashMap<>();
mp.put(1,new Person(1,"om"));
mapPerson.put(CACHE_MAP_PARAMETER,new GenericClassForMap(mp));
}
//method to clear the cache
public void clearAllCache(){
System.out.println("cache is being cleared........");
cacheManager.close();
}
}
当我 运行 程序并打印为:
时,数据正在缓存中插入
coming from map
[Person{id=1, name='om'}]
我正在为缓存设置定时器,在 15 秒内缓存需要 refreshed.So,我尝试使用 Helper.java class 作为,
import com.cache.cachemanager.CacheHelper;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class Helper extends TimerTask
{
public static int i = 0;
public void run()
{
System.out.println("Timer ran " + ++i);
if(i==15){
try {
Thread.sleep(10000);
System.out.println("system is sleeping");
CacheHelper cacheHelper=new CacheHelper();
cacheHelper.clearAllCache();
cacheHelper.putInCacheFromDb();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我在以下位置收到空指针异常:
system is sleeping
cache is being cleared........
Exception in thread "Timer-6" java.lang.NullPointerException
at com.cache.cachemanager.CacheHelper.clearAllCache(CacheHelper.java:309)
at com.cache.timer.Helper.run(Helper.java:25)
我尝试调试程序,发现空指针异常来自:
cacheManager.close();
我的 cacheManager null.But 当我尝试从 API 请求中清除缓存时,它能够清除 cache.I 我没有使用 spring framework.Why 是我的 cacheManager
是空的,但是当通过 API 调用时它很容易被删除喜欢:
http://localhost:8080/myresource/clearCache ,我的缓存正在被轻松清除。
cache is being cleared........
11:19:31,154 INFO [org.ehcache.core.EhcacheManager] (default task-8) Cache 'cacheOfmap' removed from EhcacheManager.
在调用您的 clearAllCache
方法之前,您创建了一个 new CacheHelper()
。因此,此实例的 cacheManager
属性永远不会被初始化(您只能在 putInCacheFromDb
方法中初始化它)。
我想当你通过 API 调用它时,你有一个 bean/singleton 实例被调用来填充你的缓存,然后永远不会被销毁。所以必须使用这个实例。
编辑 :回答为什么在构造函数中初始化 cacheManager
的问题。
您当前的代码运行如下:
// CacheHelper constructor is called to initialize a new CacheHelper
// => cacheManager attribute is not initialized
CacheHelper cacheHelper = new CacheHelper();
// This method will call cacheManager.close().
// => cacheManager is currently null => NullPointerException
cacheHelper.clearAllCache();
// This method will initialize the cacheManager attribute
// => Too late the Exception already occured
// => So, need to initialize cacheManager attribute before calling clearAllCache()
// => the constructor is the best place to do this here.
cacheHelper.putInCacheFromDb();
我已经搜索了解决方案,因为其他问题也已发布 java.lang.NullPointerExcpetion
。但是我无法找到我的 problem.I 我正在使用 Ehcache 3,并且我已经实例化了一个缓存,它正在放置数据进入缓存。
作为缓存工作的class是:
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.ResourceType;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import javax.ejb.Stateless;
import javax.enterprise.inject.spi.CDI;
import java.io.File;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,GenericClassForMap> mapPerson;
private static Timer timer;
public CacheHelper() {
timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 2, 5000);
}
//initialziing cache
public void putInCacheFromDb() {
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
//for map
mapPerson = cacheManager
.createCache("cacheOfmap", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,GenericClassForMap.class,
ResourcePoolsBuilder.heap(100000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
TimeUnit.SECONDS))));
Map<Integer,Person> mp=new HashMap<>();
mp.put(1,new Person(1,"om"));
mapPerson.put(CACHE_MAP_PARAMETER,new GenericClassForMap(mp));
}
//method to clear the cache
public void clearAllCache(){
System.out.println("cache is being cleared........");
cacheManager.close();
}
}
当我 运行 程序并打印为:
时,数据正在缓存中插入 coming from map
[Person{id=1, name='om'}]
我正在为缓存设置定时器,在 15 秒内缓存需要 refreshed.So,我尝试使用 Helper.java class 作为,
import com.cache.cachemanager.CacheHelper;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class Helper extends TimerTask
{
public static int i = 0;
public void run()
{
System.out.println("Timer ran " + ++i);
if(i==15){
try {
Thread.sleep(10000);
System.out.println("system is sleeping");
CacheHelper cacheHelper=new CacheHelper();
cacheHelper.clearAllCache();
cacheHelper.putInCacheFromDb();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我在以下位置收到空指针异常:
system is sleeping
cache is being cleared........
Exception in thread "Timer-6" java.lang.NullPointerException
at com.cache.cachemanager.CacheHelper.clearAllCache(CacheHelper.java:309)
at com.cache.timer.Helper.run(Helper.java:25)
我尝试调试程序,发现空指针异常来自:
cacheManager.close();
我的 cacheManager null.But 当我尝试从 API 请求中清除缓存时,它能够清除 cache.I 我没有使用 spring framework.Why 是我的 cacheManager
是空的,但是当通过 API 调用时它很容易被删除喜欢:
http://localhost:8080/myresource/clearCache ,我的缓存正在被轻松清除。
cache is being cleared........
11:19:31,154 INFO [org.ehcache.core.EhcacheManager] (default task-8) Cache 'cacheOfmap' removed from EhcacheManager.
在调用您的 clearAllCache
方法之前,您创建了一个 new CacheHelper()
。因此,此实例的 cacheManager
属性永远不会被初始化(您只能在 putInCacheFromDb
方法中初始化它)。
我想当你通过 API 调用它时,你有一个 bean/singleton 实例被调用来填充你的缓存,然后永远不会被销毁。所以必须使用这个实例。
编辑 :回答为什么在构造函数中初始化 cacheManager
的问题。
您当前的代码运行如下:
// CacheHelper constructor is called to initialize a new CacheHelper
// => cacheManager attribute is not initialized
CacheHelper cacheHelper = new CacheHelper();
// This method will call cacheManager.close().
// => cacheManager is currently null => NullPointerException
cacheHelper.clearAllCache();
// This method will initialize the cacheManager attribute
// => Too late the Exception already occured
// => So, need to initialize cacheManager attribute before calling clearAllCache()
// => the constructor is the best place to do this here.
cacheHelper.putInCacheFromDb();