我运行后,出现如下错误:
2012-4-20 11:51:43 net.sf.ehcache.store.disk.DiskStorageFactory$DiskWriteTask call
严重: Disk Write of 4 failed:
java.io.NotSerializableException: java.util.RandomAccessSubList
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
at net.sf.ehcache.Element.writeObject(Element.java:797)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream.serialize(MemoryEfficientByteArrayOutputStream.java:97)
at net.sf.ehcache.store.disk.DiskStorageFactory.serializeElement(DiskStorageFactory.java:413)
at net.sf.ehcache.store.disk.DiskStorageFactory.write(DiskStorageFactory.java:392)
at net.sf.ehcache.store.disk.DiskStorageFactory$DiskWriteTask.call(DiskStorageFactory.java:493)
at net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1154)
at net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1138)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
相关的cache管理类代码如下:
其中licensesCache中存放的数据中value为List类型,其余的两个cache为String类型。
上面那个错误什么原因啊,如何改正?谢谢啦~
[code="java"]package com.traffic.cache;
import java.util.List;
import java.util.Timer;
import com.traffic.protocol.LicenseProtocol;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class LicenseCacheManager {
private static CacheManager cacheManager = null;
private static Cache blackListCache = null;
private static Cache whiteListCache = null;
private static Cache licensesCache = null;
private static Timer timer = null;
static {
cacheManager = CacheManager.create("src/config/ehcache.xml");
cacheManager.addCache("blackListCache");
cacheManager.addCache("whiteListCache");
cacheManager.addCache("licensesCache");
licensesCache = cacheManager.getCache("licensesCache");
blackListCache = cacheManager.getCache("blackListCache");
whiteListCache = cacheManager.getCache("whiteListCache");
// 启动定时器,每隔30分钟执行清理缓存任务
timer = new Timer();
timer.scheduleAtFixedRate(new ClearCacheTask(), 300000, 300000);
}
public static void addLicense(int number, List<String> licenses){
licensesCache.put(new Element(number, licenses));
}
public static List<String> getLicenses(int number) {
Element e = licensesCache.removeAndReturnElement(number);
List<String> l =(List<String>)e.getObjectValue();
return l;
}
public static boolean haveLicenses() {
if(licensesCache.getSize() == 0) {
return false;
}
return true;
}
public static void addBlackList(String license) {
Element e = new Element(license, "blackList");
blackListCache.put(e);
}
public static void addWhiteList(String license) {
Element e = new Element(license, "whiteList");
whiteListCache.put(e);
}
// 根据type制定的类型,添加到相应的cache
public static void addItem(String type, String license) {
if(type.equals(LicenseProtocol.WhiteLicenseType)) {
addWhiteList(license);
} else if(type.equals(LicenseProtocol.BlackLicenseType)){
addBlackList(license);
}
}
public static boolean isInBlackList(String license) {
if(blackListCache.isElementInMemory(license)) {
return true;
}
return false;
}
public static boolean isInWhiteList(String license) {
if(whiteListCache.isElementInMemory(license)) {
return true;
}
return false;
}
public static Cache getBlackListCache() {
return blackListCache;
}
public static Cache getWhiteListCache() {
return whiteListCache;
}
public static CacheManager getCacheManager() {
return cacheManager;
}
public static void clear() {
blackListCache.removeAll();
whiteListCache.removeAll();
}
public static void closeManager() {
timer.cancel();
blackListCache.dispose();
whiteListCache.dispose();
cacheManager.shutdown();
}
}
[/code]
ehcache的配置文件为:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xsi:noNamespaceSchemaLocation="ehcache.xsd">
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="10000000"
/>
[/code]