Running MBeans with JBoss AS 7

User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive
 

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

    }

 

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 with JBoss AS 7

In this tutorial we will learn how to create and deploy a Java EE

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 JBoss AS 7

Installing a module on JBoss AS 7 requires creating a path under

JBoss CheatSheet for Admins