2010-07-14 342 views
1

我有一個在Linux上的JDK 1.6.0_17上運行的Java應用程序。我使用的Ehcache 2.1.0,有一個非常簡單的ehcache.xml中的文件:爲什麼jvm重啓後ehcache磁盤存儲失效?

<diskStore path="/ext/ehcache"/> 
<defaultCache 
    overflowToDisk="true" 
    diskPersistent="true" 
    maxElementsInMemory="10000" 
    maxElementsOnDisk="1000000000" 
    eternal="true"> 
</defaultCache> 

在我的Java應用程序,我有這樣的代碼:

private static final String CACHE_NAME = "APP"; 

public static void init() { 
    CacheManager.create(); 
    final CacheManager manager = CacheManager.getInstance(); 
    manager.addCache(CACHE_NAME); 
} 

private static Cache getCache() { 
    final CacheManager manager = CacheManager.getInstance(); 
    return manager.getCache(CACHE_NAME); 
} 

public static Object get(String key) { 
    final Cache cache = getCache(); 
    final Element element = cache.get(key); 
    return (element != null) ? element.getValue() : null; 
} 

private static void put(String key, Object value) { 
    final Cache cache = getCache(); 
    final Element element = new Element(key, value); 
    cache.put(element); 
} 

如果我刪除磁盤存儲文件(/ ext/ehcache)並啓動我的應用程序,我可以看到正在寫入磁盤存儲文件。如果我在應用程序運行時對文件執行「ls -l」,則「ls -l」表示文件大小隨着時間的推移而變大。

然後我用「kill -15」(SIGTERM)來停止我的java應用程序。然後我重新啓動我的Java應用程序,它似乎啓動了。 Java應用程序正在繼續將數據存儲到ehcache中,但很長一段時間,磁盤存儲文件永遠不會增長。 「ls -l」總是顯示相同的文件大小。等了好幾個小時後,文件確實又開始增長了。

我在猜測磁盤存儲中的所有內容都變得無效,並且文件的內容正在重新填充。如果是這樣的話,爲什麼磁盤存儲中的所有內容都會失效?我想在應用程序重新啓動之間保留這些數據。

回答

4

事實證明,我忘記調用CacheManager.shutdown()。添加該呼叫可以解決問題。