JMX
package com.example;
public interface HelloMBean {
public void sayHello();
public int add(int x, int y);
public String getName();
public int getCacheSize();
public void setCacheSize(int size);
} package com.example;
public class Hello ...
implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public int add(int x, int y) {
return x + y;
}
public String getName() {
return this.name;
}
public int getCacheSize() {
return this.cacheSize;
}
public synchronized void setCacheSize(int size) {
...
this.cacheSize = size;
System.out.println("Cache size now " + this.cacheSize);
}
...
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int
DEFAULT_CACHE_SIZE = 200;
}package com.example;
import java.lang.management.*;
import javax.management.*;
public class Main {
public static void main(String[] args)
throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=Hello");
Hello mbean = new Hello();
mbs.registerMBean(mbean, name);
...
System.out.println("Waiting forever...");
Thread.sleep(Long.MAX_VALUE);
}
}
Without authentication
- java -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=9001 -Dcom.sun.management.jmxremote.ssl=false -jar example.jar
- jconsole hostx:9001
With authentication
- java -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.port=9001 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.file=/home/userx/jmxremote.password -jar example.jar
- jconsole hostx:9001
