-
1. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
rhusar Apr 8, 2016 5:00 AM (in response to on-ca)Here is a pretty good book to get started with Java: http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683
PS: Cache is an interface, so cache manager will return an implementation of that interface which is CacheImpl.
infinispan/CacheContainer.java at master · infinispan/infinispan · GitHub
infinispan/Cache.java at master · infinispan/infinispan · GitHub
-
2. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
nadirx Apr 8, 2016 5:03 AM (in response to on-ca)1 of 1 people found this helpfulCache is an interface, CacheImpl is the implementation of it. The signature for CacheContainer.getCache() is
<K, V> Cache<K, V> CacheContainer.getCache();
so it should work. Can you show us your imports, so we can determine the fully-qualified class names you're using ?
Tristan
-
3. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
on-ca Apr 8, 2016 6:19 PM (in response to on-ca)Hi guys,
I do understand about Interfaces and implementors. Issue is I get a runtime exception caching from what cacheManager.getCache returned CacheImpl into the javax.cache.Cache.
Here is my code...
/**
*
*/
package com.fisglobal.cache.infinispan;
import java.io.IOException;
import javax.cache.Cache;
import org.infinispan.cache.impl.CacheImpl;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
/**
* @author lk
*
*/
public class CeBCacheSingle {
private static final String location = "D:\\tempWork\\workspace\\LisInfinispan\\src\\config\\ceb-cache-config.xml";
private static final String defaultCache = "CEBOnline";
private static Cache<Object, Object> cache = null;
private static DefaultCacheManager cacheManager = null;
public static Cache<Object, Object> getCache() {
if (cache == null) {
try {
cacheManager = new DefaultCacheManager(location);
AsyncCacheMgrListener mgrListener = new AsyncCacheMgrListener();
cacheManager.addListener(mgrListener);
cache = (Cache<Object, Object>) cacheManager.getCache(defaultCache);
AsyncCacheListener listener = new AsyncCacheListener();
// cache.registerCacheEntryListener();
System.out.println(cache.getClass().getName());
} catch (IOException e) {
System.out.println(e);
}
}
return cache;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Cache cache = CeBCacheSingle.getCache();
cache.put("101", "101");
String result = (String)cache.get("101");
System.out.println("result=" + result);
Thread.sleep(10000);
System.out.println("Exiting...");
System.exit(0);
}
}
Here is the output.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Exception in thread "main" Event - Cache Manager CACHE_STARTED for cache CEBOnline
java.lang.ClassCastException: org.infinispan.cache.impl.CacheImpl cannot be cast to javax.cache.Cache
at com.fisglobal.cache.infinispan.CeBCacheFactory.getCache(CeBCacheFactory.java:33)
at com.fisglobal.cache.infinispan.CeBCacheFactory.main(CeBCacheFactory.java:51)
Also javax.cache.Cache doesn't promise "addListener" so am confused so I commented out the addListener to the cache above, I thought Infinispan was JSR107 and adheres to the javax.cache.Cache. It appears I may not be using the right interface.
Sincerely
on-ca
-
4. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
on-ca Apr 8, 2016 7:04 PM (in response to nadirx)Ah ok appreciate your thoughts all,
Yes I was using the wrong interface of javax.cache.Cache. Once I changed to org.infinispan.Cache the sample app can cast from the CacheImpl to Cache.
Puzzling though cause isn't JSR107 promising the javax.cache.Cache interface? What's the point if we're tied to org.infinispan.Cache. AKA what if tomorrow I want to use another cache but now my client code is tied to and using org.infinispan.Cache..... This awesome generic interface is now tied to a specific implementation, or did I miss something...
on ca
-
5. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
nadirx Apr 9, 2016 3:38 AM (in response to on-ca)1 of 1 people found this helpfulI thought you might be wanting that, and that's why I asked you for the fully-qualified names.
If you want a javax.cache.Cache you need to obtain your cache using the JCache API as described here: http://infinispan.org/tutorials/simple/jcache/
Obviously, if you want to use Infinispan's advanced features (like streams or transactions) you will need to unwrap the javax.cache.Cache using the standard method:
<T> T unwrap(java.lang.Class<T> clazz);
so:
javax.cache.Cache myJCache = ...;
org.infinispan.Cache myInfinispanCache = myJCache.unwrap(org.infinispan.Cache);
-
6. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
on-ca Apr 11, 2016 8:26 AM (in response to nadirx)Thank you Tristan,
This answers the issue completely even though my original question was asked in a not so direct way (which it should have).
Once again, thank you for your time, thoughts and expertise.
Sincerely,
on ca