Ehcache - 不终止VM

时间:2014-04-10 13:50:19

标签: java ehcache

我有一个简单的一类测试来加载ehache 2.8.1

一切都很好但是如果设置diskPersistent =“true”或overflowToDisk =“true”,则VM永远不会终止。

我已经尝试设置关闭挂钩但是没有触发,因为Ehcache使用的线程不是守护进程所以vm永远不会那么远。

<ehcache 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="/temp"/>
<defaultCache
    maxElementsInMemory="100" 
    eternal="false"
    overflowToDisk="false"
/>

<cache 
    name="resolveIpAddresses" 
    maxElementsInMemory="100000" 
    diskPersistent="true"
    overflowToDisk="true"
    eternal="false"         
    timeToLiveSeconds="3600"                
/>
</ehcache>

ehcache由一个带有bean

的spring 3.1.8容器管理
<cache:annotation-driven />
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="/ehcache.xml" p:shared="true"/>

将磁盘设置更改为false工作正常,但显然不是很好。如何告诉ehcache磁盘线程是守护进程?

1 个答案:

答案 0 :(得分:0)

如果您正在使用servlet容器,则可以使用servlet ShutdownListener,否则可以通过在代码中显式调用CacheManager.getInstance().shutdown()来关闭磁盘线程。

Ehcache documentation表示JVM关闭钩子将:

  

关闭DiskStore。如果DiskStore是持久的,它将写入条目和索引&gt;到磁盘。

除了在最后一个非守护程序线程关闭时调用,还可以通过调用System.exit()显式调用shutdown hook。