jmx 实例--最简单的例子

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值