Entering content frame

Background documentation Registering MBeans

Registering a Local MBean

There are cases when you want to register a locally created object as a MBean. The crucial part here is the creation of the ObjectName. It must be done using the ObjectNameFactory, wherever possible. For more information, see com.sap.jmx.ObjectNameFactory.

In the first example, the object is registered as a local MBean, so that it is visible from other cluster elements:

Example

import javax.management.MBeanServer;

import javax.management.ObjectName;

import com.sap.jmx.ObjectNameFactory;

import com.sap.xyz.MyMBean;

...

MBeanServer mbs;

// Create the MBean manually

MyBean mbean = new MyMBean();

...

// Create the ObjectName for a local MBean

//

// The result will be:

// ":j2eeType=SAP_MyMBean,name=MyBean,SAP_J2EECluster=\"\", SAP_J2EEClusterNode=\"\""

//

// The empty keys SAP_J2EECluster and SAP_J2EEClusterNode will be filled by the MBeanServer with the

// appropriate values for local MBeans. Finally, for the registration, something like this will be used:

// "com.sap.default:j2eeType=SAP_MyMBean,name=MyBean,SAP_J2EECluster=BIN, SAP_J2EEClusterNode=4001"

//

ObjectName name = ObjectNameFactory.getNameForServerChildPerNode
(
"SAP_MyMBean", "MyBean", null, null);

// Register the MBean

mbs.registerMBean(mbean, name);

 

Displaying MBeans in the Visual Administrator

If you want your MBean to be displayed as a node in the Visual Administrator, instead of the last line from the above example (mbs.registerMBean(mbean, name);) execute the following code snippet:

Properties p = new Properties();

p.setProperty("admin.path", "/Cluster/MyMBean");

p.setProperty("admin.displayName", "MyMBean");

BroadcastingStandardMBeanWrapper wrapper = null;

try {

  wrapper = new BroadcastingStandardMBeanWrapper(mbean, null, p);

  mbs.registerMBean(wrapper, name);

} catch (NotCompliantMBeanException ncmbe) {

  ncmbe.printStackTrace();

}

 

Caution

For the code to work properly, you have to make an additional import to: com.sap.engine.services.basicadmin.mbean.BroadcastingStandardMBeanWrapper.

As a result, the MBean will appear in the Visual Administrator. For more information, see Structure linkManaging MBeans Using the Visual Administrator in the Administration Manual.

Registering a Remote MBean

You can register a MBean locally only – that is, the SAP_J2EEClusterNode key must either be empty (as in the example above), or it must point to the local cluster element. However, you can also request the MBeanServer to create the MBean. In which case, you can also create MBeans on a remote element.

Typically, the class of the MBean you want to create is not visible for the class loader of the MBeanServer, that is, you have to specify the class loader to be used. The class loaders of the J2EE Engine are virtually registered as MBeans, so that you can specify an ObjectName for the loaderName parameter of the createMBean(...) methods. Again, the ObjectNameFactory helps to create such a name by taking a J2EE Engine class loader name as the parameter. The following code shows how you create a MBean on a remote element:

Example

import javax.management.MBeanServerConnection;

import javax.management.ObjectName;

import com.sap.jmx.ObjectNameFactory;

import com.sap.xyz.MyMBean;

...

MBeanServerConnection mbsc;

...

// create the MBean remotely, assuming the class is loaded by the class loader of

// service "myservice" on node 5001

mbsc.createMBean(

  MyMBean.class.getName(),

  ObjectNameFactory.getNameForServerChildPerNode("SAP_MyType", "MyBean", "5001", null),

  ObjectNameFactory.getNameForClassLoader("service:myservice", null, null));

 

A class loader that exists on one client element may not exist on the target element. Many services are not available, especially on dispatchers.

The life cycle of a local and internal MBeans does not follow any convention. However, for clustered MBeans you must "synchronize" the life cycle of all instances in the cluster. Typically, a service registers/unregisters the instances of its clustered MBeans during startup/shutdown. Applications cannot manage the life cycle of clustered MBeans on their own.

 

See also:

 

Structure linkClass Loading System in the Architecture Manual

 

 

Leaving content frame