3 Replies Latest reply on Aug 13, 2018 3:09 PM by Paul Ferraro

    I need a demo for "Using Infinispan with WildFly" Please !

    Shady Salah Newbie

      i can create cache but can't lookup the container or cache ,

        • 2. Re: I need a demo for "Using Infinispan with WildFly" Please !
          Shady Salah Newbie

          i tried that :

           

          add to MANIFEST.MF file

          Dependencies: org.infinispan export

          Dependencies:  org.infinispan.commons, org.jboss.as.clustering.infinispan export

           

           

          add to standalone-ha.xml file

          <cache-container name="replicated_cache" default-cache="mycache" module="org.wildfly.clustering.server" jndi-name="infinispan/replicated_cache">

                  <replicated-cache name="mycache" jndi-name="infinispan/replicated_cache/mycache" mode="SYNC">

                    <transaction locking="OPTIMISTIC" mode="FULL_XA"/>

                      <eviction strategy="NONE"/>

                  </replicated-cache>

          </cache-container>

           

           

          then i can lookup

           

          EmbeddedCacheManager embeddedCacheManager;

          Cache<String, Object> cache;

          String cacheName = "mycache";

           

           

          public CacheManager() throws NamingException {

           

          try {

          embeddedCacheManager = InitialContext.doLookup("java:jboss/infinispan/container/replicated_cache");

           

           

          cache = embeddedCacheManager.getCache(mycache);

           

           

          } catch (NamingException exception) {pferraro@Philippe Marschall

          throw exception;

          }

          }

           

           

          all of this is work good

           

           

          but when add <transport lock-timeout="60000"/> like that

           

          <cache-container name="replicated_cache" default-cache="mycache" module="org.wildfly.clustering.server" jndi-name="infinispan/replicated_cache">

          <transport lock-timeout="60000"/>

                  <replicated-cache name="mycache" jndi-name="infinispan/replicated_cache/mycache" mode="SYNC">

                    <transaction locking="OPTIMISTIC" mode="FULL_XA"/>

                      <eviction strategy="NONE"/>

                  </replicated-cache>

          </cache-container>

           

           

          then can't lookup cache because nullpointerException in embeddedCacheManager

          • 3. Re: I need a demo for "Using Infinispan with WildFly" Please !
            Paul Ferraro Master

            When accessing Infinispan resources via JNDI, you should access them via resource references.  This ensures that the associated services are started before you application is deployed (and stopped when your application is undeployed).

             

            e.g.

            In your deployment descriptor, e.g. web.xml, ejb-jar.xml, etc:

             

            <resource-ref>

                <res-ref-name>infinispan/foo</res-ref-name>

                <lookup-name>java:jboss/infinispan/cache/foo/bar</lookup-name><!-- references the "bar" cache of the "foo" container -->

            </resource-ref>

             

            Alternatively, you can reference the default cache of a given container using:

            <resource-ref>

                <res-ref-name>infinispan/foo</res-ref-name>

                <lookup-name>java:jboss/infinispan/cache/foo/default</lookup-name><!-- references the default cache of the "foo" container -->

            </resource-ref>

             

            You can then lookup your cache via JNDI using an application namespace.

            e.g.

            Cache<String, Object> cache = new InitialContext().lookup("java:comp/env/infinispan/foo");

             

            or just use a @Resource annotation:

             

            @Resource(name = "infinispan/foo")

            private Cache<String, Object> cache;

             

             

            If you'd rather create caches via the parent cache manager, you'll need a resource-ref for both the cache container and the cache configuration you intend to use - this ensures that your desired cache configuration is actually installed.  However, if you do this, you must manage the lifecycle of the cache yourself.

             

            e.g.

            <resource-ref>

                <res-ref-name>infinispan/foo</res-ref-name>

                <lookup-name>java:jboss/infinispan/container/foo</lookup-name>

            </resource-ref>

            <resource-ref>

                <res-ref-name>infinispan/config/foo/bar</res-ref-name>

                <lookup-name>java:jboss/infinispan/configuration/foo/bar</lookup-name>

            </resource-ref>

             

            private Cache<String, Object> cache;

             

            @PostConstruct

            public void init() {

               EmbeddedCacheManager manager = new InitialContext().lookup("java:comp/env/infinispan/foo");

               this.cache = manager.getCache("bar");

               this.cache.start();

            }

             

            @PreDestroy

            public void destroy() {

               this.cache.stop();

            }

             

            or, more concisely:

             

            @Resource(name = "infinispan/foo")

            private EmbeddedCacheManager manager;

             

            @PostConstruct

            public void init() {

               this.cache = this.manager.getCache("bar");

               this.cache.start();

            }

             

            @PreDestroy

            public void destroy() {

               this.cache.stop();

            }