JMX示例

时间:2023-12-17 11:16:02

HelloJMXMBean.java

package jmx;

/**
* Created by george on 14-8-21.
*/
public interface HelloJMXMBean {
public String getName();
public void setName(String name);
public void printHello();
public void printHello(String whoName);
}

HelloJMX.java

package jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory; /**
* Created by george on 14-8-21.
*/
public class HelloJMX implements HelloJMXMBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("HelloJMX, " + name);
}
public void printHello(String whoName) {
System.out.println("HelloJMX, it is " + whoName);
} public static void main(String[] args) throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName helloName = new ObjectName("george:name=HelloJMX");
server.registerMBean(new HelloJMX(), helloName); Thread.sleep(Long.MAX_VALUE);
} }

1. run....

2. jconsole....