Network Deployment (Distributed operating systems), v8.0 > Administer applications and their environment > Use the administrative clients > Use administrative programs (JMX) > Extend the WAS administrative system with custom MBeans


Create and registering standard, dynamic, and open custom MBeans

We can create standard, dynamic, and open custom MBeans and register them with the product administrative service. This task assumes a basic familiarity with MBean programming. For information on MBean programming, see MBean Java application programming interface (API) documentation. In this information center, click Reference > Mbean interfaces.

Do not define new classes as parameters for your 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 application servers. If the class is not available for a process, the ClassNotFoundException exception occurs for the new class when you attempt to access it.

To create and register a standard, dynamic, or open custom MBean.


Procedure

  1. Create your particular MBean class or classes.
  2. Write an MBean descriptor in the XML language for your MBean.
  3. Register your 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 your 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 the product administrative service.


Example

The following example shows how to create and register a standard MBean with the administrative 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() {
        .println(">>> getIdentification() called...");
        return "snoopy!";
    }

    public void snoopy(String parm1) {
        .println(">>> snoopy(" + parm1 + ") called...");
    }
}

Define the following MBean descriptor for your 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. If you are 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 your 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
//that you create //String id: Unique name that you pick
//String descriptor: The full path to the MBean descriptor file

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 your Mbean.
 */
public void ejbCreate() throws javax.ejb.CreateException {
    synchronized (lockObj) {
        .println(">>> SnoopMBean activating for --|" + this + "|--");
        if (snoopyON != null) {
            return;
        }
        try {
            .println(">>> SnoopMBean activating...");
            MBeanFactory mbfactory = AdminServiceFactory.getMBeanFactory();
            RuntimeCollaborator snoop = new UserMBeanCollaborator(new SnoopMBeanImpl());
            snoopyON = mbfactory.activateMBean("SnoopMBean", snoop, "snoopMBeanId",
"SnoopMBean.xml");
            .println(">>> SnoopMBean activation COMPLETED! --|" + snoopyON + "|--");
        } catch (Exception e) {
            .println(">>> SnoopMBean activation FAILED:");
            e.printStackTrace();
        }
    }
}
.
.
.
/**
 * ejbRemove method: Unregister your MBean.
 */
public void ejbRemove() {
    synchronized (lockObj) {
        .println(">>> SnoopMBean Deactivating for --|" + this + "|--");
        if (snoopyON == null) {
            return;
        }
        try {
            .println(">>> SnoopMBean Deactivating ==|" + snoopyON + "|== for --|"
+ this + "|--");
            MBeanFactory mbfactory = AdminServiceFactory.getMBeanFactory();
            mbfactory.deactivateMBean(snoopyON);
            .println(">>> SnoopMBean Deactivation COMPLETED!");
        } catch (Exception e) {
            .println(">>> SnoopMBean Deactivation FAILED:");
            e.printStackTrace();
        }
    }
}


What to do next

Compile the MBean Java files and package the resulting class files with the descriptor .xml file, into the enterprise bean JAR file.
Best practices for standard, dynamic, and open MBeans

+

Search Tips   |   Advanced Search