+

Search Tips   |   Advanced Search

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 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 we 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:

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 Work with JMX MBeans .

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, we can use other forms of the MBeanServer.createMBean method to create the MBean 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.


Parent topic: Work with JMX MBeans