6 Replies Latest reply on Oct 21, 2017 9:58 AM by pferraro

    Access WildFly Infinispan Cache Container from Web Application

    andreap1

      Hi all,

       

      we are trying to access Infinispan CacheManager in order to use it from a Web Application

       

      Is there a way to do this ?

       

      We followed some examples found around on SO and other sites but up to now no luck.

       

      This is how we are proceeding:

       

      The servlet code:

       

      @WebServlet("/InfinispanWeb")

      public class InfinispanWeb extends HttpServlet {

          private static final long serialVersionUID = 1L;

           

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

          private CacheContainer cc;

       

           /* NOTE: up to now, no code is using CacheContainer cc .... */

       

       

       

      META-INF/MANIFEST.MF

       

      Dependencies: org.infinispan export

       

       

      The configuration of mycache container in WildFly domain.xml (full profile)

       

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

           <transport lock-timeout="60000"/>

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

      </cache-container>

       

      We use WildFly web console to deploy the war, resulting in a deployment failure and the following error in the log file

       

      2017-09-28 09:41:37,456 INFO  [org.jboss.as.repository] (ServerService Thread Pool -- 90) WFLYDR0002: Content removed from location C:\wildfly-10.1.0.Final\domain\servers\server-one\data\content\6b\6737b6192d9a71fa7acfb262e5928bf922b8ff\content

      2017-09-28 09:41:44,743 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) WFLYSRV0027: Starting deployment of "InfinispanWeb.war" (runtime-name: "InfinispanWeb.war")

      2017-09-28 09:41:44,783 WARN  [org.jboss.as.dependency.private] (MSC service thread 1-8) WFLYSRV0018: Deployment "deployment.InfinispanWeb.war" is using a private module ("org.infinispan:main") which may be changed or removed in future versions without notice.

      2017-09-28 09:41:44,870 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 90) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "InfinispanWeb.war")]) - failure description: {"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}

      2017-09-28 09:41:44,870 ERROR [org.jboss.as.server] (ServerService Thread Pool -- 90) WFLYSRV0021: Deploy of deployment "InfinispanWeb.war" was rolled back with the following failure message: {"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}

      2017-09-28 09:41:45,110 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0028: Stopped deployment InfinispanWeb.war (runtime-name: InfinispanWeb.war) in 238ms

       

      Can someone point us to what are we doing wrong ?

      Is this a feasible way for using Infinispan data grid ?

       

      Thanks a lot for your support....

       

       

      -------------------------------------------------

      Andrea

        • 1. Re: Access WildFly Infinispan Cache Container from Web Application
          ryanemerson

          Hi Andrea,

           

          It is possible to consume infinispan from a wildfly application, however it appears that you're trying to utilise the version of Infinispan that is included with Wildfly. This is meant for internal use only and should not be used by applications.

           

          To use the latest version of Infinispan with your application in wildfly you should utilise the wildfly-modules feature pack.  Details on the required deployment steps, jndi lookups and manifest dependencies can be found here.

           

          Unfortunately since Infinispan 9.1.0.Final, the wildfly-module package has the following bug which will result in some errors upon wildfly startup, so depending on your needs it may be best to wait for 9.1.2.Final to be released.

          • 2. Re: Access WildFly Infinispan Cache Container from Web Application
            andreap1

            Hello Ryan

             

            thanks a lot for your reply, actually after looking around for some

            time on various sites and docs I landed to the same infinispan guide

            chapter you mentioned. I followed the guide and finally I had it

            working. I have to say that I did not notice any error on wildfly

            startup, but I only included infinispan core as subsytem, not

            infinispan endpoints. I am using wildfly 10.1.0 Final + Infinispan

            9.1.0 final modules.

             

            Anyway, can you please tell me when 9.1.2 will be out ?

             

            Thanks again !

             

            Andrea

            • 3. Re: Access WildFly Infinispan Cache Container from Web Application
              ryanemerson

              Sorry about the slow response, we aim to release 9.1.2 by the end of next week (27/10/17).

              • 4. Re: Access WildFly Infinispan Cache Container from Web Application
                andreap1

                Hello Ryan,

                 

                thanks a lot for your answer. I have another question for you:

                is it possible to build (remote) queries in Infinispan which

                can work on cache keys ? We have seen examples

                on querying object values (the "from" clause) but

                nothing about keys....

                 

                Thanks a lot !

                 

                Andrea

                • 5. Re: Access WildFly Infinispan Cache Container from Web Application
                  gustavonalle

                  Hi Andrea, the query capability currently targets the values of the cache, if you want to search on keys, you'd need to add the key as a field in the value and then involve it in the queries.

                   

                  HTH,

                  Gustavo

                  • 6. Re: Access WildFly Infinispan Cache Container from Web Application
                    pferraro

                    Actually - within the context of WildFly, there's nothing wrong with using the version of Infinispan used by WildFly within your web application.  However, to Ryan's point - the latest release of Infinispan isn't runtime/source compatible with the version used by WildFly (e.g. WF11 uses 8.2.x) - so if you want to use the latest, follow his instructions.

                     

                    That said, to answer your original question: WildFly exposes Infinispan cache container, caches and their configurations as EE components.  To use an Infinispan cache container defined in the Infinispan subsystem from your web application:

                     

                    1. Create a resource-ref or resource-env-ref in your deployment descriptor:

                    e.g. web.xml

                    <resource-env-ref>
                        <resource-env-ref-name>infinispan/mycontainer</resource-env-ref-name>
                        <resource-env-ref-type>org.infinispan.manager.CacheContainer</resource-env-ref-type>
                        <lookup-name>java:jboss/infinispan/container/mycontainer</lookup-name>
                    </resource-env-ref>
                    

                     

                    2. Inject the cache container into your application code via an EE @Resource annotation:

                     

                    @Resource(name = "infinispan/mycontainer")
                    CacheContainer container;
                    

                     

                    or you can do a vanilla JNDI lookup using your deployment-specific namespace:

                     

                    Context context = new InitialContext();
                    CacheContainer container = (CacheContainer) context.lookup("java:comp/env/infinispan/mycontainer");
                    

                     

                    This is fine if you plan to create caches/configurations programmatically.  You should note, however, that any cache configurations defined by the Infinispan subsystem within that cache container are not guaranteed to be started.  In WildFly, Infinispan services are always started on-demand.  In order to reference a specific cache or cache configuration, you should define resource references for those resources as well.  This will ensure that the requisite resources are started on deploy, and stopped on undeploy.

                    e.g.

                    <resource-env-ref>
                        <resource-env-ref-name>infinispan/default</resource-env-ref-name>
                        <resource-env-ref-type>org.infinispan.configuration.cache.Configuration</resource-env-ref-type>
                        <lookup-name>java:jboss/infinispan/configuration/mycontainer/default</lookup-name><!-- References the default-cache of mycontainer -->
                    </resource-env-ref>
                    

                     

                    This way, specific caches will be available to calls to CacheContainer.getCache(...).  Using the approach, however, you must manage the cache lifecycle yourself.

                    e.g.

                    @Resource(name="infinispan/mycontainer")
                    CacheContainer container;
                    Cache cache;
                    
                    @PostConstruct
                    public void init() {
                         this.cache = this.container.getCache();
                    }
                    
                    @PreDestroy
                    public void destroy() {
                        this.cache.stop();
                    }
                    

                     

                    Ultimately, it is far easier to just inject the Cache directly.  This way the lifecycle of the cache and its container are bound to your application.

                    e.g.

                    <resource-env-ref>
                        <resource-env-ref-name>infinispan/mycache</resource-env-ref-name>
                        <resource-env-ref-type>org.infinispan.Cache</resource-env-ref-type>
                        <lookup-name>java:jboss/infinispan/cache/mycontainer/mycache</lookup-name>
                    </resource-env-ref>
                    

                     

                    @Resource(name="infinispan/mycache")
                    Cache cache;