Version 2

    Abstract baseclass InterceptorServiceMBeanSupport

     

    As of JBoss v4.0.3, there is a new baseclass that helps writing

    MBean services that dynamically hooks-up a JMX Interceptor to one or more

    (X)MBean that have been configured as Interceptable.

     

    (To configure an (X)MBean as Interceptable, see the wiki entry for DynamicInterceptor.)

     

    When extending InterceptorServiceMBeanSupport simply override invoke()

    to add behaviour in the installed interceptor. As you can see, the default

    implementation simply forwards every call using invokeNext(invocation):

       /**
        * Override
        */
       protected Object invoke(Invocation invocation) throws Throwable
       {
          return invokeNext(invocation);
       }
    

    To add/remove the jmx interceptor to the target mbeans, you may call

    the attach()/detach() method in the base class. By default, those

    methods are called inside startService()/stopService():

       /**
        * Override
        */
       protected void startService() throws Exception
       {
          attach();
       }
       
       /**
        * Override
        */
       protected void stopService()
       {
          detach();
       }
    

    The final step is to configure the target Interceptables attribute.

    This appears in the InterceptorServiceMBean interface that you

    most probably want to extend from your own MBean interface. Normally you'll

    combine this with a dependency, so that the interceptor is only applied

    if the target mbeans are started. Correspondigly, the interceptor will

    be removed if the dependent mbeans are stopped, or the interceptor service

    is undeployed.

     

    If you are writing an interceptor service that attaches itself specifically

    to SubDeployers use SubDeployerInterceptorSupport, instead.

     

    Example

    This example is taken from the JBoss testsuite.

     

    We have a simple AdderPOJO class that adds two integers and is deployed

    as an (X)MBean, with the DynamicInterceptor enabled:

    package org.jboss.test.jmx.interceptors;
    
    public class AdderPOJO
    {
       public AdderPOJO()
       {
          // empty
       }
    
       public int add(int a, int b)
       {
          return a + b;
       }
    }
    

    The mbean descriptor:

    <server>
       <mbean code="org.jboss.test.jmx.interceptors.AdderPOJO"
          name="jboss.test:service=interceptable"
          xmbean-dd="META-INF/interceptable-xmbean.xml" ></mbean>
    </server>
    

    The xmbean descriptor:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mbean PUBLIC
       "-//JBoss//DTD JBOSS XMBEAN 1.2//EN"
       "http://www.jboss.org/j2ee/dtd/jboss_xmbean_1_2.dtd">
    
    <mbean>
       <description>Test Interceptable</description>
       <descriptors>
         <interceptors>
           <interceptor code="org.jboss.mx.interceptor.DynamicInterceptor"></interceptor>
         </interceptors>   
       </descriptors>   
       <class>org.jboss.test.jmx.interceptors.AdderPOJO</class>
       
       &interceptable;
       
       <!-- Operations -->
       <operation>
          <description>Simple Addition</description>
          <name>add</name>
          <parameter>
             <description>First integer</description>
             <name>a</name>
             <type>int</type>
          </parameter>
          <parameter>
             <description>Second integer</description>
             <name>b</name>
             <type>int</type>
          </parameter>
          <return-type>int</return-type>      
       </operation>
    
    </mbean>
    

    Then we create the interceptor service that extends InterceptorServiceMBean

    / InterceptorServiceMBeanSupport. When attached to the AdderPOJO, the

    service will process calls to the "add" method and add +1 to the

    returned result:

    package org.jboss.test.jmx.interceptors;
    
    import org.jboss.system.InterceptorServiceMBean;
    
    public interface AdderInterceptorServiceMBean
       extends InterceptorServiceMBean
    {
       // empty
    }
    
    package org.jboss.test.jmx.interceptors;
    
    import org.jboss.mx.server.Invocation;
    import org.jboss.system.InterceptorServiceMBeanSupport;
    
    /**
     * AdderInterceptorService will attach dynamically an interceptor
     * to the AdderPOJO (xmbean) and add +1 to the add() method result
     * 
     * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
     * @version $Revision: 1.2.2.2 $
     */
    public class AdderInterceptorService extends InterceptorServiceMBeanSupport
       implements AdderInterceptorServiceMBean
    {
       /**
        * Override
        */
       protected Object invoke(Invocation invocation) throws Throwable
       {
          String type = invocation.getType();
          
          if (type.equals(Invocation.OP_INVOKE))
          {
             String name = invocation.getName();
             
             if (name.equals("add"))
             {
                Integer result = (Integer)super.invokeNext(invocation);
                
                return new Integer(result.intValue() + 1);
             }
          }
          return super.invokeNext(invocation);
       }
    }
    

    When deploying the interceptor service, we specify the interceptable

    mbean(s) to attach to:

    <server>
       <mbean code="org.jboss.test.jmx.interceptors.AdderInterceptorService"
          name="jboss.test:service=adderinterceptor">
          
          <depends-list optional-attribute-name="Interceptables">
             <depends-list-element>jboss.test:service=interceptable</depends-list-element>
          </depends-list>
       </mbean>
    </server>
    

    That's all folks!

     

    Related:

     

    DynamicInterceptor

     

    SubDeployerInterceptorSupport

     

    Referenced by: