So you think you have got rid of MBeans in the release 7 of JBoss application server ? of course not! with the release 7.1.1 you can still use MBeans pretty much the same (even easier!) then you used to to with older releases.

 

 

You might wonder what's the use of MBeans in a platform like JBoss AS 7 which has plenty of management interfaces ? well from one point of view JMX 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:

jboss as 7 mbeans mbeans tutorial

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();
        }  

    }

 


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 the quintessential 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);
   }
}

And it's interface:
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();
}

Now 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>

As you can see, the MBeans recalls the start() method once it's activated:

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

jboss as 7 mbeans mbeans tutorial

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss AS 7 introduction

This is an introduction tutorial to the newest JBoss AS 7 which a

Develop Java EE applications on WildFly

In this tutorial we will learn how to develop a basic Java EE 8 a

Adding users with JBoss AS 7

If you are planning to add new users to your management interface

Using JBoss AS 7 management API programmatically

In this tutorial we will show how to use detyped management API t

JNDI view in JBoss AS 7

JNDI can be checked on the naming subsystem of the application se

How to install a module on WildFly / JBoss AS 7

Installing a module on WildFly / JBoss AS 7 requires creating a p