+

Search Tips   |   Advanced Search

Create and registering standard, dynamic, and open custom MBeans


We can create standard, dynamic, and open custom MBeans and register them with WAS admin service.

This task assumes a basic familiarity with MBean programming. For information on MBean programming, see MBean Java API documentation. In this information center, click Reference > Mbean interfaces.

Do not define new classes as parameters for the MBeans. The classes might not be available in all processes. If define a new class, ensure that the class is available on all processes, including the dmgr, the node agents, and the appservers. If the class is not available for a process, the ClassNotFoundException exception occurs for the new class when you attempt to access it.

Perform the following tasks to create and register a standard, dynamic, or open custom MBean.

 

  1. Create the particular MBean class or classes.

  2. Write an MBean descriptor in the XML language for the MBean.

  3. Register the MBean by inserting code that uses the WAS runtime com.ibm.websphere.management.UserMBeanCollaborator collaborator class into the application code.

  4. Package the class files for the MBean interface and implementation, the descriptor XML file, and the application JAR file.

 

Results

After you successfully complete the steps, we have a standard, dynamic, or open custom MBean that is registered and activated with WAS administrative service.

 

Example

The following example shows how to create and register a standard MBean with the admin service:

 SnoopMBean.java:

/**
 * Use the SnoopMBean MBean, which has a standard mbean interface.
 */  public interface SnoopMBean {
    public String getIdentification();
    public void snoopy(String parm1);
}

SnoopMBeanImpl.java: /** * SnoopMBeanImpl - SnoopMBean implementation */ public class SnoopMBeanImpl implements SnoopMBean { public String getIdentification() { System.out.println(">>> getIdentification() called..."); return "snoopy!"; } public void snoopy(String parm1) { System.out.println(">>> snoopy(" + parm1 + ") called..."); } }

Define the following MBean descriptor for the MBean in an .xml file. The getIdentification method is set to run with the unicall option and the snoopy method is set to use the multicall option. These options are used only for z/OS platform applications. The WAS for z/OS options are not applicable to the distributed platforms, but they do not need to be removed. The options are ignored on the distributed platforms. . Some statements are split on multiple lines for printing purposes.

Avoid trouble: If running in a multiple JVM environment include the type property in the MBean descriptor.

 SnoopMBean.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MBean SYSTEM "MbeanDescriptor.dtd">
<MBean type="SnoopMBean"
 version="5.0"
 platform="dynamicproxy"
 description="Sample SnoopMBean to be initialized inside an EJB.">


 <attribute name="identification" getMethod="getIdentification"  type="java.lang.String" proxyInvokeType="unicall"/>

 <operation name="snoopy" role="operation"  type="void" targetObjectType="objectReference" 
   impact="ACTION" proxyInvokeType="multicall">
  <signature>
   <parameter name="parm1" description="test parameter" type="java.lang.String"/>
  </signature>
 </operation>
</MBean>


Assume that the MBean is used in an enterprise bean. Register your MBean in the enterprise bean ejbCreate method and unregister it in the ejbRemove method.

//The method MBeanFactory.activateMBean() requires four parameters:

//String type: The type value that you put in this MBean's descriptor. For this example 

//the string type is SnoopMBean.

//RuntimeCollaborator co: The UserMBeanCollaborator user MBean collaborator instance 

//created

//String id: Unique name that you pick

//String desciptor: The MBean descriptor file name

 import com.ibm.websphere.management.UserMBeanCollaborator;

//Import other classes here.
.
.
. static private ObjectName snoopyON = null; static private Object lockObj = "this is a lock";
.
.
.
/**
 * ejbCreate method: Register the Mbean.
 */ public void ejbCreate() throws javax.ejb.CreateException {
    synchronized (lockObj) {
        System.out.println(">>> SnoopMBean activating for --|" + this + "|--");
        if (snoopyON != null) {
            return;
        }
        try {
            System.out.println(">>> SnoopMBean activating...");
            MBeanFactory mbfactory = AdminServiceFactory.getMBeanFactory();
            RuntimeCollaborator snoop = new UserMBeanCollaborator(new SnoopMBeanImpl());
            snoopyON = mbfactory.activateMBean("SnoopMBean", snoop, "snoopMBeanId", 
"SnoopMBean.xml");
            System.out.println(">>> SnoopMBean activation COMPLETED! --|" + snoopyON + "|--");
        } catch (Exception e) {
            System.out.println(">>> SnoopMBean activation FAILED:");
            e.printStackTrace();
        }
    }
}
.
.
.
/**
 * ejbRemove method: Unregister the MBean.
 */ public void ejbRemove() {
    synchronized (lockObj) {
        System.out.println(">>> SnoopMBean Deactivating for --|" + this + "|--");
        if (snoopyON == null) {
            return;
        }
        try {
            System.out.println(">>> SnoopMBean Deactivating ==|" + snoopyON + "|== for --|" 
+ this + "|--");
            MBeanFactory mbfactory = AdminServiceFactory.getMBeanFactory();
            mbfactory.deactivateMBean(snoopyON);
            System.out.println(">>> SnoopMBean Deactivation COMPLETED!");
        } catch (Exception e) {
            System.out.println(">>> SnoopMBean Deactivation FAILED:");
            e.printStackTrace();
        }
    }
}

 

Next steps

Compile the MBean Java files and package the resulting class files with the descriptor .xml file, into the enterprise bean JAR file.

 

Related tasks


Best practices for standard, dynamic, and open MBeans