This discussion is archived
12 Replies Latest reply: Jul 30, 2013 10:32 AM by user639604 RSS

Obtaining a list of cache names from Coherence

929352 Newbie
Currently Being Moderated
Hello All,
How to I go about getting a list of the NamedCaches that are present in a Coherence cluster at any given time?

I couldn't find any APIs for the same, nor could I find any definitive answers in the forums.

I need to perform the pretty routine task of pre-loading my caches on startup, and therefore I need the names of the caches configured in my coherence-cache-config.xml file.

I do not have any dynamically added caches, so yes, I could pretty much use hard coded cache names in my initialization routine.

I could also parse my config file to avoid hardcoding of the cache names.

But I was wondering whether there was an API from Coherence that could help me out.

Thanks.
  • 1. Re: Obtaining a list of cache names from Coherence
    user738616 Pro
    Currently Being Moderated
    Hi,

    Yes it is possible using the APIs and the code will be somewhat as below:
            Cluster cluster=CacheFactory.ensureCluster();
            for (Enumeration<String> services = cluster.getServiceNames(); services.hasMoreElements(); )
            {
                 String sName = (String) services.nextElement();
                 System.out.println("Service:"+sName);
                 Service service=cluster.getService(sName);             
                 if (service instanceof CacheService){
                      CacheService cService=(CacheService)service;
                      Enumeration<String> caches = cService.getCacheNames();                  
                      for (caches = cService.getCacheNames(); caches.hasMoreElements();){
                           System.out.println("***** Cache:" + caches.nextElement());
                      }
                 }             
            }
    Hope this helps!

    Cheers,
    NJ
  • 2. Re: Obtaining a list of cache names from Coherence
    Jonathan.Knight Expert
    Currently Being Moderated
    Hi

    Actually there are unlikely to be any caches created at start up so the code above will not work; the code only shows you the caches that have been created at any point in time. Also, if you have not started your nodes using DefaultCacheServer then none of the auto-start services may have been created either so cluster.getServiceNames() may not return much either.

    When you start a Coherence node using DefaultCacheServer then all the services in the cache configuration file that have autostart = true will be started but no caches will have been created as caches are only created on demand when they are first asked for.

    There is no way to really tell at start up what caches should be in a cluster as there may be none. The only way to reliably load the correct caches at start up is to have an external list of cache names somewhere; we do this with something like an Enum class that contains all the caches.

    JK
  • 3. Re: Obtaining a list of cache names from Coherence
    user738616 Pro
    Currently Being Moderated
    Hi JK,

    The question was:
    How to I go about getting a list of the NamedCaches that are present in a Coherence cluster at any given time?
    The code that I have provided will serve the purpose.

    Re the initialization of caches, it completely depends how you make a call to "CacheFactory" (lazy or not). Lot of customers use static calls for initialization at startup in which case, also the above code will work.

    Cheers,
    NJ
  • 4. Re: Obtaining a list of cache names from Coherence
    Jonathan.Knight Expert
    Currently Being Moderated
    Hi,

    The original poster also said...
    926349 wrote:
    I need to perform the pretty routine task of pre-loading my caches on startup, and therefore I need the names of the caches configured in my coherence-cache-config.xml file.
    So, no your code would not be valid in that situation as no caches have been created on startup
    user738616 wrote:
    Re the initialization of caches, it completely depends how you make a call to "CacheFactory" (lazy or not). Lot of customers use static calls for initialization at startup in which case, also the above code will work.
    Yes I agree, most people use some form of initialization to create all the caches on startup but I think if the original poster had static initialization of caches then they would already know the cache names they need to load so would not need to ask the cluster for them.


    JK
  • 5. Re: Obtaining a list of cache names from Coherence
    user738616 Pro
    Currently Being Moderated
    Hi,
    I need to perform the pretty routine task of pre-loading my caches on startup, and therefore I need the names of the caches configured in my coherence-cache-config.xml file.
    So, no your code would not be valid in that situation as no caches have been created on startup
    user738616 wrote:
    Re the initialization of caches, it completely depends how you make a call to "CacheFactory" (lazy or not). Lot of customers use static calls for initialization at startup in which case, also the above code will work.
    Yes I agree, most people use some form of initialization to create all the caches on startup but I think if the original poster had static initialization of caches then they would already know the cache names they need to load so would not need to ask the cluster for them.
    Not necessarily, because you might have these static blocks all over the code ... anyways, all depends on the usecase.

    Cheers,
    NJ
  • 6. Re: Obtaining a list of cache names from Coherence
    929352 Newbie
    Currently Being Moderated
    I apologize for not replying to your comments sooner. I thought I had auto notification set up for the threads, but I was wrong.

    Upon running the suggested code sample, I get the following output:
    Service:Cluster
    Service:Management
    Service:DistributedCache

    No caches, because none of the returned service instances are instances of CacheService.
  • 7. Re: Obtaining a list of cache names from Coherence
    user738616 Pro
    Currently Being Moderated
    Hi,

    No the service "Service:DistributedCache" is a type of CacheService but you are not getting any caches probably because no cache has been created in the cluster yet.

    Hope this helps!

    Cheers,
    NJ
  • 8. Re: Obtaining a list of cache names from Coherence
    929352 Newbie
    Currently Being Moderated
    Then I guess the question still stands.

    I need the names of the caches upon application start up so that I can pre-populate them from the database. But since the caches have not yet been created, I cannot programmatically obtain the names of the caches.

    Chicken and egg problem.
  • 9. Re: Obtaining a list of cache names from Coherence
    Jonathan.Knight Expert
    Currently Being Moderated
    Hi,

    Yes it is a little chicken and egg - that is why most people do what has already been mentioned above and have some kind of static set of constants, either in an Interface or Enum that contain the list of all of the caches that an application uses. There are more advantages to doing this than knowing what caches you have at startup; all the code accesses the same constants for cache names, so there are no bugs where people use the wrong name and it makes it easy to change cache names. The only place this does not work is in applications where you have caches dynamically created at runtime and you do not know the names up front - but then you are unlikely to want to load those on startup.

    JK
  • 10. Re: Obtaining a list of cache names from Coherence
    user10460815 Newbie
    Currently Being Moderated

    Hi NJ,

     

    I tried the code you suggested and every service was null. However in the code if I first retrieved a NamedCache

    NamedCache c = CacheFactory.getCache("Some cache");

    Then the service was not null and the code printed the cache name.

     

    Thanks

     

    Jim

  • 11. Re: Obtaining a list of cache names from Coherence
    user10460815 Newbie
    Currently Being Moderated

    Hi,

     

    I know that the caches have been created and populated because if I manually get a cache I can see it is populated.

     

    Thanks again

     

    Jim

  • 12. Re: Obtaining a list of cache names from Coherence
    user639604 Journeyer
    Currently Being Moderated

    This is exactly the issue JK mentioned.   You got null service because you haven't initialize any service yet (beside the cluster service).   The moment you tried the getCache(), you initialized the cache service then the service was not null anymore.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points