Wednesday 25 July 2018

JMX: Working with Html Adapter

In my previous post, I explained how to connect to JMX Agent using JConsole. In this post, I am going to explain how to manage/monitor the JMX beans using a web browser.

To manage MBeans via browser, we need HtmlAdaptorServer.

Add below maven dependency to your project.
<dependency>
         <groupId>com.sun.jdmk</groupId>
         <artifactId>jmxtools</artifactId>
         <version>1.2.1</version>
</dependency>

Step 1: Initialize HtmlAdaptorServer
HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer();
ObjectName adapterName = new ObjectName("SimpleAgent:name=htmladapter,port=8000");
htmlAdaptorServer.setPort(8000);

Step 2: Register htmlAdaptorServer with MBeanServer.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(htmlAdaptorServer, adapterName);

Step 3: Start the htmlAdaptorServer
htmlAdaptorServer.start();

Find the below working application.

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() {
  // TODO Auto-generated method stub
  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;
 }

}

Application.java
package com.sample.app;

import java.lang.management.ManagementFactory;

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

import com.sample.mbeans.SystemStatistics;
import com.sample.mbeans.SystemStatisticsMBean;
import com.sun.jdmk.comm.HtmlAdaptorServer;

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

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

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

  HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer();
  ObjectName adapterName = new ObjectName("SimpleAgent:name=htmladapter,port=8000");
  htmlAdaptorServer.setPort(8000);

  mbs.registerMBean(htmlAdaptorServer, adapterName);
  htmlAdaptorServer.start();

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


Run Application.java and open the url ‘http://localhost:8000/’ in browser.

Click on SystemStatisticsMBean.

Now you can perform operations.






Previous                                                 Next                                                 Home

No comments:

Post a Comment