Monday 27 June 2022

Ehcache: Start all the CacheManager services

CacheManager#init() method will start all the services managed by this CacheManager and initialize all Caches registered with it.

 

Signature

void init() throws StateTransitionException;

 


There are two ways to initialize the CacheManager.

 

a. At the time of CacheManager creation.

CacheManagerBuilder.newCacheManagerBuilder().build(true);

Above snippet Builds a CacheManager and initializes it. If you do not want to initialize it at the time of creation, pass the argument false to the build method.

 

b. Using init() method post CacheManager creation.

 

You can’t create a Cache until CacheManager is initialized.

 

Find the below working application.

 

CacheManagerInitiDemo.java

 

package com.sample.app;

import org.ehcache.CacheManager;
import org.ehcache.Status;
import org.ehcache.config.builders.CacheManagerBuilder;

public class CacheManagerInitiDemo {

	public static void main(String[] args) {
		CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(false);

		Status status = cacheManager.getStatus();
		System.out.println("CacheManager status is set to " + status);

		System.out.println("Initializing CacheManager");
		cacheManager.init();
		
		status = cacheManager.getStatus();
		System.out.println("CacheManager status is set to " + status);
		
		cacheManager.close();

	}

}

 

Output

CacheManager status is set to UNINITIALIZED
Initializing CacheManager
CacheManager status is set to AVAILABLE

 

 

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment