Liberty: Examples of registering MBeans
An application can register its own MBean instances on Liberty. 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 that an application contains
a class that is 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 that 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 that the variable mbs
is an MBeanServer
or MBeanServerConnection
instance. See Working with JMX MBeans on Liberty.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 Liberty. The Java API documentation for each Liberty API is detailed in the Programming interfaces (Javadoc) section of the online IBM® documentation, and is also available as a separate .zip file in one of the javadoc subdirectories of the ${wlp.install.dir}/dev directory.