Ehcache磁盘存储不干净关机 [英] Ehcache disk store unclean shutdown
问题描述
我正在使用具有磁盘存储持久性的缓存。在应用程序的后续重新运行中,我收到以下错误:
I'm using a cache with disk store persistence. On subsequent reruns of the app I'm getting the following error:
net.sf.ehcache.store.DiskStore deleteIndexIfCorrupt
WARNING: The index for data file MyCache.data is out of date,
probably due to an unclean shutdown. Deleting index file MYCache.index
除了明确调用<$ c $之外,还有什么办法可以解决这个问题c> net.sf.ehcache.CacheManager.shutdown()在应用程序的某个地方?
Is there any way to fix that apart from explicitly calling net.sf.ehcache.CacheManager.shutdown()
somewhere in the app?
缓存配置:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect">
<diskStore path="C:\work"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic,
multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=1"
propertySeparator=","
/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>
<defaultCache
maxElementsInMemory="1"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="86400"
overflowToDisk="true"
diskSpoolBufferSizeMB="1"
maxElementsOnDisk="10000"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
复制问题的代码:
import java.util.ArrayList;
import java.util.List;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class CacheTest {
static CacheManager manager = new CacheManager(CacheTest.class
.getResource("ehcache.xml"));
static Cache cache;
public static void main(String[] args) {
// Get a default instance
manager.addCache("test");
cache = manager.getCache("test");
// Generate some junk so that the
// cache properly flushes to disk
// as cache.flush() is not working
List<String> t = new ArrayList<String>();
for (int i = 0; i < 1000; i++)
t.add(null);
// Oddly enough fewer elements
// do not persist to disk or give
// an error
for (int i = 0; i < 100000; i++) {
cache.put(new Element(i, t));
}
cache.flush();
if (cache.get("key1") == null) {
System.out.println("key1 not found in cache!");
cache.put(new Element("key1", "value1"));
}
System.out.println(cache.get("key1"));
}
}
推荐答案
尝试设置系统属性:
net.sf.ehcache.enableShutdownHook = true
因此,您可以添加以下行你的程序的开头:
System.setProperty(net.sf.ehcache.enableShutdownHook,true);
So, either you can add the following line in the beginning of your program:
System.setProperty("net.sf.ehcache.enableShutdownHook","true");
或者,从命令行传递属性:
java -Dnet.sf.ehcache.enableShutdownHook = true ...
Or, from the command line to pass the property:
java -Dnet.sf.ehcache.enableShutdownHook=true ...
注意,使用此关闭钩子时,ehcache网站确实提到了一些注意事项:
关闭Ehcache
Note, the ehcache website does mention some caution when using this shutdown hook: Shutting Down Ehcache
当关闭钩子运行时,当它不会
关闭钩子在以下时间运行:
The shutdown hook runs when:
- 程序正常存在。例如调用System.exit(),或者最后一个非守护程序线程退出
- 虚拟机终止。例如CTRL-C。这对应于kill -SIGTERM pid或在Unix系统上杀死
-15 pid。
关闭钩子不会在:
- 虚拟机中止
- 在Unix上将SIGKILL信号发送到虚拟机进程系统。例如kill -SIGKILL
pid或kill -9 pid - TerminateProcess调用被发送到Windows系统上的进程。
希望它有效:)
这篇关于Ehcache磁盘存储不干净关机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!