Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Mule as MBean

An MBean is a named managed object representing a resource in an JMX environment. You can easily deploy an MBean with Mule by taking the following steps:

  1. Create an MBean

  2. Create service descriptor

  3. Deploy MBean (as .sar) to application server

  4. Copy dependencies to the service’s classpath

This page describes these steps using the JBoss application server.

Creating a Simple MBean

To create an MBean, you need an interface and an implementation:


         
      
1
package foo.mbean;public interface FooServiceMBean { public String getBar();      public void start();      public void stop();}

         
      
1
package foo.mbean;import org.jboss.system.ServiceMBeanSupport;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.api.MuleContext;import org.mule.api.context.notification.ServerNotification;public class FooService extends ServiceMBeanSupport implements FooServiceMBean {      public String getBar() {     return "bar"; }   public void start() {     this.getLog().info("MBean being started");                try {         MuleContext context = new DefaultMuleContextFactory().createMuleContext(new SpringXmlConfigurationBuilder("foo-config.xml"));           context.registerListener(this);                    context.start();       }       catch(Exception e) {          e.printStackTrace();        }       this.getLog().info("MBean started");  }   public void stop() {              this.getLog().info("MBean being stopped");                try {         if (context != null) {              context.stop();             context.dispose();                          }           this.getLog().info("Done stopping Mule MBean Service!");      }       catch (Exception ex) {            this.getLog().error("Stopping Mule caused and exception!", ex);       }           }}

The extension of ServiceMBeanSupport is simply to provide you more control over the API provided by JBoss.

Creating JBoss Service Descriptor

You must create a service descriptor and add it to to META-INF/. Following is a simple example:


         
      
1
<?xml version="1.0" encoding="UTF-8"?><server>  <mbean code="foo.FooService" name="foo:service=Foo">  </mbean></server>

Deploying MBean to JBoss

Based on the examples above, your distribution looks like this:


         
      
1
../foo./foo/FooService./foo/FooServiceMBean./META-INF./META-INF/jboss-service.xml

Package the distribution either as a JAR, which you can then rename to a *.sar that you will eventually extract, or as a directory called <dirName>.sar.

Copy the Dependencies

Follow the steps below to copy the dependencies and complete the deployment:

  1. Copy your <dirName>.sar/ directory to JBOSS_HOME/server/default/deploy/.

  2. Copy all dependencies of Mule, such as MULE_HOME/lib//.jar to the <dirName>.sar/ directory

  3. Start JBoss. You will see the MBean appears in its MBean console.