You can create standard, dynamic, and open custom MBeans and register them with the WebSphere Application Server administrative service.
This task assumes a basic familiarity with MBean programming. For information on MBean programming see MBean Java application programming interface (API) documentation.
Perform the following tasks to create and register a standard, dynamic, or open custom MBean.
ResultAfter you successfully complete the steps, you have a standard, dynamic, or open custom MBean that is registered and activated with the WebSphere Application Server administrative service.
Example
The following example shows how to create and register a standard MBean with the WebSphere Application Server 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() { System.out.println(">>> getIdentification() called..."); return "snoopy!"; } public void snoopy(String parm1) { System.out.println(">>> snoopy(" + parm1 + ") called..."); } }
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>
//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 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 your 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 your 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(); } } }
Related tasks
Best practices for standard, dynamic, and open MBeans