我有一个 Spring 季启动应用程序 . 我在Spring启动应用程序中使用ehcahce实现了缓存 . 缓存工作正常,但是当触发关闭脚本时,tomcat没有关闭 . 我已经在我的构建中跳过了默认容器,并且我已经使用jstack进行了测试,并且发现ehcache阻止了应用程序关闭 . 我需要在spring boot关闭时为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 not shutting down when Spring boot app is deployed with ehcache但看起来没有任何正确的回应 .
添加配置 . (根据下面的评论)这是我的主要类,我已经配置了@EnableCaching
@SpringBootApplication
@EnableAsync
@EnableCaching
public class Application extends SpringBootServletInitializer implements AsyncConfigurer
{
我的根类路径名ehcache.xml中的ehcache.xml
<?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);
}
1 回答
如何创建一个spring上下文监听器 . 捕获上下文甚至破坏并关闭ehcache .
}
不要忘记将该类注册为spring bean .