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

本文介绍如何使用JMX技术在不重启应用的情况下动态调整连接数。通过定义MBean接口、实现该接口并注册到MBeanServer中,实现对应用参数的动态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Java代码   收藏代码
  1. package  com.example;   
  2.    
  3. public   interface  ConnectionNumberMBean {   
  4.     public   int  getConnectionNumber();   
  5.     public   void  setConnectionNumber( int  num);   
  6. }  
package com.example; 
 
public interface ConnectionNumberMBean { 
    public int getConnectionNumber(); 
    public void setConnectionNumber(int num); 
}

 第二步:实现接口

Java代码   收藏代码
  1. package  com.example;   
  2. import  javax.management.*;  
  3. public   class  ConnectionNumber  extends  NotificationBroadcasterSupport  implements  ConnectionNumberMBean {   
  4.    
  5.     public   int  getConnectionNumber() {   
  6.     return   this .conNum ;   
  7.     }   
  8.    
  9.     public   synchronized   void  setConnectionNumber( int  con_Num ) {  
  10.   
  11.     this .conNum = con_Num ;   
  12.    
  13.     System.out.println("Connection number now "  +  this .conNum );   
  14.     }   
  15.     private   int  conNum = DEFAULT_CON_Num;  
  16.     private   static   final   int  DEFAULT_CON_Num=  20 ;   
  17. }  
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来管理资源

Java代码   收藏代码
  1. package  com.example;  
  2.   
  3. import  java.lang.management.ManagementFactory;  
  4. import  javax.management.MBeanServer;  
  5. import  javax.management.ObjectName;  
  6.   
  7. public   class  Main {  
  8.     /* For simplicity, we declare "throws Exception".  
  9.        Real programs will usually want finer-grained exception handling. */   
  10.     public   static   void  main(String[] args)  throws  Exception {  
  11.         // Get the Platform MBean Server   
  12.         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();  
  13.   
  14.     // Construct the ObjectName for the Hello MBean we will register   
  15.     ObjectName mbeanName = new  ObjectName( "com.example:type=ConnectionNumber" );  
  16.   
  17.     // Create the Hello World MBean   
  18.     ConnectionNumber  mbean = new  ConnectionNumber();  
  19.   
  20.     // Register the Hello World MBean   
  21.     mbs.registerMBean(mbean, mbeanName);  
  22.   
  23.         // Wait forever   
  24.         System.out.println("Waiting for incoming requests..." );  
  25.         Thread.sleep(Long.MAX_VALUE);  
  26.     }  
  27. }  
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、付费专栏及课程。

余额充值