0

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());

    }
}
mkovacek
  • 310
  • 2
  • 15
  • What do you mean by "second build/installation"? Restarting the bundle or installing a second bundle that also uses ehcache? – Christian Schneider Jun 24 '19 at 05:05
  • I mean when I install new version of my bundle where I actually use ehcache. So for example, working in local environment, there is no ehcache, ehcache-clustered and my-project bundle, with first installation all of theme works fine. Then I do some code changes (not related to ehcache) and install my-project bundle again, than ehcache stops working – mkovacek Jun 24 '19 at 06:16
  • If I extract this code in some method and call it 2 times the same things happens. First time is ok, after it not – mkovacek Jun 24 '19 at 06:23
  • Did you solve this? – ronnyfm Sep 02 '19 at 22:02

1 Answers1

0

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.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • It's the same, but cacheManager is closed in try with resources cause it implements closeable. There is no shutdown method, just close(). I had before in activate method just to init cacheManager in deactivate method to close it, and some method to try put and get from cache. If I had call more then once it started generate errors. – mkovacek Jun 24 '19 at 11:17
  • Thanks .. did not see this .. hmm .. then I am out of good ideas. – Christian Schneider Jun 24 '19 at 15:56