-
1. Re: JBossCache v1.2.3 in Tomcat (v5.5.x) [Tomcat Cluster env
brian.stansberry Mar 16, 2006 8:40 PM (in response to sisepago)First, for general JBossCache docs, be sure you are familiar with http://docs.jboss.com/jbcache/1.2.4sp2/TreeCache/en/html/
OK, what I'm going to describe now is a cache that is deployed as part of a webapp. All the servers in the cluster that deploy the webapp will also deploy the cache, and those caches will see each other and replicate amongst each other.
BTW, what I'm about to describe could work in any servlet container. There is nothing Tomcat specific about it :)
First, you need to create a cache config file. The docs have tons of details on cache configuration, so I'm not going to get into that Let's say your config file is called "my-cache-service.xml".
This file needs to be on the webapp classpath. Typically you would place it in WEB-INF/classes.
The lifecycle of the cache is tied to the webapp lifecycle, so starting and stopping the cache a ServletContextListener makes sense. In contextInitialized() you would start the cache:TreeCache tree = new TreeCache(); PropertyConfigurator config = new PropertyConfigurator(); config.configure(tree, "my-cache--service.xml"); tree.createService(); tree.startService(); // Store a ref to the cache so servlets can access it event.getServletContext().setAttribute("cache", tree);
In contextDestroyed() you would clean up:TreeCache tree = (TreeCache) event.getServletContext().getAttribute("cache"); if (tree != null) { tree.stopService(); tree.destroyService(); event.getServletContext().removeAttribute("cache"); }
Voila, you have a cache that is accessible to your webapp code via a lookup in the ServletContext.