JMX:(Java Management Extensions,即Java管理扩展)是Java平台上为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。
1.Agent:管理MBeanServer提供远程链接rmi
2.MBeanServer:管理MBean,注册MBean,查找MBean
3.MBea:java定义java对象
JMX demo
jmxBean.java
public class jmxBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
jmxTest
public class jmxTest {
public static void main(String[] args) throws Exception {
//注册mbean
jmxBean mbean = new jmxBean();
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("com.wxw.jmx:type=jmxBean");
mbs.registerMBean(mbean, name);
Echo e = new Echo();
// 获取注册的Mbean
MBeanServer mbss = ManagementFactory.getPlatformMBeanServer();
ObjectName namee = new ObjectName("com.wxw.jmx:type=jmxBean");
mbss.invoke(namee, "setName", new Object[] { "wwwwwwxwx" }, new String[] { "java.lang.string" });
System.out.println(" ................... " + e);
while (true) {
}
}
}
JMX+Spring
jmxBean.java
public class jmxBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
jmxTest.java
<pre name="code" class="html">public class jmxTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("bean:name=jmxBean");
mbs.invoke(name, "setMessage", new Object[] { "invoke" }, new String[] { "java.lang.String" });
mbs.invoke(name, "setName", new Object[] { "wxwinvoke" }, new String[] { "java.lang.String" });
}
}
<pre name="code" class="html">applicationContext.xml
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 暴露监控设备 -->
<!-- <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/> -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=jmxBean" value-ref="jmxBean" />
</map>
</property>
<!-- <property name="server" ref="mbeanServer"/> -->
</bean>
<bean id="jmxBean" class="com.spring.jmx.jmxBean" />
</beans>