WAS v8.5 > Reference > Developer examplesLiberty profile: Examples of registering MBeans
This topic describes how an application can register its own MBean instances on the Liberty profile, and then the MBean instance can be used by other applications or external administrators.
Any application can register an MBean via 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:
- Implements any empty interface org.example.ApplicationClassLoaderMBean
- Extends java.lang.Classloader
- 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, 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 document for each Liberty profile API is detailed in the Programming Interfaces (APIs) section of the information center, and is also available as a JAR file under the /dev/ibm-api/javadoc directory of the server image.
Parent topic: Working with JMX MBeans on the Liberty profile
Terms and conditions for information centers | Feedback