1,接口
/**
* standard MBeans, dynamic MBeans, open MBeans(形如MXBean)
* and model MBeans,@MXBean()注解的接口名字可以任意取
* @author river.wang
*/
@MXBean()
public interface CMonitor0 {
public long getUptime();
public String getMemory();
}
2,MBean,被管理对象,一如JavaBean。Bean对象,实体,对应资源。。。
/**
* @author river.wang
*/
public class ServerMonitor implements CMonitor0{
private final long startTime;
private Runtime runtime = Runtime.getRuntime();
public ServerMonitor() {
startTime = System.currentTimeMillis();
}
public long getUptime() {
return System.currentTimeMillis()-startTime;
}
public String getMemory() {
float freeMemory = (float)runtime.freeMemory();
float totalMemory = (float)runtime.totalMemory();
String memory = "idle scale : "+(freeMemory/totalMemory)+"%; freeMemory="
+(freeMemory)/1024+" KB; totalMemory="+(totalMemory)/1024+" KB" ;
return memory;
}
}
3,运行下面程序,就可以用jconsole进行本地连接了
/**
* @author river.wang
*/
public class ServerMonitorAgent {
public static void main( String[] args )
throws Exception {
// MBeanServer server=MBeanServerFactory.createMBeanServer(); //不可在jconsole中使用
MBeanServer server = ManagementFactory.getPlatformMBeanServer();// 可在jconsole中使用
// 被管理的bean,"com.jmx"包名任意取
ObjectName monitorName = new ObjectName( "com.jmx:type=ServerMonitor" );
server.registerMBean( new ServerMonitor(), monitorName );
// 必须确保线程活着
Thread.sleep( Long.MAX_VALUE );
}
}
4.1,指定端口--Server端
/**
* @author river.wang
*/
public class ServerMonitorAgentv2 {
public static void main( String[] args )
throws Exception {
// MBeanServer server=MBeanServerFactory.createMBeanServer(); //不可在jconsole中使用
MBeanServer server = ManagementFactory.getPlatformMBeanServer();// 可在jconsole中使用
// 指定端口
LocateRegistry.createRegistry( 1098 );
JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + 1098 + "/myJmx" );
Map<String, Object> env = new HashMap<String, Object>();
// System.setProperty( "com.sun.management.jmxremote.authenticate", "true" );
// env.put( "jmx.remote.credentials", new String[] { "admin", "admin123" } );
JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer( url, env, server );
cntorServer.start();
// 被管理的bean,"com.jmx"包名任意取,可以多个Bean
ObjectName monitorName = new ObjectName( "com.jmx:type=ServerMonitor" );
server.registerMBean( new ServerMonitor(), monitorName );
}
}
4.2,指定端口--Client端
/**
* @author river.wang
*/
public class ServerMonitorClient {
public static void main( String[] args )
throws Exception {
JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://192.168.22.91:1098/myJmx" );
Map<String, Object> env = new HashMap<String, Object>();
// env.put( "jmx.remote.credentials", new String[] { "admin", "admin123" } );
JMXConnector conn = JMXConnectorFactory.connect(url,env );
CMonitor0 monitor = JMX.newMBeanProxy( conn.getMBeanServerConnection(),
new ObjectName( "com.jmx:type=ServerMonitor" ),CMonitor0.class );
for ( int i = 0; i < 10; i++ ) {
System.out.println( monitor.getUptime() );
System.out.println( monitor.getMemory() );
}
conn.close();
}
}