Infinispan lifecyle question
galfstad Mar 12, 2019 2:54 PMI am upgrading an application from JBoss 5.5 to Wildfly 16. This application was using org.jboss.cache.jmx.CacheJmxWrapperMBean. It kept the cache in a static org.jboss.cache.Cache and was accessed by a javax.servlet.Filter.
Since org.jboss.cache is not a part of Wildfly I am trying to change to use Infinispan. I have modified the Filter code to set in the cache as resource. However each time the Filter is hit, the Cache is a new instance, and what was put in cache previously is lost.
Would really appreciate how to tackle this!
The old JBoss code:
public class CacheManager { private static Cache<String, Object> cache = null; static { MBeanServer server = MBeanServerLocator.locateJBoss(); CacheJmxWrapperMBean<String, Object> cacheWrapper = (CacheJmxWrapperMBean<String, Object>) MBeanProxyExt.create(CacheJmxWrapperMBean.class, "jboss.cache:service=MyService", server); cache = cacheWrapper.getCache(); } public static Object get(String node, String key) throws Exception { return cache.get(node, key); } public static void put(String node, String key, Object object) throws Exception { cache.put(node, key, object); } }
Wildfly Standalone-full.xml
<cache-container name="LnWpsNetMarkCache" default-cache="myCache" module="org.wildfly.clustering.server"> <local-cache name="myCache"> <transaction mode="NONE"/> <object-memory size="10000"/> <expiration max-idle="100000"/> </local-cache> </cache-container>
jboss-deployment-structure.xml
<deployment> <dependencies> <module name="com.oracle.ojdbc14" export="true"/> <module name="org.dom4j" export="true"/> <module name="org.picketbox" export="true"/> <module name="org.infinispan" export="true"/> <module name="org.infinispan.commons" export="true"/> </dependencies> </deployment>
web.xml
<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/LnWpsNetMarkCache/myCache</lookup-name> </resource-env-ref>
MyFilter, will sometimes put in Cache, and then other pages will get from cache.
public class MyFilter implements Filter { @Resource(name="infinispan/myCache") Cache<String, MyObject> myCache; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { HttpServletRequest hreq = (HttpServletRequest)request; HttpSession session = hreq.getSession(true); String servletPath = hreq.getServletPath(); String key = getKey(request); if (servletPath.startsWith("myLogin")) { Object loginUser = getSessionInfo(request); this.myCache.put(key, loginUser); hresp.sendRedirect(hreq.getContextPath() + LOGIN_CONFIRM_PAGE); } Object obj = this.myCache.get(key); if (ojb == null) { session.invalidate(); } } } }