简体   繁体   中英

shutdown ehcache on a spring boot application

I have a spring boot application. I have implemented caching using the ehcahce in my spring boot application. The caching is working fine, but the tomcat is not shutting down when the shutdown script is triggered. I have skipped the default container in my build and I have tested using jstack and could find the ehcache is preventing the application to shutdown. I need to implement the shutdown for ehcache when the spring boot shut down. I know I need to implement a shutdown listener for ehcahce. I have tried setting the property in application.properties.

net.sf.ehcache.enableShutdownHook=true

But it did not work out. This should be the last option to try out ideally. I need to try adding a listener in web.xml

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

But as spring boot does not have a web.xml how can implement this listener? Can i do it in webconfig? Any one implemented this please help.

I have looked into some of the older posts Tomcat not shutting down when Spring boot app is deployed with ehcache but does not look like having any proper response.

Adding configuration.(As per comment below) This is my main class, I have configured @EnableCaching

@SpringBootApplication
@EnableAsync
@EnableCaching
public class Application extends SpringBootServletInitializer implements AsyncConfigurer 

{

My ehcache.xml in root class path name 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>

I have configured to load it on start up.

public class ApplicationStartupService implements
        ApplicationListener<ApplicationReadyEvent> {


@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
    //load cache
}

Method annotated with caching.

@Cacheable(value = CACHE_1, key = "#root.target.KEY")
    public Map<String, String> cache1() {

}

in pom.xml I have configured the cache start up.

<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>

As per comment I have tried adding bean and it did not help.

@Bean
    public CacheManager cacheManager() {
        net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager();
        return new EhCacheCacheManager(cacheManager);
    }

How about create a spring context listener. Trap the context destroy even and shutdown the ehcache.

public class SpringEhcacheShutdownListenerBean implements ApplicationListener {

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

}

Don't forget to register the class as a spring bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM