关闭spring启动应用程序上的ehcache

关闭spring启动应用程序上的ehcache,spring,spring-boot,ehcache,Spring,Spring Boot,Ehcache,我有一个spring启动应用程序。我已经在spring boot应用程序中使用ehcahce实现了缓存。缓存工作正常,但触发关闭脚本时tomcat没有关闭。我跳过了构建中的默认容器,并使用jstack进行了测试,可能发现ehcache正在阻止应用程序关闭。我需要在spring启动关闭时实现ehcache的关闭。我知道我需要为ehcahce实现一个关机侦听器。我已尝试在application.properties中设置属性 net.sf.ehcache.enableShutdownHook=tru

我有一个spring启动应用程序。我已经在spring boot应用程序中使用ehcahce实现了缓存。缓存工作正常,但触发关闭脚本时tomcat没有关闭。我跳过了构建中的默认容器,并使用jstack进行了测试,可能发现ehcache正在阻止应用程序关闭。我需要在spring启动关闭时实现ehcache的关闭。我知道我需要为ehcahce实现一个关机侦听器。我已尝试在application.properties中设置属性

net.sf.ehcache.enableShutdownHook=true
但它没有成功。这应该是最后一个理想的选择。我需要尝试在web.xml中添加一个侦听器

    <listener> 
    <listener-class> 
       net.sf.ehcache.constructs.web.ShutdownListener</listener-class> 
   </listener>
{

根类路径名ehcache.xml中的My ehcache.xml
创建一个spring上下文侦听器怎么样?捕获上下文并关闭ehcache

public class SpringEhcacheShutdownListenerBean implements ApplicationListener {

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextClosedEvent) {
        // now you can do ehcache shutdown
        // ...
    }
}
}


不要忘记将该类注册为Springbean。

创建一个spring上下文侦听器怎么样。捕获上下文并关闭ehcache

public class SpringEhcacheShutdownListenerBean implements ApplicationListener {

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextClosedEvent) {
        // now you can do ehcache shutdown
        // ...
    }
}
}


别忘了将该类注册为Springbean。

您如何使用EhCache?如果您正在使用Spring提供程序类来引导,那么EhCache关闭将得到处理。我正在使用Spring引导,如果EhCache.xml位于类路径中,Spring引导将自动创建缓存管理器。这也将关闭缓存。。。这是由创建ehcache的适当工厂来处理的。我本以为会这样,但不幸的是,事情不是这样发生的。我没有收到任何错误,但我的tomcat没有停止,当我检查它时,它只是因为ehcache。那,好吧,不应该发生。什么是阻塞?您可以添加您的配置和可能的pom吗。另外,您使用的是哪个EhCache版本。您如何使用EhCache?如果您正在使用Spring提供程序类来引导,那么EhCache关闭将得到处理。我正在使用Spring引导,如果EhCache.xml位于类路径中,Spring引导将自动创建缓存管理器。这也将关闭缓存。。。这是由创建ehcache的适当工厂来处理的。我本以为会这样,但不幸的是,事情不是这样发生的。我没有收到任何错误,但我的tomcat没有停止,当我检查它时,它只是因为ehcache。那,好吧,不应该发生。什么是阻塞?您可以添加您的配置和可能的pom吗。另外,您使用的是哪个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() {
<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
    public CacheManager cacheManager() {
        net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager();
        return new EhCacheCacheManager(cacheManager);
    }
public class SpringEhcacheShutdownListenerBean implements ApplicationListener {

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextClosedEvent) {
        // now you can do ehcache shutdown
        // ...
    }
}