一、簡介
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,修改靜態類的靜態屬性;