5 Replies Latest reply on Aug 23, 2011 7:53 AM by Kevin Pollet

    CDI extension and distributed cache

    Vinicius Carvalho Apprentice

      Hi. I'm trying to get the CDI extensions (5.1.0-SNAPSHOT) to work as described no the docs. If I use the exact same config it does work, but I need a clustered cache, and hence I use this config instead:

       

       

      @Infinispan("sample")
                @ClusteredCache
                @Produces
                public Configuration getDefault(){
                          GlobalConfiguration gc = GlobalConfiguration.getClusteredDefault();
                          gc.fluent().transport().clusterName("shared-cache-cluster").addProperty("configurationFile", "jgroups-tcp.xml");
                          Configuration c = new Configuration().fluent().clustering().mode(CacheMode.REPL_ASYNC).build();
                          c.setGlobalConfiguration(gc);
                          return c;
                }
      
      
      //on the service:
      @Inject @ClusteredCache
                private Cache<String,String> cache;
        
                @POST
                @Path("/{key}")
                public void put(@PathParam(value="key") String key, @QueryParam("value") String value){
                          cache.put(key, value);
                }
      
      
      

       

       

      That exact configuration snippet works if I decide to use a regular CDI produces method to create a CacheManager and then return a cache. So I believe it's valid.

       

      But using the CDI extension, when I try to put something on the cache:

       

       

      18:39:10,635 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/cache].[com.fb.cache.CacheApp]] (http-127.0.0.1-127.0.0.1-8080-1) Servlet.service() for servlet com.fb.cache.CacheApp threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Exception invoking method [getAdvancedCache] on object [org.infinispan.cdi.CacheManager@1aff9e8], using arguments [Manager
      Enabled alternatives: [] []
      Registered contexts: [interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.inject.Singleton, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.Dependent]
      Registered beans: 129
      Specialized beans: 0
      ]
                at org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:345) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:321) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:534) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.2.1.GA.jar:]
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
                at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
                at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.0.0.Final.jar:7.0.0.Final]
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [jbossweb-7.0.0.CR4.jar:7.0.0.Final]
                at java.lang.Thread.run(Thread.java:619) [:1.6.0_21]
      Caused by: java.lang.RuntimeException: Exception invoking method [getAdvancedCache] on object [org.infinispan.cdi.CacheManager@1aff9e8], using arguments [Manager
      Enabled alternatives: [] []
      Registered contexts: [interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.inject.Singleton, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.Dependent]
      Registered beans: 129
      Specialized beans: 0
      ]
                at org.jboss.seam.solder.reflection.Reflections.invokeMethod(Reflections.java:547) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.reflection.Reflections.invokeMethod(Reflections.java:458) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.reflection.annotated.InjectableMethod.invoke(InjectableMethod.java:187) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.reflection.annotated.InjectableMethod.invoke(InjectableMethod.java:139) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.bean.generic.GenericProducerMethod.getValue(GenericProducerMethod.java:77) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.bean.generic.AbstractGenericProducerBean.create(AbstractGenericProducerBean.java:70) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.seam.solder.bean.generic.GenericProducerMethod.create(GenericProducerMethod.java:42) [seam-solder-3.0.0.Final.jar:]
                at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:67) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:693) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:775) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:138) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:875) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:887) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1$1.proceed(ManagedBean.java:182) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:54) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1.work(ManagedBean.java:176) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.ManagedBean$FixInjectionPoint.run(ManagedBean.java:142) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.inject(ManagedBean.java:170) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.resteasy.cdi.JaxrsInjectionTarget.inject(JaxrsInjectionTarget.java:36) [resteasy-cdi-2.2.1.GA.jar:]
                at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:339) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:122) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at com.fb.cache.CacheEndpoint$Proxy$_$$_WeldClientProxy.put(CacheEndpoint$Proxy$_$$_WeldClientProxy.java) [classes:]
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_21]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_21]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_21]
                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_21]
                at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:255) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:220) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:209) [resteasy-jaxrs-2.2.1.GA.jar:]
                at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:519) [resteasy-jaxrs-2.2.1.GA.jar:]
                ... 24 more
      Caused by: org.infinispan.config.ConfigurationException: Cache cannot use a clustered mode (REPL_ASYNC) mode and not define a transport!
                at org.infinispan.config.Configuration.assertValid(Configuration.java:1547) [infinispan-core-5.1.0-20110807.185553-4.jar:]
                at org.infinispan.manager.DefaultCacheManager.wireCache(DefaultCacheManager.java:606) [infinispan-core-5.1.0-20110807.185553-4.jar:]
                at org.infinispan.manager.DefaultCacheManager.createCache(DefaultCacheManager.java:568) [infinispan-core-5.1.0-20110807.185553-4.jar:]
                at org.infinispan.manager.DefaultCacheManager.getCache(DefaultCacheManager.java:453) [infinispan-core-5.1.0-20110807.185553-4.jar:]
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_21]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_21]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_21]
                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_21]
                at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:48) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:125) [weld-core-1.1.2.AS7.jar:2011-07-06 12:26]
                at org.jboss.weld.proxies.CacheContainer$EmbeddedCacheManager$Lifecycle$Listenable$1328373168$Proxy$_$$_WeldClientProxy.getCache(CacheContainer$EmbeddedCacheManager$Lifecycle$Listenable$1328373168$Proxy$_$$_WeldClientProxy.java) [weld-core-1.1.2.AS7.jar:]
                at org.infinispan.cdi.CacheManager.getAdvancedCache(CacheManager.java:87) [infinispan-cdi-5.1.0-20110807.190812-1.jar:]
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_21]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_21]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_21]
                at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_21]
                at org.jboss.seam.solder.reflection.Reflections.invokeMethod(Reflections.java:535) [seam-solder-3.0.0.Final.jar:]
                ... 56 more
      
      

       

      The error happens inside the CacheManager from CDI:

       

       

        @Produces
         @ApplyScope
         public <K, V> AdvancedCache<K, V> getAdvancedCache(BeanManager beanManager) {
            final String name = infinispan.value();
            Cache<K, V> cache;
      
      
            if (name.isEmpty()) {
               cache = getCacheContainer().getCache();
            } else {
               cache = getCacheContainer().getCache(name); // -> Error is thrown here
            }
      
      
            cacheEventBridge.registerObservers(
                  getQualifiers(beanManager, annotatedMember.getAnnotations()),
                  cache
            );
      
      

       

       

      I'm using JBoss 7, but I'm bundling the entire Infinispan core + libs inside + war file and the other ear file on another node.

       

      Any ideas?

       

      Regards

        • 1. Re: CDI extension and distributed cache
          Kevin Pollet Newbie

          Hi,

           

          I'm not sure to understand what you are doing with the CacheManager producer. Could you provide a snippet of code?

           

          Are you sure this error is related to CDI support because at the end of the stacktrace it seems to be a Configuration error.

           

          Caused by: org.infinispan.config.ConfigurationException: Cache cannot use a clustered mode (REPL_ASYNC) mode and not define a transport! 

           

          Hope this help.

           

          --Kevin

          • 2. Re: CDI extension and distributed cache
            Vinicius Carvalho Apprentice

            Hi Kevin. The problem is that when I build the configuration using a different approach it works:

             

             

            @Produces
              @ApplicationScoped
              public EmbeddedCacheManager getCacheManager() {
              GlobalConfiguration gc = GlobalConfiguration.getClusteredDefault();
              gc.fluent().globalJmxStatistics().transport().clusterName("shared-cache-cluster").addProperty("configurationFile", "jgroups-tcp.xml");
              Configuration c = new Configuration().fluent().jmxStatistics().clustering().mode(CacheMode.REPL_ASYNC).build();
            
              return new DefaultCacheManager(gc,c);
              }
            
            

             

            What I could get from the extension is that it will construct a cachemanager using the provided configuration annotated with @Infinispan. But why when using the configuration directly on the cachemanager as shown above it works, but not this code (from the cdi-extension) :

             

             

             // if the cache name is empty this is the default configuration. This configuration is registered
                     // by default in the default cache manager.
                     if (!cacheName.isEmpty() && configuration != null) {
                        cacheManager.get().defineConfiguration(cacheName, configuration);
                     }
            //Debuging I was able to see the the injected DefaultCacheManager gets my configuration.
            //But later on when the cache is injected into the code the error is thrown:
            
             @Produces
               @ApplyScope
               public <K, V> AdvancedCache<K, V> getAdvancedCache(BeanManager beanManager) {
                  final String name = infinispan.value();
                  Cache<K, V> cache;
            
            
                  if (name.isEmpty()) {
                     cache = getCacheContainer().getCache();
                  } else {
                     cache = getCacheContainer().getCache(name); 
                  }
            
            
            • 3. Re: CDI extension and distributed cache
              Kevin Pollet Newbie

              I've attached a test project which works fine for me.

              Could you try to run it on JBoss AS7? (use the following command: mvn clean package jboss-as:deploy)

               

              --Kevin

              • 4. Re: CDI extension and distributed cache
                Vinicius Carvalho Apprentice

                Hi Kevin that worked. Thank you VERY much for your time, But now I'm a bit confused, would you mind clearing things up a bit? On your code:

                 

                 @Clustered
                   @Infinispan("clustered")
                   @Produces
                   public Configuration clusteredConfiguration;
                
                
                   @Produces
                   @ApplicationScoped
                   @OverrideDefault
                   public EmbeddedCacheManager defaultCacheManager() {
                
                
                      return new DefaultCacheManager(
                            GlobalConfiguration.getClusteredDefault().fluent()
                            .transport().clusterName("shared-cache-cluster").addProperty("configurationFile", "jgroups-tcp.xml")
                            .build() ,
                
                
                            new Configuration().fluent()
                            .clustering().mode(Configuration.CacheMode.REPL_ASYNC)
                            .build()
                      );
                   }
                
                

                 

                 

                I can not understand why on my version using the @Infinispan and producing a configuration with the clustered setup does not work, while producing a CacheManager with the configuration it works.

                 

                What I understood from the docs is that:

                 

                You either produce a @Infinispan configuration and then, all the @Inject @YourCacheQualifier would inject a cache configured from a cachemanger using your configuration OR you Override the Cachemanager with your version, and every @Inject Cache would get a cache from your cache manager.

                 

                So Is the @Infinispan + @Produces/OverrideDefault required?

                 

                Again, thanks a lot for that, you guys really Rock! This extension is really gonna help me a lot

                • 5. Re: CDI extension and distributed cache
                  Kevin Pollet Newbie

                  You either produce a @Infinispan configuration and then, all the @Inject @YourCacheQualifier would inject a cache configured from a cachemanger using your configuration OR you Override the Cachemanager with your version, and every @Inject Cache would get a cache from your cache manager.

                   

                  Exactly

                   

                   

                  So Is the @Infinispan + @Produces/OverrideDefault required?

                   

                  No it's not.

                   

                  Your example doesn't work because the GlobalConfiguration has to be set at the CacheManager level. Maybe you could be interested to use a specific CacheManager (with the GlobalConfiguration) like it's described here https://docs.jboss.org/author/display/ISPN/CDI+Support#CDISupport-Supplyacustomcachemanagerforoneormorecaches

                   

                  Hope this help.

                   

                  --Kevin