JBoss.org Community Documentation
If JBoss Cache is run in JBoss AS then the cache can be deployed as an
MBean simply by copying a standard cache configuration file to the server's
deploy
directory. The standard format of JBoss Cache's
standard XML configuration file (as shown in the
Configuration Reference
) is the same
as a JBoss AS MBean deployment descriptor, so the AS's SAR Deployer has
no trouble handling it. Also, you don't have to place the configuration
file directly in
deploy
; you can package it along
with other services or JEE components in a SAR or EAR.
In AS 5, if you're using a server config based on the standard
all
config, then that's all you need to do; all required
jars will be on the classpath. Otherwise, you will need to ensure
jbosscache.jar
and
jgroups-all.jar
are on the classpath. You may need to add other jars if you're using
things like
JdbmCacheLoader
. The simplest way to do
this is to copy the jars from the JBoss Cache distribution's
lib
directory to the server config's
lib
directory. You could also package the jars with the configuration file
in Service Archive (.sar) file or an EAR.
It is possible to deploy a JBoss Cache 2.0 instance in JBoss AS 4.x (at least in 4.2.0.GA; other AS releases are completely untested). However, the significant API changes between the JBoss Cache 2.x and 1.x releases mean none of the standard AS 4.x clustering services (e.g. http session replication) that rely on JBoss Cache will work with JBoss Cache 2.x. Also, be aware that usage of JBoss Cache 2.x in AS 4.x is not something the JBoss Cache developers are making any significant effort to test, so be sure to test your application well (which of course you're doing anyway.)
Note in the
example
the value of the
mbean
element's
code
attribute:
org.jboss.cache.jmx.CacheJmxWrapper
. This is the
class JBoss Cache uses to handle JMX integration; the
Cache
itself does not expose an MBean
interface. See the
JBoss Cache MBeans section
for more on the
CacheJmxWrapper
.
Once your cache is deployed, in order to use it with an in-VM client such as a servlet, a JMX proxy can be used to get a reference to the cache:
MBeanServer server = MBeanServerLocator.locateJBoss();
ObjectName on = new ObjectName("jboss.cache:service=Cache");
CacheJmxWrapperMBean cacheWrapper =
(CacheJmxWrapperMBean) MBeanServerInvocationHandler.newProxyInstance(server, on,
CacheJmxWrapperMBean.class, false);
Cache cache = cacheWrapper.getCache();
Node root = cache.getRoot(); // etc etc
The MBeanServerLocator class is a helper to find the (only) JBoss
MBean server inside the current JVM. The
javax.management.MBeanServerInvocationHandler
class'
newProxyInstance
method creates a
dynamic proxy implementing the given interface and uses JMX to
dynamically dispatch methods invoked against the generated interface
to the MBean. The name used to look up the MBean is the same as defined
in the cache's configuration file.
Once the proxy to the
CacheJmxWrapper
is obtained,
the
getCache()
will return a reference to the
Cache
itself.