jmx中标准MBean使用
一、简介
jmx(java management extentions),即java管理扩展,用于对应用程序进行监控和管理。jmx是一个标准、接口或者框架,可以在应用程序运行期,对自定义类发起调用。这里对标准MBean进行介绍。
二、标准Mbean开发步骤
标准Mbean开发步骤如下:
自定义以MBean结尾的接口,如:StaticConfigManagerMBean;
实现步骤1定义的接口,特别注意接口实现类名必须是接口去掉结尾MBean,如:StaticConfigManager;
对MBean实现类进行注册,如:
//mbean注册
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("staticConfigManagerMBean:name=staticConfigManager");
mBeanServer.registerMBean(new StaticConfigManager(), name);
在应用程序开启监听端口,方式有两种,程序中开启和启动类添加参数开启。
方式一,程序中开启,如:
//开启jmx监听端口10014
LocateRegistry.createRegistry(10014);
//指定监控对象及名称
JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:10014/jmxrmi");
JMXConnectorServer jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mBeanServer);
//开启监控
jmxConnectorServer.start();
方式二,启动类添加参数开启,如:
-Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.port=10014 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
启动java工具jconsole,并远程连接,连接地址如:
localhost:10014
在jconsole连接成功后,在界面MBean中,调用自定义的MBean接口实现。
三、示例
这里以操作静态类StaticConfig的MBean管理为例。
3.1 示例项目
定义表态类
package com.dragon.study.spring_boot_main;
public class StaticConfig {
//自定义静态属性
public static String configName="apple";
//自定义静态方法
public static String sayHello(){
return "hello " + configName;
}
}
定义MBean MBean接口如下:
package com.dragon.study.spring_boot_main.mbean;
public interface StaticConfigManagerMBean {
String printConfigName();
void changeConfigName(String configName);
}
MBean接口实现类如下:
package com.dragon.study.spring_boot_main.mbean;
import com.dragon.study.spring_boot_main.StaticConfig;
public class StaticConfigManager implements StaticConfigManagerMBean {
@Override
public String printConfigName() {
System.out.println("StaticConfigManager, configName:" + StaticConfig.configName);
return StaticConfig.configName;
}
@Override
public void changeConfigName(String configName) {
StaticConfig.configName = configName;
}
}
启动类里注册MBean及开启监控端口
package com.dragon.study.spring_boot_main;
import com.dragon.study.spring_boot_main.mbean.StaticConfigManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
@SpringBootApplication
public class SpringBootMainApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootMainApplication.class, args);
//主动加载静态类
Class.forName("com.dragon.study.spring_boot_main.StaticConfig");
//mbean注册
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("staticConfigManagerMBean:name=staticConfigManager");
mBeanServer.registerMBean(new StaticConfigManager(), name);
//开启jmx监听端口10014
LocateRegistry.createRegistry(10014);
//指定监控对象及名称
JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:10014/jmxrmi");
JMXConnectorServer jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mBeanServer);
//开启监控
jmxConnectorServer.start();
}
}
3.2 测试
测试如下:
命令行输入jconsole,弹出界面选择远程连接,输入地址localhost:10014;
进程jconsole界面后,选择MBean界面,对自定义MBean进行调用,如:
staticConfigManagerMBean -> staticConfigManager -> 操作 -> changeConfigName,打印静态类的静态属性;
staticConfigManagerMBean -> staticConfigManager -> 操作 -> printConfigName,修改静态类的静态属性;