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