今天给大家分享一个JMX入门的案例,JMX是Java平台上为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。直接上代码。
HelloMBean.java
package com.xz.helloworld.jmx;
public interface HelloMBean {
public String getName();
public void setName(String name);
void printString(String str);
}
Hello.java
package com.xz.helloworld.jmx;
public class Hello implements HelloMBean{
private String name;
@Override
public String getName() {
// TODO Auto-generated method stub
System.out.println("set name : " + name);
return name;
}
@Override
public void setName(String name) {
System.out.println("set name : " + name);
this.name = name;
}
@Override
public void printString(String str) {
System.out.println("printString "+str);
}
}
TestMain.java
package com.xz.helloworld.jmx;
import java.rmi.registry.LocateRegistry;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class TestMain {
public static void main(String[] args) throws Exception {
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName helloName = new ObjectName("xxxx:name=HelloWorld");
server.registerMBean(new Hello(), helloName);
LocateRegistry.createRegistry(8088);
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:8088/jmxrmi");
JMXConnectorServer jsc = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
jsc.start();
Thread.sleep(100000);
}
}
这个时候我们启动项目,通过jconsole去链接,如图:
这里用来模拟远程的进程链接,不使用本地的进程,如果服务部署服务器上的时候就必须通过远程来链接了。
接下来我们给Name属性设置一个值:
写好之后点击这里的刷新,我们可以在控制台看到:
可以看到已经成功调用到方法了。下面我们来看看Java客户端。
public static void clientInit() {
try {
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8088/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
JMXConnector connector = JMXConnectorFactory.connect(serviceUrl);
MBeanServerConnection connection = connector.getMBeanServerConnection();
Set<ObjectName> objectNames = connection.queryNames(null, null);
for(ObjectName objectName:objectNames) {
System.out.println("========" + objectName + "========");
MBeanInfo mBeanInfo = connection.getMBeanInfo(objectName);
System.out.println("[Attributes]");
for (MBeanAttributeInfo attr : mBeanInfo.getAttributes()) {
Object value = null;
try {
value = attr.isReadable() ? connection.getAttribute(objectName, attr.getName()) : "";
} catch (Exception e) {
value = e.getMessage();
}
System.out.println(attr.getName() + ":" + value);
}
System.out.println("[Operations]");
for (MBeanOperationInfo oper : mBeanInfo.getOperations()) {
System.out.println(oper.getName() + ":" + oper.getDescription());
}
System.out.println("[Notifications]");
for (MBeanNotificationInfo notice : mBeanInfo.getNotifications()) {
System.out.println(notice.getName() + ":" + notice.getDescription());
}
}
ObjectName objectName = new ObjectName("xxxx:name=HelloWorld");
// connection.addNotificationListener(objectName, new NotificationListener() {
//
// @Override
// public void handleNotification(Notification notification, Object handback) {
// System.out.println("\nReceived notification:");
// System.out.println("\tClassName: " + notification.getClass().getName());
// System.out.println("\tSource: " + notification.getSource());
// System.out.println("\tType: " + notification.getType());
// System.out.println("\tMessage: " + notification.getMessage());
// if (notification instanceof AttributeChangeNotification) {
// AttributeChangeNotification acn =
// (AttributeChangeNotification) notification;
// System.out.println("\tAttributeName: " + acn.getAttributeName());
// System.out.println("\tAttributeType: " + acn.getAttributeType());
// System.out.println("\tNewValue: " + acn.getNewValue());
// System.out.println("\tOldValue: " + acn.getOldValue());
// }
// }
// }, null, null);
//给MBean赋值
connection.setAttribute(objectName, new Attribute("Name", "我是张三"));
//获取MBean的值
Object Name = connection.getAttribute(objectName, "Name");
System.out.println(Name);
//调用MBean的方法
connection.invoke(objectName, "printString", new Object[] {"我是Object"}, new String[] {String.class.getCanonicalName()});
System.out.println("---"+String.class.getCanonicalName());
} catch (Exception e) {
e.printStackTrace();
}
}
在上面的main方法中通过 开启一个线程去调用,即可看到效果。
========JMImplementation:type=MBeanServerDelegate========
[Attributes]
MBeanServerId:DESKTOP-LPKSION_1576138303413
SpecificationName:Java Management Extensions
SpecificationVersion:1.4
SpecificationVendor:Oracle Corporation
ImplementationName:JMX
ImplementationVersion:1.8.0_221-b11
ImplementationVendor:Oracle Corporation
[Operations]
[Notifications]
javax.management.MBeanServerNotification:Notifications sent by the MBeanServerDelegate MBean
========xxxx:name=HelloWorld========
[Attributes]
set name : null
Name:null
[Operations]
printString:Operation exposed for management
[Notifications]
set name : 我是张三
set name : 我是张三
我是张三
printString 我是Object
---java.lang.String
当然JMX里边还有很多比较强大的东西,也有很多玩法,这篇文章就介绍到此,有建议的朋友可以在评论区留言,技术问题可以私信我。