29.2.3. Deployment Via a -jboss-beans.xml File

Much like it can deploy MBean services described with a -service.xml, JBoss Enterprise Application Platform 5 can also deploy services that consist of Plain Old Java Objects (POJOs) if the POJOs are described using the JBoss Microcontainer schema in a -jboss-beans.xml file. You create such a file and deploy it, either directly in the deploy dir, or packaged in an ear or sar. Following is an example:
<?xml version="1.0" encoding="UTF-8"?>

<deployment xmlns="urn:jboss:bean-deployer:2.0">

   <!-- First we create a Configuration object for the cache -->
   <bean name="ExampleCacheConfig"
         class="org.jboss.cache.config.Configuration">

      <!-- Externally injected services -->  
      <property name="runtimeConfig">
         <bean name="ExampleCacheRuntimeConfig" class="org.jboss.cache.config.RuntimeConfig">
            <property name="transactionManager">
               <inject bean="jboss:service=TransactionManager" 
                       property="TransactionManager"/>
            </property>
            <property name="muxChannelFactory"><inject bean="JChannelFactory"/></property>
         </bean>
      </property>
      
      <property name="multiplexerStack">udp</property>
      <property name="clusterName">Example-EntityCache</property>        
      <property name="isolationLevel">REPEATABLE_READ</property>
      <property name="cacheMode">REPL_SYNC</property>
      <property name="initialStateRetrievalTimeout">15000</property>
      <property name="syncReplTimeout">20000</property>
      <property name="lockAcquisitionTimeout">15000</property>
      <property name="exposeManagementStatistics">true</property>

   </bean>
 
   <!-- Factory to build the Cache. -->
   <bean name="DefaultCacheFactory" class="org.jboss.cache.DefaultCacheFactory">      
      <constructor factoryClass="org.jboss.cache.DefaultCacheFactory" />
   </bean>
  
   <!-- The cache itself -->
   <bean name="ExampleCache" class="org.jboss.cache.Cache">
      <constructor factoryMethod="createCache">
          <factory bean="DefaultCacheFactory"/>
          <parameter class="org.jboss.cache.config.Configuration"><inject bean="ExampleCacheConfig"/></parameter>
          <parameter class="boolean">false</false>
      </constructor>
   </bean>
   
   <bean name="ExampleService" class="org.foo.ExampleService">
      <property name="cache"><inject bean="ExampleCache"/></property>
   </bean>

</deployment>
The bulk of the above is the creation of a JBoss Cache Configuration object; this is the same as what we saw in the configuration of the CacheManager service (see Section 29.1.1, “Editing the CacheManager Configuration”). In this case we are not using the CacheManager service as a cache factory, so instead we create our own factory bean and then use it to create the cache (the "ExampleCache" bean). The "ExampleCache" is then injected into a (fictitious) service that needs it.
An interesting thing to note in the above example is the use of the RuntimeConfig object. External resources like a TransactionManager and a JGroups ChannelFactory that are visible to the microcontainer are dependency injected into the RuntimeConfig. The assumption here is that in some other deployment descriptor in the Enterprise Application Platform, the referenced beans have already been described.
Using the configuration above, the "ExampleCache" cache will not be visible in JMX. Here's an alternate approach that results in the cache being bound into JMX:
<?xml version="1.0" encoding="UTF-8"?>

<deployment xmlns="urn:jboss:bean-deployer:2.0">

   <!-- First we create a Configuration object for the cache -->
   <bean name="ExampleCacheConfig"
         class="org.jboss.cache.config.Configuration">

      .... same as above

   </bean>
 
   <bean name="ExampleCacheJmxWrapper" class="org.jboss.cache.jmx.CacheJmxWrapper">
         
      <annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX
      (name="foo:service=ExampleCacheJmxWrapper",
                  exposedInterface=org.jboss.cache.jmx.CacheJmxWrapperMBean.class, 
                  registerDirectly=true)
      </annotation>
      
      <property name="configuration"><inject bean="ExampleCacheConfig"/></property>
      
   </bean>
   
   <bean name="ExampleService" class="org.foo.ExampleService">
      <property name="cache"><inject bean="ExampleCacheJmxWrapper" property="cache"/></property>
   </bean>

</deployment>
Here the "ExampleCacheJmxWrapper" bean handles the task of creating the cache from the configuration. CacheJmxWrapper is a JBoss Cache class that provides an MBean interface for a cache. Adding an <annotation> element binds the JBoss Microcontainer @JMX annotation to the bean; that in turn results in JBoss Enterprise Application Platform registering the bean in JXM as part of the deployment process.
The actual underlying org.jboss.cache.Cache instance is available from the CacheJmxWrapper via its cache property; the example shows how this can be used to inject the cache into the "ExampleService".