Running MBeans with JBoss AS 7

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

    }