ehcache-clustered not working in OSGi when project is installed several times

503 Views Asked by At

Having troubles with clustered ehcache in osgi/aem. Only with first project build/installation it works fine, but with second build/installation it stops working, generate a lot of errors. It looks like terracotta connection, cachemanager or something third is not properly closed.

Even after deleting bundles it tries connect to terracotta.

ok log, errors in log

I'm installing ehcache and ehcache-clustered as standalone bundles in osgi. Also tried with embedding them into my bundle. Ehcache and ehcache-clustered are set as dependencies, also tried with org.apache.servicemix.bundles.javax-cache-api (embedding, not sure if it's needed)

First time all ehcache and ehcache-clustered services are active, 2nd time are satisfied.

Ehcache bundle, ehcache-clustered bundle, javax-cache-api bundle, my project bundle

pom.xml

Same code I have tired as standalone java app and it works perfectly fine (https://github.com/ehcache/ehcache3-samples/blob/master/clustered/src/main/java/org/ehcache/sample/ClusteredXML.java)

So not sure what I have missed (dependencies, import packages..)?

ehcache config, terracotta config

@Activate
private void activate() {
    LOGGER.info("Creating clustered cache manager from XML");

    URL myUrl = ClusteredService.class.getResource("/com/myco/services/ehcache-clustered-local2.xml");
    Configuration xmlConfig = new XmlConfiguration(myUrl);

    try (CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig) ) {
        cacheManager.init();

        org.ehcache.Cache<String, String> basicCache = cacheManager.getCache("basicCache4", String.class, String.class);

        LOGGER.info("1. Putting to cache");
        basicCache.put("1","abc");

        LOGGER.info("1. Getting from cache");
        String value = basicCache.get("1");
        LOGGER.info("1. Retrieved '{}'", value);

        LOGGER.info("cache manager status2, " + cacheManager.getStatus().toString());

    }
}
1

There are 1 best solutions below

2
Christian Schneider On

You have to also create a @Deactivate method where you do a cacheManager.shutdown();

I guess if you call your code in a non OSGi project twice you would also experience the same error.