5

Is there a replacement for net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS in

    <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.8.1</version>
    </dependency>

?

This is the code that I need to modify to work with version 3.8.1:

import org.ehcache.CacheManager;
    .
    .
    .
        List list = CacheManager.ALL_CACHE_MANAGERS;

        for (int i = 0, n = list.size(); i < n; i++) {
            CacheManager cm = (CacheManager) list.get(i);
            log.debug("CacheManager: " + cm.getName());

            if (cm.getName().equals(CACHE_MANAGER_NAME)) {
                log.debug("CM " + CACHE_MANAGER_NAME + " existed. Destroying it.");
                cm.shutdown();
            }
        }

Is there a way of doing this in ehcache 3.8.1?

Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
  • Maybe a stupid question, but why do you need to check back with `ALL_CACHE_MANAGERS` in the first place? You have `cm`, so why not assume it exists (why shouldn't it?) and shut it down? – Markus Appel Feb 03 '20 at 16:35
  • I don't have `cm`. `cm` is in the list I get from `CacheManager.ALL_CACHE_MANAGERS`. – Paul Reiners Feb 03 '20 at 16:38
  • My guess is that this "constant" was deemed a bad practice and thus removed. I would suggest maintaining a similar list yourself, adding new `CacheManager`s to the list as you create them (maybe abstracted away as a service). This is probably what the developers tried to force you to do in the first place. – Markus Appel Feb 03 '20 at 16:53
  • are you using JCache? There's a `Caching.getCachingProviders()` method which might work - the javadoc says it returns the all caching providers (and through that the cache managers).. – stringy05 Feb 06 '20 at 04:30
  • I'm using ehcache 3.8.1. Is that JCache? What is the package name for `Caching`? – Paul Reiners Feb 06 '20 at 15:31
  • I think that @stringy05 is referring to this: https://www.ehcache.org/documentation/3.8/107.html – Victor Feb 06 '20 at 20:53
  • JCache is the java spec for caching, which ehcache supports, a bit like how you can do dependency injection with either spring or javax CDI. You might be able to add jcache to your project and use it's API to access the underlying ehcache implementation. It's a long shot... – stringy05 Feb 06 '20 at 22:15

2 Answers2

0

That should work:

import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.spi.CachingProvider;

CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();          
System.out.println(cacheManager); // For my case: Eh107CacheManager[urn:X-ehcache:jsr107-default-config]
jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
-1

It looks ALL_CACHE_MANAGERS is replaced with org.ehcache.clustered.operations command ListCacheManagers in ehcache 3.8.1.

It can be used as follows.

OperationsTool.main(...)

public static void main(String[] args) {
 System.exit(innerMain(args));
}

OperationsTool.innerMain(...)

public static int innerMain(String[] args) {
 BaseOptions base = new BaseOptions();
 JCommander jc = new JCommander(base);
 jc.setProgramName("ehcache-ops");
 jc.addCommand(new ListCacheManagers(base));
 jc.addCommand(new CreateCacheManager(base));
 jc.addCommand(new UpdateCacheManager(base));
 jc.addCommand(new DestroyCacheManager(base));
 jc.setParameterDescriptionComparator(REQUIRED_FIRST);
 for (JCommander jcc : jc.getCommands().values()) {
  jcc.setParameterDescriptionComparator(REQUIRED_FIRST);
 }
 try {
  jc.parse(args);
  if (base.isHelp()) {
   return usage(jc, new StringBuilder());
  } else {
   int result = 0;
   for (Object o : jc.getCommands().get(jc.getParsedCommand()).getObjects()) {
    result |= ((Command) o).execute();
   }
   return result;
  }
 } catch (ParameterException e) {
  return usage(jc, new StringBuilder(e.getMessage()).append("\n"));
 }
}

OperationsToolTest.run(...)

 public static int run(String command) {
  return OperationsTool.innerMain(command.split("\\s+"));
 }
}

Reference:

  1. https://www.codota.com/web/assistant/code/rs/5c76b38b49efcb0001524b15#L36
  2. https://www.codota.com/code/java/classes/org.ehcache.clustered.operations.OperationsTool
Devesh mehta
  • 1,505
  • 8
  • 22