I am trying to use Jboss AS
managed Infinispan
in my application, so that I can use Jboss Admin console
to manage it.
I have tried the following steps based on Infinispan documentation,
1) Created a class named Config
import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import org.infinispan.manager.EmbeddedCacheManager;
public class Config {
@Produces
@ApplicationScoped
@Resource(lookup = "java:jboss/infinispan/test")
private EmbeddedCacheManager defaultCacheManager;
public void printObject() {
System.out.println("defaultCacheManager:" + defaultCacheManager);
}
}
2) created a servlet just for making Config object and calling printObject() method
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3200037917839533696L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
protected void doIt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Config config = new Config();
config.printObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3) Added the application dependencies via standalone.xml global configuration
<subsystem xmlns="urn:jboss:domain:ee:1.0">
<global-modules>
<module name="org.infinispan" slot="main"/>
<module name="javax.enterprise.api" slot="main"/>
<module name="javax.faces.api" slot="main"/>
<module name="javax.inject.api" slot="main"/>
<module name="javax.annotation.api" slot="main"/>
</global-modules>
</subsystem>
4)Added new cache container in subsystem named test
<subsystem xmlns="urn:jboss:domain:infinispan:1.2" default-cache-container="test">
<cache-container name="test" default-cache="entity" start="EAGER">
<local-cache name="entity"/>
</cache-container>
</subsystem>
5)Removed all Infinispan related jars from my application
But, When I try to call the printObject() method from servel it is printing null
13:37:24,206 INFO [stdout] (http--127.0.0.1-9090-1) defaultCacheManager:null
Why it is happening , please correct me if any thing is missed from my side.
Try this one
@Resource(lookup = "java:jboss/infinispan/container/test")
private EmbeddedCacheManager defaultCacheManager;
Or
Here is a sample code:
Config.java:
Its defined as managed bean that is initialized at the time of server start-up and its default constructor is called where its reference is stored in a static field to access it from anywhere in the application.
Config class must be as singleton. Do not call new Config()
any where in code. It is created by server itself.
Call Config.getInstance()
whenever it is needed in the application.
It also handle the condition where configuration is missing (on dev machine).
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
@ManagedBean(name = "Config", eager = true)
@ApplicationScoped
public class Config {
@Resource(name = "java:jboss/infinispan/container/test")
private CacheContainer container;
private Cache<String, Object> entityCache = null;
private static Config configInstance = null;
public Config() {
configInstance = this;
}
@PostConstruct
public void start() {
entityCache = container.getCache("entity");
}
public static Config getInstance() {
if (configInstance == null) {
createInstance();
}
return configInstance;
}
public Cache<String, Object> getEntityCache() {
return entityCache;
}
private static void createInstance() {
configInstance = new Config();
if (configInstance.container == null) {
ConfigurationBuilder confBuilder = new ConfigurationBuilder();
confBuilder.clustering().cacheMode(CacheMode.LOCAL);
EmbeddedCacheManager cacheManager = new DefaultCacheManager(confBuilder.build());
cacheManager.start();
configInstance.container = cacheManager;
}
configInstance.start();
}
}