Cannot inject infinispan cache container in a websocket
nicolas.2324 Mar 6, 2019 4:36 AMHi 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?