Thursday 26 July 2018

JMX: Expose a Resource for Remote Management by jconsole

In this post, I am going to explain how to expose a resource for remote management.

It is very simple, run the main application using below arguments.

-Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

For the sake of simplicity, I am not considered security aspects like encryption and authentication here.

Find the below working 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;
 }

}

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();

}

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;
 }

}

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);
 }

}

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, by passing below arguments.
-Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

If you are running in Eclipse, follow below steps to pass above arguments.
Right click on Application.java.
Run As -> Run Configurations…

In VM arguments: filed, add below arguments.
-Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

Click on Run button.

Open command prompt (or) terminal and execute jconsole command.

Select ‘Remote Process:’ and enter the url as localhost:9999 and click on Connect button.

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

For the left panel, navigate to AppStats. In the right side of the window, double click on Value field.

You can see readCount, writeCount values as 0.


Select the attribute ReadCount, enter the value as 50 and press Enter.

Come to ‘AppStats’ again, you can able to see ReadCount value as 50.


That’s it.. you are done….:)




Previous                                                 Next                                                 Home

No comments:

Post a Comment