Wednesday 25 July 2018

JMX: Introduction to MXBeans

An MXBean is a special type of MBean that references only a predefined set of data types.

MBean vs MXBean
An MXBean can be used by any client. But it is not be the case with MBean. In case of MBean, the reference types exposed by MBean interfaces must be serializable and available to client JVM. But in case of MXBeans, all the reference types in the interface are mapped to a predefined set of types defined in the javax.management.openbean.

For example,
a.   Primitive classes and wrappers are mapped to an equivalent SimpleType
b.   Arrays and Lists are mapped to an ArrayType
c.   Maps are mapped to a TabularType
d.   Any other objects are mapped to a CompositeType

How to define MXBean?
MXBean is defined by writing a Java interface called SomethingMXBean and a Java class that implements that interface. In this case interface name must be suffixed with MXBean.
                                    (or)

Annotate the interface with annotation @MXBean. In this case, interface name may not be suffixed with string MXBean.

Let me explain with an example.

Step 1: Define a model class 'CRUDStatistics.java' that stores number of reads and writes across the application.

CRUDStatistics.java

package com.sample.model;

public class CRUDStatistics {
 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;
 }

}

Step 2: Define MXBean interface.

SystemStatisticsMXBean.java

package com.sample.mbeans;

import com.sample.model.CRUDStatistics;

public interface SystemStatisticsMXBean {

 public void setReadCount(int readCount);
 
 public void setWriteCount(int writeCount);
 
 public CRUDStatistics getAppStats();

}

Step 3: Implement SystemStatisticsMXBean interface.


SystemStatistics.java

package com.sample.mbeans;

import com.sample.model.CRUDStatistics;

public class SystemStatistics implements SystemStatisticsMXBean {
 private CRUDStatistics appStats = new CRUDStatistics();

 @Override
 public void setReadCount(int readCount) {
  appStats.setReadCount(readCount);
 }

 @Override
 public void setWriteCount(int writeCount) {
  appStats.setWriteCount(writeCount);
 }

 @Override
 public CRUDStatistics getAppStats() {
  return appStats;
 }

}

Step 4: Create StatsAgent class, that registers this MBean to MBean server.


StatsAgent.java

package com.sample.agents;

import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import com.sample.mbeans.SystemStatistics;

public class StatsAgent {
 private MBeanServer mbs;
 private SystemStatistics mbean;

 public StatsAgent() throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException,
   NotCompliantMBeanException, IOException {
  ObjectName name = new ObjectName("com.sample.mbeans:type=SystemStatisticsMBean");
  mbean = new SystemStatistics();

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

}

Step 5: Define Application.java class like below.


Application.java

package com.sample.app;

import com.sample.agents.StatsAgent;

public class Application {

 public static void main(String[] args) throws Exception {
  new StatsAgent();

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

 }

}

Run Application.java class.


Open command prompt (or) terminal and execute jconsole command.
Select the process 'com.sample.app.Application' and click on Connect button.

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

Go to MBeans tab, select AppStats attribute and double click on value field (it is available in right side of the window).
You can see below kind of screen.
As you see above screen, readCount, writeCount are set to 0.

Let’s try to update readCount and see whether it is reflecting appStats attribute or not.

I entered readCount value as 50 and press Enter button.

Select ‘AppStats’ attribute, you can able to see readCount value as 50.

That’s it, you are done with MXBean setup. I hope you enjoyed this tutorial.

Previous                                                 Next                                                 Home

No comments:

Post a Comment