Tuesday, 7 August 2018

Jcmd: ManagementAgent.stop, ManagementAgent.start_local and ManagementAgent.start example

ManagementAgent.stop: Stop remote management agent.
ManagementAgent.start: Start remote management unit
ManagementAgent.start_local: Start local management agent.

To demonstrate the application, I am going to build simple JMX application, and start the management agent using jcmd command, then connect to the JMX agent using jconsole and stop the management agent using jcmd command.

Building jmx application
Follow below steps and setup JMX application.

Create new java project in Eclipse. Define SystemStatisticsMBean.java like below

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

}


JMXAgent.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 JMXAgent {
 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 JMXAgent.java.

Start Management agent using jcmd

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

C:\>jcmd
7296 com.sample.app.JMXAgent
13220
5752 sun.tools.jcmd.JCmd


As you see above output, JMXAgent application is running with pid 7296.

Check the commands available with process id 7296 by executing the statement 'jcmd 7296 help'.

C:\>jcmd 7296 help
7296:
The following commands are available:
JFR.stop
JFR.start
JFR.dump
JFR.check
VM.native_memory
VM.check_commercial_features
VM.unlock_commercial_features
ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start
GC.rotate_log
Thread.print
GC.class_stats
GC.class_histogram
GC.heap_dump
GC.run_finalization
GC.run
VM.uptime
VM.flags
VM.system_properties
VM.command_line
VM.version
help


Start the management agent
Start the management agent by executing below command.

jcmd 7296 ManagementAgent.start jmxremote.port=9999 jmxremote.authenticate=false jmxremote.ssl=false

C:\>jcmd 7296 ManagementAgent.start jmxremote.port=9999 jmxremote.authenticate=false jmxremote.ssl=false
7296:
Command executed successfully


Connecting to JMXAgent using jconsole
Open command prompt (or) terminal and execute the command ‘jconsole’.

Select radio button ‘Remote Process’ and enter the url localhost:9999.




Click on connect button.

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



It opens below screen. Navigate to MBeans tab and you can able to see all the operations exposed by MBean in the left side of the window.


Stop the management agent using jcmd
Open command prompt (or) terminal and execute the statement ‘jcmd 7296 ManagementAgent.stop’.

C:\>jcmd 7296 ManagementAgent.stop
7296:
Command executed successfully


Once the command executed successfully, jconsole thorws below warning message.

Jul 26, 2018 11:45:27 AM ClientCommunicatorAdmin restart
WARNING: Failed to restart: java.rmi.NoSuchObjectException: no such object in table




Previous                                                 Next                                                 Home

No comments:

Post a Comment