6 Replies Latest reply on Apr 11, 2016 8:26 AM by on-ca

    Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?

    on-ca

      Hi all,

       

      I am trying out Infinispan with the following config.  Now the API promises to return a Cache but the getCache actually returns an infinispan CacheImpl which cannot be cast to Cache.  Your thoughts are appreciated.

       

      Here is the config

      <?xml version="1.0" encoding="UTF-8"?>

      <infinispan xmlns="urn:infinispan:config:8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:infinispan:config:8.0 http://infinispan.org/schemas/infinispan-config-8.0.xsd">

        

         <cache-container default-cache="CEBOnline">

            <transport cluster="demoCluster"/>

            <jmx domain="localhost" />

            <distributed-cache name="CEBOnline" mode="SYNC" l1-lifespan="-1" owners="2" statistics="true">

        <eviction strategy="NONE" />

        <expiration max-idle="1200000" lifespan="-1" interval="60000" />

          </distributed-cache>

         </cache-container>

      </infinispan>

       

      Here is the code snippet

      private static final String location = "D:\\tempWork\\workspace\\Infinisan\\src\\config\\ceb-cache-config.xml";
      private static final String defaultCache = "CEBOnline";
      private static CacheImpl<Object, Object> cache = null;
      private static CacheContainer container = null;

       

       

      public static CacheImpl<Object, Object> getCache() {
      if (cache == null) {
      try {
      container = new DefaultCacheManager(location);
      cache = (CacheImpl<Object, Object>) container.getCache(defaultCache);
      Object obj = container.getCache("default");
      System.out.println(obj.getClass().getName());
      } catch (IOException e) {
      System.out.println(e);
      }
      }
      return cache;
      }

       

      Thanks

        • 1. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
          rhusar

          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

            Cache 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

            1 of 1 people found this helpful
            • 3. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
              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

                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

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

                  1 of 1 people found this helpful
                  • 6. Re: Why does CacheContainer.getCache() return a CacheImpl rather than a Cache?
                    on-ca

                    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