MFIhsan
MFIhsan

Reputation: 1057

How to shutdown Ehcache CacheManager in standalone application

I have a simple API that my clients use in a standalone application. Behind the scenes my API uses Ehcache for performance.

Everything works fine except that my client needs to invoke a shutdown() method in my API to invoke CacheManager.shutdown() without which Ehcache continues to run in the background even though the main thread is completed.

Is there a way I can avoid this extra shutdown() call for my client?

I tried using @PreDestroy Spring annotation to invoke this call, but it didn't work?

Here I am adding a sample client program.

public class ClientApp{
    @Autowired
    private IClientService service;

    public static void main(String[] args){
        try{
            service.getClients();
            ...
        } finally {
            service.shutdown(); // to shutdown the cache background thread
        }
    }
}

In ClientServiceImpl.java, I have the following lines

public void shutdown(){
    LOGGER.info("Shutting the cache down...");
    ehcacheManager.shutdown();
}

Upvotes: 1

Views: 3397

Answers (2)

MFIhsan
MFIhsan

Reputation: 1057

Adding the following line does what I was looking for.

System.setProperty(CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY, "true");

Upvotes: 1

Louis Jacomet
Louis Jacomet

Reputation: 14500

Your example confirms the standalone application setup. Ehcache should not prevent the JVM from shutting down when the main thread terminates.

If it does, you will need to add thread dumps to this issue so we can analyse further the issue and its cause.

Upvotes: 1

Related Questions