[英]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.