8.2.1. Example usage of Region Activation/Inactivation
As an example of the usage of region activation and inactivation, let's imagine a scenario where a TreeCache instance is deployed as a shared MBean service by deploying a -service.xml in the JBoss /deploy directory. One of the users of this cache could be a web application, which when started will register its classloader with the TreeCache and activate its own region of the cache.
First, the XML configuration file for the shared cache service would be configured as follows (only relevant portions are shown):
<?xml version="1.0" encoding="UTF-8" ?>
<server>
<classpath codebase="./lib" archives="jboss-cache.jar, jgroups.jar" />
<!-- ==================================================================== -->
<!-- Defines TreeCache configuration -->
<!-- ==================================================================== -->
<mbean code="org.jboss.cache.TreeCache" name="com.xyz.cache:service=SharedCache">
.......
<!-- Configure Marshalling -->
<attribute name="getUseRegionBasedMarshalling">true</attribute>
<attribute name="InactiveOnStartup">true</attribute>
........
</mbean>
</server>
For the webapp, registering/unregistering the classloader and activating/inactivating the app's region in the cache are tasks that should be done as part of initialization and destruction of the app. So, using a ServletContextListener to manage these tasks seems logical. Following is an example listener:
package example;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.mx.util.MBeanProxyExt;
public class ActiveInactiveRegionExample implements ServletContextListener
{
private TreeCacheMBean cache;
public void contextInitialized(ServletContextEvent arg0) {
try {
findCache();
cache.registerClassLoader("/example", Thread.currentThread().getContextClassLoader());
cache.activeRegion("/example");
}
catch (Exception e) {
// ... handle exception
}
}
public void contextDestroyed(ServletContextEvent arg0) {
cache.inactivateRegion("/example");
cache.unregisterClassLoader("/example");
}
private void findCache() throws MalformedObjectNameException {
// Find the shared cache service in JMX and create a proxy to it
ObjectName cacheServiceName_ = new ObjectName("com.xyz.cache:service=SharedCache");
// Create Proxy-Object for this service
cache = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cacheServiceName_);
}
}
The listener makes use of the JBoss utility class MBeanProxyExt to find the TreeCache in JMX and create a proxy to it. (See the "Running and using TreeCache inside JBoss" section below for more on accessing a TreeCache). It then registers its classloader with the cache and activates its region. When the webapp is being destroyed, it inactivates its region and unregisters its classloader (thus ensuring that the classloader isn't leaked via a reference to it held by TreeCacheMarshaller).
Note the order of the method calls in the example class -- register a classloader before activating a region, and inactivate the region before unregistering the classloader.