6 Replies Latest reply on Oct 29, 2018 12:50 PM by jayaprabahar

    Wildfly 10 Infinispan configuration

    jperezbrockhaus

      I have tried to configure a local cache for the Infinispan subsystem of Wildfly 10. I am using the standalone-full.xml file for it.

      <cache-container name="box" default-cache="myCache" jndi-name="infinispan/box">
           <local-cache name="myCache" jndi-name="infinispan/box/mycache"/>
      </cache-container>
      

      I have a module where it is located a GenericDAO along with the entity manager within it. In this module I have the following CacheBean:

       

      package de.brockhaus.cot.entity;
      
      
      import javax.annotation.Resource;
      import javax.enterprise.inject.Produces;
      import javax.faces.bean.ApplicationScoped;
      
      
      import org.infinispan.Cache;
      
      
      @ApplicationScoped
      public class CacheBean {
         
        @Produces
        @Resource(name="myCache") 
        private Cache<Long, BaseEntity>  cache;
      
      }
      

      This bean is injected into the GenericDAO like this:

       @Inject
          Cache<Long, BaseEntity> cache;
      

      There is a web application that imports the module above. In its pom I have added the following lines:

      <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ejb-plugin</artifactId>
        <version>2.5.1</version>
          <configuration>
            <archive>
              <manifestEntries>
                <Dependencies>org.infinispan org.infinispan.commons org.jboss.as.clustering.infinispan export</Dependencies>
              </manifestEntries> 
              </archive>
            </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
        <warSourceDirectory>src/main/webapp</warSourceDirectory>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <archive>
              <manifestEntries>
                <Dependencies>org.infinispan org.infinispan.commons org.jboss.as.clustering.infinispan export</Dependencies>
              </manifestEntries> 
              </archive>
        </configuration>
        </plugin>
      

      In the web.xml of the web app I have added:

      <resource-ref>  
              <res-ref-name>myCache</res-ref-name>  
              <lookup-name>java:jboss/infinispan/box/mycache</lookup-name>  
         </resource-ref>
      

       

      When I deploy the web app and try to use it, I am getting the following error:

      Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:134)
        at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:88)
        at org.jboss.as.ejb3.component.stateless.StatelessSessionComponent$1.create(StatelessSessionComponent.java:64)
        at org.jboss.as.ejb3.component.stateless.StatelessSessionComponent$1.create(StatelessSessionComponent.java:61)
        at org.jboss.as.ejb3.pool.AbstractPool.create(AbstractPool.java:56)
        at org.jboss.as.ejb3.pool.strictmax.StrictMaxPool.get(StrictMaxPool.java:124)
        at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:47)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:254)
        ... 166 more
      Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: Can not set org.infinispan.Cache field de.brockhaus.cot.entity.CacheBean.cache to org.jboss.as.clustering.infinispan.DefaultCache
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277)
        at org.jboss.as.ejb3.tx.CMTTxInterceptor.requiresNew(CMTTxInterceptor.java:344)
        at org.jboss.as.ejb3.tx.LifecycleCMTTxInterceptor.processInvocation(LifecycleCMTTxInterceptor.java:66)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.as.weld.injection.WeldInjectionContextInterceptor.processInvocation(WeldInjectionContextInterceptor.java:43)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
        at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
        at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
        ... 175 more
      

      I have tried to use the class org.jboss.as.clustering.infinispan.DefaultCache instead of org.infinispan Cache but the error is similar:

      Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: Can not set org.jboss.as.clustering.infinispan.DefaultCache field de.brockhaus.cot.entity.CacheBean.cache to org.jboss.as.clustering.infinispan.DefaultCache
      

      .cache to org.jboss.as.clustering.infinispan.DefaultCache

        • 1. Re: Wildfly 10 Infinispan configuration
          pferraro

          You'll want to use a JEE @Resource annotation instead of CDI.

          e.g.

          @Resource(name = "myCache")
          Cache<Long, BaseEntity> cache;
          

           

          The Infinispan subsystem will bind all caches to JNDI by default, so you don't need to specify a jndi-name in your Infinispan subsystem.

          If you were to use the default jndi-name, your resource-ref would look like:

          <resource-ref>
              <res-ref-name>myCache</res-ref-name>
              <lookup-name>java:jboss/infinispan/cache/box/mycache</lookup-name>
          </resource-ref>
          
          1 of 1 people found this helpful
          • 2. Re: Wildfly 10 Infinispan configuration
            jayaprabahar

            Hi,

             

            Is this question answered? I am also facing the same problem. The solution given by Paul Ferraro, doesn't seem to be related to what you want.

             

            If I access the cache through IntialContext and JNDI, I am able to get normalCache object of type (org.infinispan.Cache) and can convert to advanced cache.

             

            package <a.b.c>;

             

            <some imports>

            import org.infinispan.AdvancedCache;

            import org.infinispan.Cache;

            import org.infinispan.CacheSet;

             

            public class InfinispanServiceImpl {

            private static InfinispanServiceImpl instance;

            private Cache<String, Object> normalCache;

             

            /**

            * Implementing Singleton design pattern.

            *

            * @return InfinispanServiceImpl

            */

            public static synchronized InfinispanServiceImpl getSingleInstance() {

            if (instance == null) {

            synchronized (InfinispanServiceImpl.class) {

            if (instance == null) {

            instance = new InfinispanServiceImpl();

            }

            }

            }

            return instance;

            }

             

            /**

            * Private Constructor

            *

            * @return InfinispanServiceImpl

            */

            @SuppressWarnings("unchecked")

            private InfinispanServiceImpl() {

            try {

            logger.info("Reading infinispan cache from JNDI");

            normalCache = (Cache<String, Object>) new InitialContext().lookup("java:jboss/infinispan/applnCache");

            } catch (NamingException e) {

                 logger.error("Error reading jndi lookup for cache");

            }

            }

             

            /**

            * Return normal cache

            *

            * @return

            */

            public Cache<String, Object> getNormalCache() {

            return normalCache;

            }

             

            /**

            * Return advanced cache. Useful for storing non string objects.

            *

            * @return

            */

            public AdvancedCache<String, Object> getAdvancedCache() {

            if (normalCache != null)

            return normalCache.getAdvancedCache();

            return null;

            }

            }

             

            But if I try to access the same with the following code and try to call getAdvancedCache() method, I am getting the same error "Can not set org.infinispan.Cache field a.b.c.InfinispanServiceImpl.normalCache.cache to org.jboss.as.clustering.infinispan.DefaultCache "

             

            @Singleton

            @Startup

            @Remote(InfinispanServiceImplBeanRemote.class)

            public class InfinispanServiceImpl implements InfinispanServiceImplBeanRemote {

                 @Resource(name = "applnCache")

                   private Cache<String, Object> normalCache;

             

            Also the Infinispan subsystem is not binding the cache to JNDI. I removed JNDI name in the standalone config before making the previous code change,

             

                        <cache-container name="mainCacheCntr" default-cache="applnCache" statistics-enabled="true">

                            <transport lock-timeout="60000"/>

                            <replicated-cache name="applnCache" jndi-name="java:jboss/infinispan/applnCache"  statistics-enabled="true" mode="SYNC">

                                <locking isolation="REPEATABLE_READ"/>

                                <transaction mode="NON_XA"/>

                                <eviction strategy="LRU" max-entries="10000"/>

                                <expiration lifespan="604800"/>

                                <file-store path="applnCacheData" passivation="false" preload="true" purge="false" shared="false"/>

                            </replicated-cache>

                        </cache-container>

            • 3. Re: Wildfly 10 Infinispan configuration
              pferraro

              jayaprabahar Your issue looks like a classloading issue.  Does you application archive include infinispan jars?  If so, remove these.  Instead add a dependency on WF's infinispan modules to your application.

              e.g.

              /META-INF/MANIFEST.MF

               

              Dependencies: org.infinispan

               

              See: wildfly/Class_Loading_in_WildFly.adoc at master · wildfly/wildfly · GitHub

              2 of 2 people found this helpful
              • 4. Re: Wildfly 10 Infinispan configuration
                jayaprabahar

                Thanks pferraro. Removing infinispan jars from the package resolved the issue. "Dependencies: org.infinispan" was alrady added.