Fix Pack 8501

Liberty profile: Examples of registering MBeans

An application can register its own MBean instances on the Liberty profile. That MBean instance can then be used by other applications or external administrators.

Any application can register an MBean by using an MBeanServer instance. Suppose an application contains a class called org.example.Example that implements the interface org.example.ExampleMBean, which defines some attributes and operations. As in the following example, the application might simply instantiate the Example class then register it using a unique ObjectName. If the ObjectName chosen is already in use, a javax.management.InstanceAlreadyExistsException is reported.
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.example.Example;

...

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Object mbean = new Example();
ObjectName name = new ObjectName("org.example.MyApplication:name=Example");
mbs.registerMBean(mbean, name);
In addition, an application might register an MBean that extends java.lang.ClassLoader and provides access to any number of MBean implementation classes. Then you can use any other JMX client, local or remote, to create and register MBeans provided by the application. For example, suppose the application has an MBean class org.example.ApplicationClassLoader that performs the following tasks:
  • Implements any empty interface org.example.ApplicationClassLoaderMBean
  • Extends java.lang.Classloader, and
  • Provides access to the org.example.Example MBean implementation class
The application can register an instance of ApplicationClassLoader to make the Example MBean available to other JMX clients as follows:
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.example.ApplicationClassLoader;

...

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Object classLoader = new ApplicationClassLoader();
ObjectName name = new ObjectName("org.example.MyApplication:name=ClassLoader");
mbs.registerMBean(classLoader, name);
Any JMX client can create an Example instance. The following example assumes the variable mbs is an MBeanServer or MBeanServerConnection instance. See Working with JMX MBeans on the Liberty profile.
import javax.management.ObjectName;

...

ObjectName loaderName = new ObjectName("org.example.MyApplication:name=ClassLoader");
ObjectName exampleName = new ObjectName("org.example.MyApplication:name=Example");
mbs.createMBean("org.example.Example”, exampleName, loaderName);

If necessary, you can use other forms of the MBeanServer.createMBean method to create the MBean by using non-default constructors.

For more information about the management interface, see the Java API document for the Liberty profile. The Java API documentation for each Liberty profile API is detailed in the Programming Interfaces (APIs) section of the information center, and is also available as a separate .zip file in one of the javadoc subdirectories of the ${wlp.install.dir}/dev directory.


Icon that indicates the type of topic Reference topic

Terms and conditions for information centers | Feedback


Timestamp icon Last updated: Monday, 21 April 2014
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-base-iseries&topic=rwlp_mbeans_registration
File name: rwlp_mbeans_registration.html