Wednesday, 25 July 2018

JMX: HelloWorld example


A standard MBean is defined by writing a Java interface called ‘SomethingMBean’ and a Java class called ‘Something’ that implements 'SomethingMBean' interface. You must follow the same naming conventions.

For example, I am going to define SystemStatisticsMBean interface that is implemented by SystemStatistics class. Every method in the MBean interface defines an operation and these can be accessed via remote management systems like jconsole.


As per convention, a MBean interface name = implementation class name + MBean. In our example, SystemStatistics is the class name, so Mbean name is like SystemStatisticsMBean.


Step 1: Define SystemStatisticsMBean interface.

SystemStatisticsMBean.java
package com.sample.mbeans;

public interface SystemStatisticsMBean {

 public int readCount();

 public int incrementReadCount();

 public int decrementReadCount();

 public int writeCount();

 public int incrementWriteCount();

 public int decrementWriteCount();

}

SystemStatistics.java
package com.sample.mbeans;

public class SystemStatistics implements SystemStatisticsMBean {

 private int readCount = 0;
 private int writeCount = 0;

 public int getReadCount() {
  return readCount;
 }

 public void setReadCount(int readCount) {
  this.readCount = readCount;
 }

 public int getWriteCount() {
  return writeCount;
 }

 public void setWriteCount(int writeCount) {
  this.writeCount = writeCount;
 }

 @Override
 public int readCount() {
  return readCount;
 }

 @Override
 public int incrementReadCount() {
  return ++readCount;
 }

 @Override
 public int decrementReadCount() {
  return --readCount;
 }

 @Override
 public int writeCount() {
  return writeCount;
 }

 @Override
 public int incrementWriteCount() {
  return ++writeCount;
 }

 @Override
 public int decrementWriteCount() {
  return --writeCount;
 }

}

By defining SystemStatisticsMBean.java and SystemStatistics.java, you are done with MBean creation. To access MBean from outside world, we need to register this MBean to an MBean server.

Below statements create MBean server instance and register MBean to MBean server.

ObjectName name = new ObjectName("com.sample.mbean.interfaces:type=SystemStatisticsMBean");
SystemStatistics mbean = new SystemStatistics();

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(mbean, name);

Application.java
package com.sample.app;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import com.sample.mbeans.SystemStatistics;

public class Application {
 public static void main(String[] args) throws Exception {

  MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  
  ObjectName name = new ObjectName("com.sample.mbean.interfaces:type=SystemStatisticsMBean");
  SystemStatistics mbean = new SystemStatistics();
  
  mbs.registerMBean(mbean, name);

  System.out.println("Waiting forever...");
  Thread.sleep(Long.MAX_VALUE);
 }
}

Run Application.java.

Open command prompt and execute jconsole command. It opens below user interface.


Select the application ‘com.sample.app.Application’, Click on Connect button.


If you see below kind of screen, Click on ‘Insecure connection’ button.

It opens below kind of window.
Go to ‘MBeans’ tab, On the left side of the window, you can able to see all the exposed operations.

Select the readCount operation.

Click on readCount button, it opens a window that displays the value of the variable readCount.


Select the operation ‘incrementReadCount’ and click on the button ‘IncrementReadCount’.


It opens new window by displaying return value.

Click on OK button.

Go to ‘readCount’ operation and click on ‘readCount’ button, you can see the updated value there.

That’s it….We are done with first JMS application….:)

Explanation
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Creates an instance of MBeanServer.

ObjectName name = new ObjectName("com.sample.mbean.interfaces:type=SystemStatisticsMBean");
Every JMX bean must have a object name associated with it. Object names are used to uniquely identify the MBeans in a MBean server. An object name contains two parts (A domain name followed by list of key properties).
a.   Domain Name: Domain names are used to segregate Mbeans. If you do not specify domain name, MBean server provides a default domain name. In the above example ‘com.sample.mbean.interfaces’ is the domain name.
b.   key=value property list: These are used to provide information about MBeans. These properties are also used to uniquely identify MBean. You can supply multiple key=value pairs by separating them by ,.

mbs.registerMBean(mbean, name);
Registes java object to MBean server. ‘name’ specifies the object name of the Mbean.



Previous                                                 Next                                                 Home

No comments:

Post a Comment