Chapter 13. Running JBoss Cache within JBoss Application Server

If JBoss Cache is run in JBoss AS then JBoss Cache can be deployed as an MBean. The steps below illustrate how to do this. We do not deploy JBoss Cache as a Service Archive (SAR), but as a JAR (jboss-cache.jar in the lib directory) and an XML file defining the MBean. Of course, JBoss Cache can also be deployed as a SAR, or even as part of a WAR, EJB or EAR.

First, the jboss-cache.jar file has to be copied to the /lib directory and JBoss AS has to be restarted. Then a regular JBoss Cache configuration file in XML format has to be copied to the /deploy directory. The XML file format is the same as discussed in the Configuration chapter.

In order to be used from a client such as a servlet in the Tomcat web container inside the same JBoss container, JMX can be used:

MBeanServer server=MBeanServerLocator.locateJBoss();
TreeCacheMBean cache;
cache=(TreeCacheMBean)MBeanProxyExt.create(TreeCacheMBean.class, "jboss.cache:service=TreeCache", server);
cache.put("/a/b/c", null);

The MBeanServerLocator class is a helper to find the (only) JBoss MBean server inside the current VM. The static create() method creates a dynamic proxy to the given interface and uses JMX to dynamically dispatch methods invoked against the generated interface. The name used to look up the MBean is the same as defined in the configuration file.

13.1. Running as an MBean

If JBoss Cache is run inside of JBoss AS (as an MBean), we can bind it into JNDI using JrmpProxyFactory, just like any other MBean. Below is an example of how to do this:

 <mbean
      code="org.jboss.invocation.jrmp.server.JRMPProxyFactory"
      name="mydomain:service=proxyFactory,type=jrmp,target=factory">
      <attribute
      name="InvokerName">jboss:service=invoker,type=jrmp</attribute>
      <attribute
      name="TargetName">jboss.cache:service=TreeCache</attribute>
      <attribute name="JndiName">MyCache</attribute> <attribute
      name="InvokeTargetMethod">true</attribute> <attribute
      name="ExportedInterface">org.jboss.cache.TreeCacheMBean</attribute>
      <attribute name="ClientInterceptors"> <iterceptors>
      <interceptor>org.jboss.proxy.ClientMethodInterceptor</interceptor>
      <interceptor>org.jboss.proxy.SecurityInterceptor</interceptor>
      <interceptor>org.jboss.invocation.InvokerInterceptor</interceptor>
      </iterceptors> </attribute>
      <depends>jboss:service=invoker,type=jrmp</depends>
      <depends>jboss.cache:service=TreeCache</depends>
      </mbean> 

The InvokerName attribute needs to point to a valid JBoss invoker MBean. TargetName is the JMX name of the MBean that needs to be bound into JNDI. JndiName is the name under which the MBean will be bound, and ExportedInterface is the interface name of the MBean.