Create and register MBean in MBeanServer

In this example we shall show you how to create and register an MBean in MBeanServer. An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed. MBeans expose a management interface that consists of a set of readable or writable attributes, or both, a set of invokable operations and a self-description. To create and register an MBean in MBeanServer one should perform the following steps:

  • Create a Java interface. Here we create HelloBean. Every method in the interface defines either an attribute or an operation in the MBean. The interface has a method, sayHello() a read-write attribute called message of type String.
  • Create a Java class, Hello in the example, that implements that interface
  • Create an MBeanServer that is the interface for MBean manipulation on the agent side. Use getPlatformMBeanServer() API method of ManagementFactory.
  • Define the object name for the MBean. The object name is an instance of the JMX class ObjectName and must contain a domain and a list of key-properties.
  • Create a new instance of the MBean and register it to MBeanServer, using registerMBean(Object object, ObjectName name) API method of MBeanServer.
  • Use queryMBeans(ObjectName name, QueryExp query) API method of MBeanServer with the given ObjectName to get all ObjectInstances of the specified MBean.
  • Get the first ObjectInstance name and class,

as described in the code snippet below.  

package com.javacodegeeks.snippets.enterprise;

import java.util.Set;


public class CreateAndRegisterMBeanInMBeanServer {

	public static void main(String[] args) throws Exception {
		String objectName = "com.javacodegeeks.snippets.enterprise:type=Hello";
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();

	    // Construct the ObjectName for the Hello MBean we will register
	    ObjectName mbeanName = new ObjectName(objectName);
	    Hello mbean = new Hello();
	    server.registerMBean(mbean, mbeanName);
	    Set<ObjectInstance> instances = server.queryMBeans(new ObjectName(objectName), null);
	    ObjectInstance instance = (ObjectInstance) instances.toArray()[0];
	    System.out.println("Class Name:t" + instance.getClassName());
	    System.out.println("Object Name:t" + instance.getObjectName());
	static class Hello implements HelloMBean {
		private String message = "Hello World";

		public String getMessage() {
			return this.message;

		public void sayHello() {

		public void setMessage(String message) {
			this.message = message;
	static interface HelloMBean {
		// operations
		public void sayHello();

		// attributes
		// a read-write attribute called Message of type String
		public String getMessage();
		public void setMessage(String message);


Class Name:	com.javacodegeeks.snippets.enterprise.CreateAndRegisterMBeanInMBeanServer$Hello
Object Name:	com.javacodegeeks.snippets.enterprise:type=Hello

This was an example of how to create and register an MBean in MBeanServer.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button