1.概述
什么是jmx,首先来看一段对话:

java management extensions(jmx)是一种java标准,用于管理和监控java应用程序,特别是分布式系统。它提供了一种标准化的方式来管理应用程序的各种方面,包括性能监控、配置更改、事件通知等。目前jmx最常用的就是用来做java程序的监控,市面上常见的java 监控框架基本都是基于jmx来实现的。
jmx分为三部分:
mbean:mbean是用来提供功能的,mbean是管理对象的标准接口,通过mbean可以获取和操作应用程序的各种属性和操作。java提供了几种类型的mbean,包括标准mbean、动态mbean、模型mbean等,每种类型适用于不同的场景。
mbean服务器:mbean服务器是用来管理mbean的,它是一个注册表,用于注册和管理mbean。应用程序通过mbean服务器暴露mbean,并且其他应用程序或工具可以连接到mbean服务器以检索和操作这些mbean。
jmx代理:jmx代理是用来访问mbean服务器的,可以理解为cs架构中的client端,支持多种访问方式。jmx代理是一个位于应用程序和mbean服务器之间的中间层,用于管理远程应用程序。它允许远程管理工具连接到远程应用程序的mbean服务器,并执行各种管理操作,如监控、配置和诊断。

jmx标准的实现通常由java虚拟机(jvm)提供支持。java虚拟机会实现jmx标准,以便应用程序可以利用jmx的功能。jvm会提供一个mbean服务器,应用程序可以将mbean注册到该服务器中,使其可供其他应用程序或工具访问和操作。
java虚拟机通过提供标准的mbean服务器和相关的api,为应用程序提供了一种标准化的方式来管理和监控应用程序的各个方面。这种标准化使得开发人员可以使用相同的接口和工具来管理不同的java应用程序,从而提高了管理的效率和一致性。
2.mbean
mbean服务器由于是jvm实现的,所以对我们应用开发者来说,最核心的其实就是mbean了,因为mbean是用来实现功能的。jmx标准中将mbean分为以下几种:
standard mbean(标准 mbean):它是最简单的一种 mbean 类型。一个标准 mbean 由一个 java 接口和一个对应的实现类组成。接口中定义了 mbean 的属性和操作,实现类则提供了对应的实现。标准 mbean 的命名约定为
yourinterfacenamembean。dynamic mbean(动态 mbean):与标准 mbean 不同,动态 mbean 可以在运行时动态地确定其属性和操作。动态 mbean 的属性和操作是在运行时通过方法调用来确定的,因此可以根据需要进行动态修改。
model mbean(模型 mbean):模型 mbean 是一种特殊的 mbean,它通过描述符来描述其管理接口,可以在运行时动态地配置其属性和操作。模型 mbean 可以通过 xml 或者通过程序动态地加载描述符。
mxbean:mxbean 是一种专门用于 jmx 的 mbean 类型。它定义了一种简单的 java 接口和实现约定,用于描述 mbean 的属性和操作,同时这些接口和实现类遵循 java bean 的命名约定。mxbean 在 jmx 中用于管理特定类型的对象,例如内存使用情况、线程信息等。
open mbean:开放 mbean 是一种通用的 mbean 类型,它可以包含各种类型的属性和操作。开放 mbean 不限制其属性和操作的类型,因此可以包含任意类型的数据。
2.1.standard mbean
所谓的standard mbean,就是说按照jmx的命名规范来定义接口然后注册到mbean server中,mbean就能被jmx的client端访问到。具体该mbean提供了什么功能,在这个自定义的mbean中去定义属性和方法仔细实现即可。
代码示例:
// 定义一个接口,命名约定为 yourinterfacenamembean
public interface hellombean {
string getname();
void setname(string name);
void sayhello();
}
// 实现上述接口的类
public class hello implements hellombean {
private string name;
@override
public string getname() {
return name;
}
@override
public void setname(string name) {
this.name = name;
}
@override
public void sayhello() {
system.out.println("hello, " + name + "!");
}
}
// 主程序
public class main {
public static void main(string[] args) throws exception {
// 创建标准 mbean 实例
hellombean hellombean = new hello();
// 创建 mbeanserver
mbeanserver mbs = managementfactory.getplatformmbeanserver();
// 创建 objectname,用于标识 mbean
objectname name = new objectname("com.example:type=hello");
// 注册 mbean 到 mbeanserver
mbs.registermbean(hellombean, name);
// 等待一段时间,使得可以通过 jconsole 或者其他 jmx 客户端来操作 mbean
thread.sleep(long.max_value);
}
}2.2.dynamic mbean
动态 mbean 是 java management extensions (jmx) 中的一种特殊类型的 mbean,它与标准 mbean 不同之处在于,动态 mbean 允许在运行时动态地确定其属性和操作,这使得动态 mbean 能够在运行时根据需要动态地调,而不是在编译时就确定了。
说人话就是很方便动态的去修改dynamic mbean的属性,这非常适合拿来做一些动态的功能,比如:
动态配置管理,从外部去加载配置文件,实现配置的动态变化。
动态监控,从外部去获取监控信息,比如实时的cpu、内存的信息,实现对系统的动态监控。
动态 mbean 的核心是实现 dynamicmbean 接口,该接口定义了一组方法,用于动态地获取和设置 mbean 的属性,执行 mbean 的操作,以及获取 mbean 的元数据信息。以下是 dynamicmbean 接口的几个核心方法:
getattribute(string attribute):根据属性名获取属性值。setattribute(attribute attribute):设置指定属性的值。getattributes(string[] attributes):批量获取属性值。setattributes(attributelist attributes):批量设置属性值。invoke(string actionname, object[] params, string[] signature):执行指定的操作。getmbeaninfo():获取 mbean 的元数据信息,包括属性、操作和描述信息。
代码示例:
import javax.management.*;
import java.lang.management.managementfactory;
public class dynamichello implements dynamicmbean {
private string name;
@override
public object getattribute(string attribute) throws attributenotfoundexception, mbeanexception, reflectionexception {
if (attribute.equals("name")) {
return name;
} else {
throw new attributenotfoundexception("attribute '" + attribute + "' not found");
}
}
@override
public void setattribute(attribute attribute) throws attributenotfoundexception, invalidattributevalueexception, mbeanexception, reflectionexception {
if (attribute.getname().equals("name")) {
name = (string) attribute.getvalue();
} else {
throw new attributenotfoundexception("attribute '" + attribute.getname() + "' not found");
}
}
@override
public attributelist getattributes(string[] attributes) {
attributelist attrs = new attributelist();
for (string attr : attributes) {
try {
object value = getattribute(attr);
attrs.add(new attribute(attr, value));
} catch (exception e) {
// ignore if attribute not found
}
}
return attrs;
}
@override
public attributelist setattributes(attributelist attributes) {
attributelist responseattrs = new attributelist();
for (attribute attr : attributes.aslist()) {
try {
setattribute(attr);
responseattrs.add(new attribute(attr.getname(), getattribute(attr.getname())));
} catch (exception e) {
// ignore if attribute not found or set failed
}
}
return responseattrs;
}
@override
public object invoke(string actionname, object[] params, string[] signature) throws mbeanexception, reflectionexception {
if (actionname.equals("sayhello")) {
return "hello, " + name + "!";
} else {
throw new unsupportedoperationexception("operation '" + actionname + "' not supported");
}
}
@override
public mbeaninfo getmbeaninfo() {
mbeanattributeinfo[] attributes = new mbeanattributeinfo[]{
new mbeanattributeinfo("name", "java.lang.string", "name of the person", true, true, false)
};
mbeanoperationinfo[] operations = new mbeanoperationinfo[]{
new mbeanoperationinfo("sayhello", "say hello to the person", null, "java.lang.string", mbeanoperationinfo.action)
};
return new mbeaninfo(this.getclass().getname(), "dynamic hello mbean", attributes, null, operations, null);
}
public static void main(string[] args) throws exception {
dynamichello dynamichello = new dynamichello();
mbeanserver mbs = managementfactory.getplatformmbeanserver();
objectname name = new objectname("com.example:type=dynamichello");
mbs.registermbean(dynamichello, name);
thread.sleep(long.max_value);
}
}在这个示例中,我们实现了 dynamicmbean 接口,并重写了它的方法来提供 mbean 的属性和操作。动态 mbean 允许在运行时确定其属性和操作,因此我们需要在实现中动态地处理属性的获取、设置和操作的调用。
在 getmbeaninfo() 方法中,我们定义了 mbean 的元数据信息,包括属性、操作和描述信息。在 main() 方法中,我们创建了 dynamichello 的实例,并将其注册到 mbeanserver 中。然后通过 thread.sleep(long.max_value) 使程序保持运行,以便通过 jconsole 或其他 jmx 客户端来操作该动态 mbean。
2.3.model bean
model mbean 的核心是实现 modelmbean 接口,该接口继承自 dynamicmbean 接口,因此具有动态 mbean 的所有特性,同时还添加了一些额外的方法用于管理描述符。以下是 modelmbean 接口的一些重要方法:
setmodelmbeaninfo(modelmbeaninfo mbi):设置 model mbean 的元数据信息,包括属性、操作和描述符。setmanagedresource(object mr, string mr_type):指定 model mbean 管理的资源对象及其类型。sendnotification(notification ntfyobj):发送通知给注册的监听器。setattribute(attribute attribute)和getattribute(string attribute):获取和设置属性,这些属性可以通过描述符来配置。
代码示例:
import javax.management.*;
import javax.management.modelmbean.*;
import java.lang.management.managementfactory;
public class simplemodelmbeanexample {
public static void main(string[] args) throws exception {
// 创建 modelmbeaninfo 对象
modelmbeaninfo modelmbeaninfo = createmodelmbeaninfo();
// 创建 modelmbean 实例
modelmbean modelmbean = createmodelmbean(modelmbeaninfo);
// 创建 mbeanserver
mbeanserver mbs = managementfactory.getplatformmbeanserver();
// 创建 objectname,用于标识 mbean
objectname name = new objectname("com.example:type=simplemodelmbean");
// 注册 modelmbean 到 mbeanserver
mbs.registermbean(modelmbean, name);
// 等待一段时间,使得可以通过 jconsole 或者其他 jmx 客户端来操作 mbean
thread.sleep(long.max_value);
}
private static modelmbeaninfo createmodelmbeaninfo() throws exception {
// 创建属性描述符
descriptor namedesc = new descriptorsupport();
namedesc.setfield("name", "name");
namedesc.setfield("descriptortype", "attribute");
// 创建操作描述符
descriptor sayhellodesc = new descriptorsupport();
sayhellodesc.setfield("name", "sayhello");
sayhellodesc.setfield("descriptortype", "operation");
// 创建属性列表
mbeanattributeinfo[] attributes = new mbeanattributeinfo[]{
new mbeanattributeinfo("name", "java.lang.string", "name of the person", true, true, false, namedesc)
};
// 创建操作列表
mbeanoperationinfo[] operations = new mbeanoperationinfo[]{
new mbeanoperationinfo("sayhello", "say hello to the person", null, "java.lang.string", mbeanoperationinfo.action, sayhellodesc)
};
// 创建 modelmbeaninfo
modelmbeaninfo modelmbeaninfo = new modelmbeaninfosupport(
simplemodelmbeanexample.class.getname(),
"simple model mbean example",
attributes,
null,
operations,
null
);
return modelmbeaninfo;
}
private static modelmbean createmodelmbean(modelmbeaninfo modelmbeaninfo) throws exception {
// 创建 modelmbean 实例
requiredmodelmbean modelmbean = new requiredmodelmbean();
modelmbean.setmodelmbeaninfo(modelmbeaninfo);
// 创建 mbean 实例
objectname name = new objectname("com.example:type=simplemodelmbean");
simplemodelmbean example = new simplemodelmbean();
// 设置 mbean 实例
modelmbean.setmanagedresource(example, "objectreference");
return modelmbean;
}
}在这个示例中,我们首先创建了一个 modelmbeaninfo 对象,该对象描述了 model mbean 的元数据信息,包括属性和操作。然后,我们创建了一个 modelmbean 实例,并将 modelmbeaninfo 设置到该实例中。接着,我们创建了一个 objectname 对象,用于标识 mbean,然后将 modelmbean 实例注册到了 mbeanserver 中。
同时,我们还定义了一个 simplemodelmbean 类,该类实现了 model mbean 管理的资源对象。在这个类中,我们可以定义属性和操作的具体实现逻辑。
2.4.dynamic mbean和model bean的区别
其实model bean本来就是继承自dynamic mbean的,所以后者有的特点,前者也有,其二者本质的区别在于使用场景上:
动态性质:
dynamic mbean:dynamic mbean 的设计理念是允许在运行时动态地确定其属性和操作。它适用于那些在编译时无法确定其管理接口的场景,例如,那些需要根据外部配置文件或运行时状态来确定其管理接口的应用程序。
model mbean:model mbean 的设计理念是通过描述符来描述其管理接口,使得可以在运行时动态地加载和配置 mbean 的属性和操作。它适用于需要动态加载和配置管理接口的场景,例如,根据不同的环境或条件来动态地调整管理接口。
自描述性:
dynamic mbean:dynamic mbean 不提供自描述性,开发人员需要手动实现属性和操作的获取和设置逻辑。它主要关注于动态性和灵活性,适用于那些需要在运行时动态确定管理接口的场景。
model mbean:model mbean 提供了详细的元数据信息,包括属性和操作的描述信息,使得管理客户端可以动态地了解 mbean 的结构和功能。它适用于那些需要自描述性和灵活性的场景,例如,需要通过管理客户端来动态地了解和操作 mbean 的情况。
适应性:
dynamic mbean:dynamic mbean 具有较高的适应性,可以根据外部条件或运行时状态动态地调整其管理接口。它适用于那些需要根据外部条件或运行时状态来动态调整管理接口的场景。
model mbean:model mbean 具有较高的适应性,可以根据不同的环境或条件动态地调整管理接口。它适用于那些需要根据不同的部署环境或运行时条件来动态地配置 mbean 的属性和操作的场景。
2.5.mxbean
在 java 中,一切都被视为对象,而 jmx 则提供了一种标准化的方式来监控和管理 java 程序的运行时状态、性能指标以及运行环境。而mxbean其实就是专门用于管理和监控这其中一些标准化类型的对象的。什么叫标准化的类型对象?其实就是一些定量的指标,例如内存使用情况、线程信息、操作系统属性等。这些mbean是拿来即用的。
java 平台中有一些预定义的 mxbean,例如 memorymxbean、threadmxbean、operatingsystemmxbean 等,用于管理和监控 jvm 的内存、线程、操作系统属性等。
代码示例:
import java.lang.management.managementfactory;
import java.lang.management.memorymxbean;
import java.lang.management.memoryusage;
public class mxbeanexample {
public static void main(string[] args) {
// 获取 memorymxbean 实例
memorymxbean memorymxbean = managementfactory.getmemorymxbean();
// 获取堆内存使用情况
memoryusage heapmemoryusage = memorymxbean.getheapmemoryusage();
system.out.println("heap memory usage:");
system.out.println(" init: " + heapmemoryusage.getinit() / (1024 * 1024) + " mb");
system.out.println(" used: " + heapmemoryusage.getused() / (1024 * 1024) + " mb");
system.out.println(" committed: " + heapmemoryusage.getcommitted() / (1024 * 1024) + " mb");
system.out.println(" max: " + heapmemoryusage.getmax() / (1024 * 1024) + " mb");
// 获取非堆内存使用情况
memoryusage nonheapmemoryusage = memorymxbean.getnonheapmemoryusage();
system.out.println("non-heap memory usage:");
system.out.println(" init: " + nonheapmemoryusage.getinit() / (1024 * 1024) + " mb");
system.out.println(" used: " + nonheapmemoryusage.getused() / (1024 * 1024) + " mb");
system.out.println(" committed: " + nonheapmemoryusage.getcommitted() / (1024 * 1024) + " mb");
system.out.println(" max: " + nonheapmemoryusage.getmax() / (1024 * 1024) + " mb");
}
}可以自定义mxbean吗?当然可以。
自定义 mxbean 主要包括两个步骤:
定义接口:首先,需要定义一个 java 接口来描述 mxbean 的管理接口。这个接口中定义了一组属性和操作,用于描述被管理的对象的状态和行为。
实现类:然后,需要编写一个实现类来实现定义的接口。在实现类中,需要提供属性和操作的具体实现逻辑,以及可能需要的管理逻辑。
代码示例:
// 定义 mxbean 接口
public interface mymxbean {
// 属性:名称
string getname();
void setname(string name);
// 操作:打印消息
void printmessage(string message);
}
// 实现类
public class mymxbeanimpl implements mymxbean {
private string name;
// 实现属性的 getter 和 setter 方法
@override
public string getname() {
return name;
}
@override
public void setname(string name) {
this.name = name;
}
// 实现操作的方法
@override
public void printmessage(string message) {
system.out.println(message);
}
}
// 在管理应用程序中注册和使用自定义的 mxbean
public class mymxbeanregistration {
public static void main(string[] args) throws exception {
// 创建 mxbean 实例
mymxbean mxbean = new mymxbeanimpl();
// 注册 mxbean 到 mbeanserver
mbeanserver mbs = managementfactory.getplatformmbeanserver();
objectname name = new objectname("com.example:type=mymxbean");
mbs.registermbean(mxbean, name);
// 等待一段时间,使得可以通过 jconsole 或者其他 jmx 客户端来操作 mxbean
thread.sleep(long.max_value);
}
}在这个示例中,我们定义了一个名为 mymxbean 的接口,包含了一个属性 name 和一个操作 printmessage。然后,我们编写了一个实现类 mymxbeanimpl,实现了这个接口,并提供了属性和操作的具体实现逻辑。最后,我们在管理应用程序中注册和使用了这个自定义的 mxbean。
2.6.open bean
与传统的 standard mbean 和 dynamic mbean 不同,open mbean 不需要预先定义接口,而是使用开放式的数据类型和操作来描述 mbean 的管理接口,使得可以更灵活地适应各种场景和需求。
代码示例:
import javax.management.*;
import javax.management.openmbean.*;
public class openmbeanexample {
public static void main(string[] args) throws exception {
// 创建 opentype
opentype<string> stringtype = simpletype.string;
opentype<integer> integertype = simpletype.integer;
// 创建 openmbeanattributeinfo
openmbeanattributeinfosupport nameattrinfo = new openmbeanattributeinfosupport(
"name",
"name of the person",
stringtype,
true,
false,
false
);
openmbeanattributeinfosupport ageattrinfo = new openmbeanattributeinfosupport(
"age",
"age of the person",
integertype,
true,
false,
false
);
// 创建 openmbeanoperationinfo
openmbeanparameterinfo[] params = new openmbeanparameterinfo[]{};
openmbeanoperationinfosupport sayhelloopinfo = new openmbeanoperationinfosupport(
"sayhello",
"say hello to the person",
params,
stringtype,
openmbeanoperationinfo.action
);
// 创建 openmbeaninfo
openmbeaninfosupport mbeaninfo = new openmbeaninfosupport(
simpleopenmbean.class.getname(),
"simple open mbean example",
new openmbeanattributeinfo[]{nameattrinfo, ageattrinfo},
null,
new openmbeanoperationinfo[]{sayhelloopinfo},
null
);
// 创建 openmbean 实例
simpleopenmbean mbean = new simpleopenmbean("john", 30);
// 创建 mbeanserver
mbeanserver mbs = managementfactory.getplatformmbeanserver();
// 创建 objectname,用于标识 mbean
objectname name = new objectname("com.example:type=simpleopenmbean");
// 注册 openmbean 到 mbeanserver
mbs.registermbean(mbean, name);
// 等待一段时间,使得可以通过 jconsole 或者其他 jmx 客户端来操作 mbean
thread.sleep(long.max_value);
}
}
// 实现 openmbean
public class simpleopenmbean implements openmbean {
private string name;
private int age;
public simpleopenmbean(string name, int age) {
this.name = name;
this.age = age;
}
@override
public object getattribute(string attribute) throws attributenotfoundexception, mbeanexception, reflectionexception {
if (attribute.equals("name")) {
return name;
} else if (attribute.equals("age")) {
return age;
} else {
throw new attributenotfoundexception("attribute " + attribute + " not found");
}
}
@override
public void setattribute(attribute attribute) throws attributenotfoundexception, invalidattributevalueexception, mbeanexception, reflectionexception {
throw new unsupportedoperationexception("setattribute not supported");
}
@override
public object invoke(string actionname, object[] params, string[] signature) throws mbeanexception, reflectionexception {
if (actionname.equals("sayhello")) {
return "hello, " + name + "!";
} else {
throw new unsupportedoperationexception("operation " + actionname + " not supported");
}
}
@override
public mbeaninfo getmbeaninfo() {
// 实现 openmbean 接口的 getmbeaninfo 方法,返回 mbean 的元数据信息
return null;
}
}3.控制台
jdk自带了一个jmx的控制台jconsole,启动后选择进程,就能看到相应程序中的mbean。

到此这篇关于java监控jmx的使用的文章就介绍到这了,更多相关java监控jmx内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论