1 Reply Latest reply on Mar 6, 2019 4:22 PM by pferraro

    Cannot inject infinispan cache container in a websocket

    nicolas.2324

      Hi all,

       

      I've been trying to add infinispan cache in a websocket project as I've done in some previous restful projects but it doesn't work at all. When injecting the resource, it allways gets NullPointerException when using the EmbeddedCacheContainer to retrieve a cache. I'm working out my project with maven. I created to projects that do the same but one being a websocket project and the other one being a restful one and works only for restful project.

       

      Here is my beans.xml (same in both projects):

       

      <?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"

             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"

             bean-discovery-mode="all">

      </beans>

       

      My pom's dependencies section (same in both projects):

              <dependency>

                  <groupId>org.infinispan</groupId>

                  <artifactId>infinispan-embedded</artifactId>

                  <version>8.1.2.Final</version>

                  <scope>provided</scope>

              </dependency>

       

      My pom's war plugin section (same in both projects):

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-war-plugin</artifactId>

                      <version>2.3</version>

                      <configuration>

                          <failOnMissingWebXml>false</failOnMissingWebXml>

                          <archive>

                              <manifestEntries>

                                  <Dependencies>

                                      org.infinispan

                                  </Dependencies>

                              </manifestEntries>

                          </archive>

                      </configuration>

                  </plugin>

       

      My Restful resource:

       

      package test;

       

       

      import javax.annotation.PostConstruct;

      import javax.annotation.Resource;

       

       

      import javax.ws.rs.core.Context;

      import javax.ws.rs.core.UriInfo;

      import javax.ws.rs.Produces;

      import javax.ws.rs.GET;

      import javax.ws.rs.Path;

      import javax.ws.rs.core.MediaType;

      import org.infinispan.Cache;

      import org.infinispan.manager.EmbeddedCacheManager;

       

       

      @Path("generic")

      public class GenericResource {

       

       

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

          EmbeddedCacheManager cacheManager;

          Cache<String,String> cache;

         

          @Context

          private UriInfo context;

       

       

          @PostConstruct

          public void postConstruct(){

              cache = cacheManager.getCache();

          }

       

       

          @GET

          @Produces(MediaType.APPLICATION_XML)

          public String getXml() {

              System.out.println("0 = "+this.cache.get("x"));

              this.cache.put("x", "y");

              System.out.println("1 = "+this.cache.get("x"));

              return "<xml></xml>";

          }

         

      }

       

      My websocket:

       

      package test;

       

      import javax.annotation.PostConstruct;

      import javax.annotation.Resource;

      import javax.websocket.OnOpen;

      import javax.websocket.Session;

      import javax.websocket.server.ServerEndpoint;

      import org.infinispan.Cache;

      import org.infinispan.manager.EmbeddedCacheManager;

       

       

      @ServerEndpoint("/websocket")

      public class Websocket {

         

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

          EmbeddedCacheManager cacheManager;

          Cache<String,String> cache;

       

       

          @PostConstruct

          public void postConstruct(){

              cache = cacheManager.getCache();

          }

         

          @OnOpen

          public void onOpen(Session session){

              System.out.println("0 = "+this.cache.get("z"));

              this.cache.put("z", "a");

              System.out.println("1 = "+this.cache.get("z"));

          }

         

      }

       

      Is there any other thing I've gotta do to get it to work?

        • 1. Re: Cannot inject infinispan cache container in a websocket
          pferraro

          This is due to [WFLY-11817] CDI @Resource(lookup=...) processing does not start corresponding binding service - JBoss Issue Tracker

          To workaround this bug, add a <resource-ref/> to your deployment descriptor and reference the resource by name.

           

          Additionally, I suggest you inject the cache directly, instead of obtaining it from an injected cache manager.  That way, the lifecycle of the cache will be managed by the container (I noticed that your GenericResource class is missing a @PreDestroy method that stops the cache that was created in @PostConstruct), and thus can inject the Cache into multiple classes/deployments without issue.

           

          e.g.

          <resource-ref>

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

              <lookup-name>java:jboss/infinispan/cache/server/default</lookup-name>

          </resource-ref>

           

          You can then inject the cache directly in your GenericResource class:

           

          @Resource(name = "foo")

          Cache<String, String> cache;