0

I am developing a REST application to read all the caches in a Cluster that uses J Cache with Hazel cast 3.3.3

This application will create another hazel cast node when I call the following line in the application:

cacheManager= Caching.getCachingProvider().getCacheManager();

The node get clustered with already created nodes. But when I try to get all the cache names of the cluster with the following command, It returns an empty iterable:

cacheManager.getCacheNames().iterator()

I went through the Java doc of the Jcache which contained:

May not provide all of the Caches managed by the CacheManager. For example: Internally defined or platform specific Caches that may be accessible by a call to getCache(java.lang.String) or getCache(java.lang.String,java.lang.Class,java.lang.Class) may not be present in an iteration.

But the caches that I am trying to access is not internally defined or platform specific. They are created by other nodes.

I want a way to get all the names present in the cluster. Is there a way to this?

NB: No hazelcast.xml is used in the application. All is initialized by the default xml s.

Update:

I can access the cache if I know the name. And after accessing for the first time by giving the name directly, now it shows that cache in the cacheManager.getCacheNames().iterator()

Shenal
  • 202
  • 5
  • 21

2 Answers2

2

CacheManager only provides names of caches it manages, so you cannot obtain all caches known to the cluster using JCache API.

In Hazelcast 3.7 (EA was released just yesterday), all Caches are available as DistributedObjects, so invoking HazelcastInstance.getDistributedObjects() and then checking for objects being instances of javax.cache.Cache or the Hazelcast-specific subclass com.hazelcast.cache.ICache you should be able to get references to all Caches in the cluster:

// works for 3.7
Collection<DistributedObject> distributedObjects = hazelcastInstance.getDistributedObjects();
for (DistributedObject distributedObject : distributedObjects) {
     if (distributedObject instanceof ICache) {
        System.out.println("Found cache with name " + distributedObject.getName());
     }
}

In Hazelcast 3.6 it is possible to obtain all cache names known to the cluster only using internal classes, so there is no guarantee this will work with any other version.

// works for 3.6 using internal classes, most probably will not work for other versions
public static void main(String[] args) {

    // start a hazelcast instance
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();

    // create a CacheManager and Cache on this instance
    CachingProvider hazelcastCachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider",
            HazelcastCachingProvider.class.getClassLoader());
    CacheManager cacheManager = hazelcastCachingProvider.getCacheManager();
    cacheManager.createCache("test1", new CacheConfig<Object, Object>());

    // hacky: obtain a reference to internal cache service
    CacheDistributedObject cacheDistributedObject = hz.getDistributedObject("hz:impl:cacheService", "setupRef");
    ICacheService cacheService = cacheDistributedObject.getService();

    // obtain all CacheConfig's in the cluster
    Collection<CacheConfig> cacheConfigs = cacheService.getCacheConfigs();
    for (CacheConfig cacheConfig : cacheConfigs) {
        System.out.println("Cache name: " + cacheConfig.getName() + 
            ", fully qualified name: " + cacheConfig.getNameWithPrefix());
    }

    hz.shutdown();
}
1

But the caches that I am trying to access is not internally defined or platform specific

Its good because this method should return all the others and some of the internally defined or platform specific ones.

Pusker György
  • 398
  • 1
  • 11
  • He's correct. It returns all user defined caches but may leave out internal ones that are used for internal purposes. – noctarius Jan 09 '15 at 09:23
  • But it does not return the names. I am not interested in internal caches. What I want are the caches created by other nodes. – Shenal Jan 09 '15 at 09:23