2 Replies Latest reply on Oct 13, 2017 2:39 PM by Dominique Toupin

    How to inject a custom Infinispan cache in Wildfly 11.0.0-CR1

    Dominique Toupin Newbie

      Hi,

       

      We recently started using a custom Infinispan cache in Wildfly 10.1.0.

       

      We defined the cache like this:

       

                  <cache-container name="ourCacheContainer" default-cache="localCache">
                      <local-cache name="localCache">
                          <expiration lifespan="1800000"/>
                      </local-cache>
                  </cache-container>
      

       

      In our code, we inject the CacheContainer using @Resource

       

      @Resource(lookup = "java:jboss/infinispan/container/ourCacheContainer")
      

       

      And this works fine.

       

      We tried porting the exact same code to Wildfly 11.0.0-CR1 but then the cache cannot be resolved:

       

      15:04:46,743 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /xxx: org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: WFLYWELD0049: Error injecting resource into CDI managed bean. Can't find a resource named java:jboss/infinispan/container/ourCacheContainer defined on private org.infinispan.manager.CacheContainer CacheService.container
      at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:78)
      at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:222)
      at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:179)
      at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:422)
      at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:213)
      at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:228)
      at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
      at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
      at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:130)
      at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
      at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
      at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
      at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
      at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
      at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
      at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
      at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
      at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
      at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
      at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
      at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
      at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
      at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
      at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
      at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
      at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
      at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
      at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
      at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
      at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
      at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
      at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
      at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
      Caused by: java.lang.IllegalArgumentException: WFLYWELD0049: Error injecting resource into CDI managed bean. Can't find a resource named java:jboss/infinispan/container/ourCacheContainer defined on private org.infinispan.manager.CacheContainer CacheService.container
      at org.jboss.as.weld.services.bootstrap.WeldResourceInjectionServices.resolveResource(WeldResourceInjectionServices.java:225)
      at org.jboss.as.weld.services.bootstrap.WeldResourceInjectionServices$1.createResource(WeldResourceInjectionServices.java:169)
      at org.jboss.weld.injection.AbstractResourceInjection.getResourceReference(AbstractResourceInjection.java:49)
      at org.jboss.weld.injection.AbstractResourceInjection.injectResourceReference(AbstractResourceInjection.java:63)
      at org.jboss.weld.util.Beans.injectEEFields(Beans.java:331)
      at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
      at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
      at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
      at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:117)
      at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
      at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
      at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
      at org.jboss.weld.bean.ContextualInstanceStrategy$ApplicationScopedContextualInstanceStrategy.get(ContextualInstanceStrategy.java:140)
      at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
      at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
      at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
      at com.netappsid.deck.web.configurator.service.CacheService$Proxy$_$$_WeldClientProxy.storeObjectInCache(Unknown Source)
      at com.netappsid.deck.web.configurator.resource.ConfiguratorResource.createNewDeck(ConfiguratorResource.java:207)
      at com.netappsid.deck.web.configurator.resource.ConfiguratorResource.init(ConfiguratorResource.java:116)
      at com.netappsid.deck.web.configurator.resource.ConfiguratorResource$Proxy$_$$_WeldClientProxy.init(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
      at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
      at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
      at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
      at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:406)
      ... 48 more
      Caused by: javax.naming.NameNotFoundException: infinispan/container/ourCacheContainer [Root exception is java.lang.IllegalStateException]
      at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:153)
      at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:83)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
      at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:239)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
      at javax.naming.InitialContext.lookup(InitialContext.java:417)
      at javax.naming.InitialContext.lookup(InitialContext.java:417)
      at org.jboss.as.weld.services.bootstrap.WeldResourceInjectionServices.resolveResource(WeldResourceInjectionServices.java:223)
      ... 76 more
      Caused by: java.lang.IllegalStateException
      at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)
      at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:142)
      at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:46)
      at org.jboss.msc.service.ServiceControllerImpl.getValue(ServiceControllerImpl.java:1158)
      at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:131)
      ... 85 more
      

       

      Given what I have seen under JNDI view (via Wildfly Adminstration console), this is indeed expected, but how can we inject our CacheContainer in Wildfly 11?

       

      Wildfly 10

      Wildfly 11 (notice there is only the ejb container)

       

      Also, I read here that we could use the notation java:jboss/infinispan/cache/<cacheContainer>/<cacheName> but it never worked for us and it is again coherent with the JNDI View...

       

      Thank you for any hints!

        • 1. Re: How to inject a custom Infinispan cache in Wildfly 11.0.0-CR1
          Paul Ferraro Master

          This a long standing bug in the CDI integration.  To workaround this, specify your resource jndi name in your deployment descriptor using a resource-ref or resource-env-ref and inject it via it's application-specific namespace.  This establishes a dependency between the service providing the cache and your application, ensuring the the resource is started and the cache is available via jndi when your application is deployed (and stops the cache when your application is undeployed).

           

          Also, don't inject the cache container, inject the cache itself - this guarantees that the requisite cache configuration is defined.

           

          e.g.

          <resource-env-ref>
              <resource-env-ref-name>cache/localCache</resource-env-ref-name>
              <resource-env-ref-type>org.infinispan.Cache</resource-env-ref-type>
              <lookup-name>java:jboss/infinispan/cache/ourCacheContainer/default</lookup-name>
          </resource-ref>
          

           

          @Resource(name = "java:comp/env/cache/localCache")
          org.infinispan.Cache<K, V> cache;
          
          • 2. Re: How to inject a custom Infinispan cache in Wildfly 11.0.0-CR1
            Dominique Toupin Newbie

            Hi Paul,

             

            We will definitely try that and hope for the best.

             

            Thank you!