JmxTest
2024-09-06 02:16:35
package mbeanTest; import java.util.Set; import javax.management.Attribute;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.modelmbean.RequiredModelMBean; public class JmxTest
{
/**
* 测试标准MBean 需要被管理的方法、属性等在接口中定义好,创建一个类,继承此接口,然后实现时候方法,
* 这样,但注册到MBeanServer的时候,会自动管理其,接口中的各个属性、方法。
*
* @throws Exception
*/ public void test1StandardMBean() throws Exception
{
// MBeanServer ms = MBeanServerFactory.createMBeanServer("JMX2Test");
MBeanServer ms = MBeanServerFactory.createMBeanServer();
ObjectName name = new ObjectName("Hellogg:type=myfirstMbean"); // ms.createMBean("HelloWorld", objectName);
HelloWorld hello = new HelloWorld(" yao yao , qie ke nao "); // MBean需要实现NotificationBroadcaster接口,支持各种事件的发送和处理
hello.addNotificationListener(new NotificationListener()
{ public void handleNotification(Notification notification,
Object handback)
{
System.out.println(" access listen : " + notification);
}
}, null, null); ms.registerMBean(hello, name); String s1 = (String) ms.getAttribute(name, "Hello");
System.out.println(" the init value1 : " + s1); ms.setAttribute(name, new Attribute("Hello", " hi ,hi ,man "));
String s2 = (String) ms.getAttribute(name, "Hello");
System.out.println(" the init value2 : " + s2); ms.invoke(name, "message", new Object[]{ " i as message " }, new String[]{ "java.lang.String" }); ObjectName name2 = new ObjectName("Hellogg:type=myfirstMbean");
Set<ObjectInstance> set = ms.queryMBeans(name2, null);
System.out.println("set: "+set);
} /**
* 动态Mbean,需要实现DynamicMBean接口,并且任何需要,管理的方法、属性,都需要在接口的方法中,
* 自己来实现,Mbeaninfo也需要自己设置,这样编程的工作量大,但是有很大的可控性。
*
* @throws Exception
*/ public void test2DynamicMBean() throws Exception
{
HelloWorldDynamic dynamic = new HelloWorldDynamic(); MBeanServer ms = MBeanServerFactory.createMBeanServer();
// 创建一个ObjectName
ObjectName name = new ObjectName("DynamicHello:type=dinamicMbean"); // 注册动态MBean到MBeanServer服务上去
ms.registerMBean(dynamic, name); // 得到属性值
Object o = ms.getAttribute(name, "getInstance");
String hello = (String) ms.getAttribute(name, "gh");
MBeanOperationInfo operation = dynamic.getMBeanInfo().getOperations()[0];
System.out.println(" attribute value of getInstance:" + o
+ "; attribute value of gh:" + hello); // 执行一个方法(操作)
ms.invoke(name, operation.getName(), null, null);
} public void test3RequiredModelMBean() throws Exception
{
HelloWorldModelMBean hello = new HelloWorldModelMBean(); MBeanServer ms = MBeanServerFactory.createMBeanServer();
RequiredModelMBean modelMbean = hello.createModelBean();
ObjectName name = new ObjectName("RequiredMBeanHello:type=ModelMbean");
// 监听属性变化事件
modelMbean.addAttributeChangeNotificationListener(
new NotificationListener()
{
@Override
public void handleNotification(Notification notification,
Object handback)
{
System.out.println(" --Attribute已经改变-- ");
}
}, null, null);
ms.registerMBean(modelMbean, name); //ms.invoke(name, "setHello", new Object[]{ " 调用setHello方法 " }, new String[]{ "java.lang.String" });
String s = (String) ms.getAttribute(name, "hello");
// 出发Attribute改变事件
// ms.setAttribute(name, new Attribute("hello", "bbb"));
String s2 = (String) ms.getAttribute(name, "hello"); System.out.println(s);
System.out.println(s2);
} public static void main(String args[]) throws Exception
{ JmxTest jmx = new JmxTest();
jmx.test3RequiredModelMBean(); } }
最新文章
- Asp.net使用代码修改配置文件的节点值
- [SVN] SVN在Eclipse里的各个状态解释
- 解决IIS Express 80端口被占用的情况
- servlet运行流程
- Weak Pair---hud5877大连网选(线段树优化+dfs)
- js生成动态日历
- apache 三种工作模式的讲解
- AngularJS学习笔记3
- 关于mpu6050的几个很好的帖子
- (5)微信二次开发 之 XML格式数据解析
- Python Counter class
- 【Qt编程】基于Qt的词典开发系列<;十四>;自动补全功能
- ionic4+angular6 混合移动开发 capacitor cordova
- redis 配置命令
- Excel设置excel打印每页都有表头标题
- matplotlib 28原则
- Python语言规范
- tyvj 创世纪 - 基环树
- ElasticSearch 2 (28) - 信息聚合系列之高层概念
- luogu2296 [NOIp2014]寻找道路 (bfs)
热门文章
- CentOS 7 tmpwatch 2.11 版本变更,移除 cronjob 任务
- Socket `accept queue is full ` 但是一个连接需要从SYN->ACCEPT
- Jackson &; fastJson的使用
- Python知识整理(一)
- Effective C++ 总结笔记(二)
- C# 复制目录及其下文件
- 【Microsoft Azure 的1024种玩法】三.基于Azure云平台构建Discuz论坛
- [ccBB]Billboards
- SpringCloud微服务实战——搭建企业级开发框架(二十二):基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能
- nginx反向代理出错:proxy_pass