How to deploy MBeans in WildFly

This tutorial shows how to deploy MBeans , bundled in SAR files, on the top of WildFly / JBoss application server.

What is a SAR archive?

You can use SAR archive to deploy a service component in the application server without dependency on other components. You can create a separate component as a SAR file and deploy it in the server. When application server starts, the component will be deployed and started running independently

A SAR file relies on JMX API which still proves to be an useful Swiss knife if you want to access programmatically some AS features of perform some actions.
For example, what about checking the status of an application ? then all you need is opening the JConsole tool and check for the ObjectName of the application:

mbeans wildfly jboss

Once you have got it, it’s pretty easy to check the application’s attributes:

   public void status() {
       MBeanServer server = ManagementFactory.getPlatformMBeanServer();
       ObjectName name=null;
       try {
           name = new ObjectName("jboss.as:deployment=jboss-as-ejb-remote-app.jar");

           String status = (String) (server.getAttribute(name, "status"));
           Boolean enabled = (Boolean) (server.getAttribute(name, "enabled"));


           System.out.println(status);
           System.out.println(enabled);

       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }  

   }
}

You can perform as well actions on your Mbeans, such as undeploying an application:

  public void undeploy() {
       MBeanServer server = ManagementFactory.getPlatformMBeanServer();
         ObjectName name=null;
        try {
            name = new ObjectName("jboss.as:deployment=javaee6example.war");
            server.invoke(name, "undeploy", null,null);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
         
   }

You might be interested to know that JMX can be pretty useful if you want need some quick clustering info, and you don’t want to learn some low level APIs like JGroups. Here’s for example how to read some Cluster information like the number of members and the number of running caches:

    public void clusterInfo() {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ObjectName name=null;
        try {
            name = new ObjectName("jboss.infinispan:type=CacheManager,name=\"web\",component=CacheManager");


            String clusterMembers = (String) (server.getAttribute(name, "clusterMembers"));
            String runningCacheCount = (String) (server.getAttribute(name, "runningCacheCount"));


            System.out.println(clusterMembers);
            System.out.println(runningCacheCount);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

    }

To learn how to access WildFly MBeans we recommend checking this article: Managing MBeans with WildFly

Deploying MBeans on WildFly

So far we have seen how to read some attributes and perform some actions on MBeans built-in with the application server. What about adding your own MBeans to the application server ?
This can be done almost the same you used to do for your earlier JBoss AS platform, that is deploying a .SAR archive, with a jboss-service.xml contained in the META-INF folder of your Service Archive, describing the service.

The only difference is that you don’t need anymore to extend ServiceMBeanSupport and ServiceMBean classes in order to create your MBeans. Just deploy a Plain Old Java Object with a start() and stop() mode and that’s it!

Here’s an HelloWorld MBean:

package com.sample;

public class HelloWorldService  implements HelloWorldServiceMBean
{

   private String message = "Sorry no message today";


   public String getMessage()
   {
      return message;
   }
   public void setMessage(String message)
   {
      this.message = message;
   }


   public void printMessage()
   {
      System.out.println(message);
   }

   // The lifecycle
   public void start() throws Exception
   {
       System.out.println(">>>>Starting with message=" + message);
   }
   public void stop() throws Exception
   {
       System.out.println(">>>>Stopping with message=" + message);
   }
}

Then, include an interface for your Bean:

package com.sample;

public interface HelloWorldServiceMBean  
{
   // Configure getters and setters for the message attribute
   String getMessage();
   void setMessage(String message);
   
   // The print message operation
   void printMessage();
}

Next, add the jboss-service.xml file which needs to be placed in the META-INF folder of your SAR:

<?xml version="1.0" encoding="UTF-8"?>
<server xmlns="urn:jboss:service:7.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd">

    <mbean code="com.sample.HelloWorldService" name="service.server.samplembean:service=HelloWorldService">
         <attribute name="message">Hello</attribute>
    </mbean>

</server>

Here is the directory structure of your application:

src
└── main
    ├── java
    │   └── com
    │       └── sample
    │           ├── HelloWorldService.java
    │           └── HelloWorldServiceMBean.java
    └── resources
        └── META-INF
            └── jboss-service.xml

Next, build the application and rename the extensions to be .sar. For example “demo.sar“.

Then, copy in the application in the deployments folder of the application server.

As you can see, upon deployment, the MBeans recalls the start() method:

jboss wildfly sar files mbeans

Finally, move to the JConsole and check that the service has been enlisted and it correctly displays its attributes and actions.

mbeans jboss tutorial