[英]shutdown ehcache on a spring boot application
我有一個春季啟動應用程序。 我在Spring啟動應用程序中使用ehcahce實現了緩存。 緩存工作正常,但是當關閉腳本被觸發時,tomcat沒有關閉。 我已經在我的構建中跳過了默認容器,並且我已經使用jstack進行了測試,並且發現ehcache阻止了應用程序關閉。 當彈簧啟動關閉時,我需要為ehcache實現關閉。 我知道我需要為ehcahce實現一個關閉監聽器。 我試過在application.properties中設置屬性。
net.sf.ehcache.enableShutdownHook=true
但它沒有成功。 這應該是理想情況下嘗試的最后一個選項。 我需要嘗試在web.xml中添加一個監聽器
<listener>
<listener-class>
net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener>
但是由於spring boot沒有web.xml,如何實現這個監聽器呢? 我可以在webconfig中執行此操作嗎? 任何人實施此請幫助。
我已經研究了一些較舊的帖子Tomcat在使用ehcache部署Spring啟動應用程序時沒有關閉,但看起來沒有任何正確的響應。
添加配置。(根據下面的評論)這是我的主要類,我已經配置了@EnableCaching
@SpringBootApplication
@EnableAsync
@EnableCaching
public class Application extends SpringBootServletInitializer implements AsyncConfigurer
{
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10" eternal="false"
timeToIdleSeconds="1200" timeToLiveSeconds="600" overflowToDisk="true" />
<cache name="cache1" maxElementsInMemory="60000" eternal="false"
overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="43200" memoryStoreEvictionPolicy="LFU"/>
<cache name="cache2" maxElementsInMemory="500" eternal="false"
overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="43200" memoryStoreEvictionPolicy="LFU"/>
</ehcache>
public class ApplicationStartupService implements
ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
//load cache
}
用緩存注釋的方法。
@Cacheable(value = CACHE_1, key = "#root.target.KEY")
public Map<String, String> cache1() {
}
在pom.xml中我已經配置了緩存啟動。
<packaging>war</packaging>
<name>myapp</name>
<description>my test application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jcl.slf4j.version>1.7.12</jcl.slf4j.version>
<logback.version>1.1.3</logback.version>
<rootDir>${project.basedir}</rootDir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
根據評論,我嘗試添加bean,它沒有幫助。
@Bean
public CacheManager cacheManager() {
net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager();
return new EhCacheCacheManager(cacheManager);
}
如何創建一個spring上下文監聽器。 捕獲上下文甚至破壞並關閉ehcache。
public class SpringEhcacheShutdownListenerBean implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
// now you can do ehcache shutdown
// ...
}
}
}
不要忘記將該類注冊為spring bean。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.