tomcat 7 源码分析-6 server初始化中的JMX(DynamicMBean)续

先说JMX,The JMX technology provides a simple, standard way of managing resources such as applications, devices, and services.

JMX是为了管理资源产生的,这个资源包括应用、设备和服务等。取个例子,如果你写了一个应用,初始化了20个的数据连接数,当你的应用还在跑的时候,发觉这个连接数少了,你要增加这个参数。JMX提供不需要停机,动态的管理连接数的方法。

先从简单的Standard MBeans看起。

通常我们先定义interface

第一步:MBean Interface

package com.example; 
 
public interface ConnectionNumberMBean { 
    public int getConnectionNumber(); 
    public void setConnectionNumber(int num); 
}

 第二步:实现接口

package com.example; 
import javax.management.*;
public class ConnectionNumber extends NotificationBroadcasterSupport implements ConnectionNumberMBean { 
 
    public int getConnectionNumber() { 
	return this.conNum ; 
    } 
 
    public synchronized void setConnectionNumber(int con_Num ) {

	this.conNum = con_Num ; 
 
	System.out.println("Connection number now " + this.conNum ); 
    } 
    private int conNum = DEFAULT_CON_Num;
    private static final int DEFAULT_CON_Num= 20; 
}

 第三步:创建a JMX Ageng来管理资源

package com.example;

import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

public class Main {
    /* For simplicity, we declare "throws Exception".
       Real programs will usually want finer-grained exception handling. */
    public static void main(String[] args) throws Exception {
        // Get the Platform MBean Server
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

	// Construct the ObjectName for the Hello MBean we will register
	ObjectName mbeanName = new ObjectName("com.example:type=ConnectionNumber");

	// Create the Hello World MBean
	ConnectionNumber  mbean = new ConnectionNumber();

	// Register the Hello World MBean
	mbs.registerMBean(mbean, mbeanName);

        // Wait forever
        System.out.println("Waiting for incoming requests...");
        Thread.sleep(Long.MAX_VALUE);
    }
}

 编译生产Class文件后,运行它。

打开windows控制台,运行jconsole命令,打开java控制台,如下图。

可以看见我们注册的mian已经在列表上,可以连接

看下图,点MBean

修改属性值,结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值