JBossAS7 provides useful ways to create our own custom MBeans using SAR archives as mentioned in our previous articles. Now in this current demo we will see how we can create and and register our own custom “MXBean” in JBossAS7.

An MXBean is a kind of MBean. An MXBean object can be registered directly in the MBean Server, or it can be used as an argument to StandardMBean and the resultant MBean registered in the MBean Server. These beans have an annotation @MXBean(true) or just @MXBean. The MXBean concept provides a simple way to code an MBean that only references a predefined set of types, the ones defined by javax.management.openmbean. In this way, you can be sure that your MBean will be usable by any client, including remote clients, without any requirement that the client have access to model-specific classes representing the types of your MBeans.

More infrmation on @MXBean annotation can be found in the following link:

NOTE: the Codes related to this demo are available in the Github repository:

Step-1). Here we are using JBoss AS7.1.1 Final (“jboss-as-7.1.1.Final”) which can be downloaded from the following link: http://www.jboss.org/jbossas/downloads

Step-2). Create a directory with the name “src” somewhere in your file system like “/home/userone/MXBeanDemo/”. Inside the “src” directory we are going to place our all source code and xml files for development.

Step-3). Now we will write a simple interface “HelloWorldServiceMBean.java” inside “/home/userone/MXBeanDemo/src” as following

package test.startup;
import javax.management.*;

public interface HelloWorldServiceMBean
     public String getMessage();
     public void setMessage(String message);
     public void printMessage();

Step-3). Now we will write a Simple java program which will using the @MXBean and will provide the implementation of above interface, so write a Class “HelloWorldService.java” inside “/home/userone/MXBeanDemo/src” as following:

package test.startup;

public class HelloWorldService implements HelloWorldServiceMBean
   private String message = "Sorry no message today";

   public HelloWorldService()
       System.out.println("ntHelloWorldService() Initialized");

   public String getMessage()
      System.out.println("ntgetMessage() : "+message);
      return message;

   public void setMessage(String message)
      System.out.println("ntsetMessage() : "+message);
      this.message = message;

   public void printMessage()
      System.out.println("ntprintMessage() : "+message);


Step-4). Write a simple “index.jsp” file inside “/home/userone/MXBeanDemo/src” as following (This simple JSP will actually create the above mentioned) :

   <%@ page import="javax.management.*,javax.management.remote.*,test.startup.*" %>

     MBeanServer mbeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer(); 
     System.out.println("Got the MBeanServer.... "+mbeanServer);

     ObjectName objectName=new ObjectName("test.startup:service=HelloWorld");

     HelloWorldServiceMBean bean=new HelloWorldService();
     mbeanServer.registerMBean(bean, objectName); 
     System.out.println("MBean Registered with ObjectName:  "+objectName);

<h1> Check the JBossAS7 Console to see if Your MXBean  is registered properly or not.   You can also use the "$JBOSS_HOME/bin/jconsole.sh" script as well to start the JConsole and to see if the MXBean is accessible through it or not </h1>

Step-5). Now we will write a simple “web.xml” file inside “/home/userone/MXBeanDemo/src” as following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


Step-6). Now in order to build and deploy the above application we will write a very simple ant build script “build.xml” inside “/home/userone/jboss-as-7.1.1.Final/standalone/TESTING/MXBeanDemo” as following:

<project name="MXBeanDemo" default="deploy">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.1.Final" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="output.dir" value="build" />
<property name="src.dir" value="src" />
<property name="war.name" value="MXBeanDemo.war" />

        <path id="jboss.classpath">
             <fileset dir="${jboss.module.dir}">
                 <include name="**/*.jar"/>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}/WEB-INF/classes"/>

        <target name="build" depends="init">
           <javac srcdir="${src.dir}" destdir="${tmp.dir}/WEB-INF/classes"  includes="*.java" classpathref="jboss.classpath" />
           <copy todir="${tmp.dir}/WEB-INF/classes">
                <fileset dir="${src.dir}" includes="**/*.java"/>
           <copy file="${src.dir}/index.jsp" tofile="${tmp.dir}/index.jsp"/>
           <copy file="${src.dir}/web.xml" tofile="${tmp.dir}/WEB-INF/web.xml"/>      
           <jar jarfile="${tmp.dir}/${war.name}" basedir="${tmp.dir}" compress="true"/> 

           <copy file="${tmp.dir}/${war.name}" tofile="${output.dir}/${war.name}"/>
           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}"/>

        <target name="deploy" depends="build">
            <echo message="*******************  Deploying the WAR file ${war.name} *********************" />
            <echo message="********** ${output.dir}/${war.name} to ${jboss.home}/standalone/deployments **********" />
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${war.name}"/>
            <echo message="*******************  Deployed Successfully   *********************" />

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script to point to your own JBoss AS7 directory.

Step-7). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-7). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/userone/jboss-as-7.1.0.Final/standalone/TESTING/MXBeanDemo” and then run the ant to build & deploy the webapplication. by running the command “ant deploy”

[userone@localhost MXBeanDemo]$ ant 

Buildfile: build.xml

   [delete] Deleting directory /home/userone/MXBeanDemo/build
    [mkdir] Created dir: /home/userone/MXBeanDemo/build
    [mkdir] Created dir: /home/userone/MXBeanDemo/tmp
    [mkdir] Created dir: /home/userone/MXBeanDemo/tmp/WEB-INF/classes

    [javac] Compiling 2 source files to /home/userone/MXBeanDemo/tmp/WEB-INF/classes
     [copy] Copying 2 files to /home/userone/MXBeanDemo/tmp/WEB-INF/classes
     [copy] Copying 1 file to /home/userone/MXBeanDemo/tmp
     [copy] Copying 1 file to /home/userone/MXBeanDemo/tmp/WEB-INF
      [jar] Building jar: /home/userone/MXBeanDemo/tmp/MXBeanDemo.war
     [copy] Copying 1 file to /home/userone/MXBeanDemo/build

     [echo] *******************  Deploying the WAR file MXBeanDemo.war *********************
     [echo] ********** build/MXBeanDemo.war to /home/userone/jboss-as-7.1.1.Final/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.1.Final/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

Total time: 6 seconds

Step-8). Now you will able to access your application. “http://localhost:8080/MXBeanDemo/index.jsp” then you will see the following kind of output on your JBossAS7 Console as following:

11:57:32,024 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015877: Stopped deployment MXBeanDemo.war in 34ms
11:57:32,026 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "MXBeanDemo.war"
11:57:32,078 INFO  [org.jboss.web] (MSC service thread 1-6) JBAS018210: Registering web context: /MXBeanDemo
11:57:32,190 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "MXBeanDemo.war" with deployment "MXBeanDemo.war"
11:57:54,160 INFO  [stdout] (http-- Got the MBeanServer.... org.jboss.as.jmx.PluggableMBeanServerImpl@3c21251c
11:57:54,161 INFO  [stdout] (http-- 
11:57:54,162 INFO  [stdout] (http-- 	HelloWorldService() Initialized
11:57:54,165 INFO  [stdout] (http-- MBean Registered with ObjectName:  test.startup:service=HelloWorld

Verifying using JConsole

Step-9). Start the jconsole using the JBoss provided script “jboss-as-7.1.1.Final/bin/jconsole.sh” (Make sure that you set the JAVA_HOME properly in the script)

Connecting to JBossAS7 using JConsole

Connecting to JBossAS7 using JConsole

See your MXBean is available on JConsole

See your MXBean is available on JConsole

Middleware Magic Team 🙂

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.