package mbeanTest;

import java.lang.reflect.Constructor;

import javax.management.Descriptor;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.RuntimeOperationsException;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.InvalidTargetObjectTypeException;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanConstructorInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean; public class HelloWorldModelMBean extends RequiredModelMBean
{
public HelloWorldModelMBean() throws Exception
{} public static RequiredModelMBean createModelBean() throws RuntimeOperationsException, MBeanException,
InstanceNotFoundException, InvalidTargetObjectTypeException
{
// 模型MBean信息
ModelMBeanInfo info = buildModelMBeanInfo();
// 模型MBean
RequiredModelMBean modelMBean = new RequiredModelMBean(info);
// 目前只支持ObjectReference,将来可能会支持ObjectReference", "Handle", "IOR", "EJBHandle",
// or "RMIReference,
// RMIReference从名字上可以看出,如果支持的话,那么以后就可以支持远程MBean引用
modelMBean.setManagedResource(new HelloWorld(), "ObjectReference");
return modelMBean;
} protected static ModelMBeanInfo buildModelMBeanInfo() throws RuntimeOperationsException, MBeanException
{
// --
// attributes
// ------------------------------------------------------------------
ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[1]; // 设置属性
Descriptor nameDesc = new DescriptorSupport();
nameDesc.setField("name", "hello");
nameDesc.setField("value", "这是value的值");
nameDesc.setField("hee", "这是hee的值");
nameDesc.setField("displayName", "myname");
nameDesc.setField("setMethod", "setHello");
nameDesc.setField("getMethod", "getHello");
nameDesc.setField("descriptorType", "attribute");
attributes[0] = new ModelMBeanAttributeInfo("hello",
"java.lang.String", "name say hello to", true, true, false,
nameDesc);
String[] sa = nameDesc.getFields();
// --
// operations
// -------------------------------------------------------------------
ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[2];
String className = HelloWorld.class.getName(); // getName method
Descriptor getDesc = new DescriptorSupport(new String[]
{ "name=getHello", "descriptorType=operation", "class=" + className,
"role=operation" });
operations[0] = new ModelMBeanOperationInfo("getHello", "get hello ",
null, null, MBeanOperationInfo.ACTION, getDesc); Descriptor setDesc = new DescriptorSupport(new String[]
{ "name=setHello", "descriptorType=operation", "class=" + className,
"role=operation" });
operations[1] = new ModelMBeanOperationInfo("setHello", "set hello ",
new MBeanParameterInfo[]
{ new MBeanParameterInfo("a", "java.lang.String",
" a method's arg ") }, null, MBeanOperationInfo.ACTION,
setDesc); // constructors
ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[1];
Constructor<?>[] ctors = HelloWorld.class.getConstructors(); constructors[0] = new ModelMBeanConstructorInfo("default constructor",
ctors[0], null); // ModelMBeanInfo
ModelMBeanInfo mmbeanInfo = new ModelMBeanInfoSupport(className,
"Simple implementation of model bean.", attributes, null,
operations/* null */, null, null); // 设置一个Descriptor策略,这样RequiredModelMBean改变 Attribute值得时候会记录日志
// 当然RequiredModelMBean还需要addAttributeChangeNotificationListener,注册一个监听器
Descriptor globalDescriptor = new DescriptorSupport(new String[]
{ "name=HelloWorldModelMBean", "displayName=globaldescriptor",
"descriptorType=mbean", "log=T", "logfile=hello.log" });
mmbeanInfo.setMBeanDescriptor(globalDescriptor); return mmbeanInfo;
}
}

HelloWorldModelMBean的更多相关文章

  1. JmxTest

    package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...

随机推荐

  1. 盘点 GitHub 年度盛会|附视频

    「Universe 2021」是 GitHub 于今年举办的开发者盛会,本次 Universe 2021 大会采用线上直播模式,为期两天已于上周落下帷幕. 这是 GitHub 举办的一年一度开发者盛会 ...

  2. linux系列之: 你知道查看文件空间的两种方法吗?

    目录 简介 du命令 df命令 总结 简介 linux系统中查看文件空间大小应该是一个非常常见的命令了,今天给大家介绍linux系统中查看文件空间的两种方法和在使用中可能会遇到的奇怪问题. 为什么会有 ...

  3. 🔥完美解决ESlint+Prettier各项配置冲突的语法报错问题(新手向)

    本文重点: 1.解决修改了Prettier默认配置,项目内格式化无法生效 2.解决Prettier缺少配置,函数名和括号之间,自动添加空格 3.settings.json配置项分享 一个程序员,可能非 ...

  4. 2020 ICPC 沈阳站 I - Rise of Shadows 题解

    题面看这里 \(PS\):符号 \([\ \rm P\ ]\) 的意义是:当表达式 \(\rm P\) 为真则取值为 \(1\),为假则取值为 \(0\). 题目大意 给你一个一天有 \(H\)​​​ ...

  5. github上传和删除文件(三)

    上传文件: git init git add * git commit -m "description" //git remote rm origin 或查看当前 git remo ...

  6. 【Git 系列】一个超好用的命令你会用吗?

    stash在英文意思是隐藏.git stash 的作用也是隐藏没完成的代码,防止它干扰别人或者新分支的工作. 一.背景 1.1 我们经常会遇到这样的情况 正在 dev 分支开发新功能,做到一半时有人过 ...

  7. Beyond compare 4.2.3 激活和秘钥

    安装完 Beyond Compare 4.2.3 以后.打开输入密匙是不是会跳到官网去?不用慌,我们只需要删除你安装Beyond Compare 4目录下的 BCUnrar.dll 文件,然后再打开就 ...

  8. 如何实现异步 connect

    写过网络程序的同学,应该都知道 connect 函数,在 socket 开始读写操作之前,先要进行连接,也即 TCP 的三次握手 , 这个过程就是在 connect 函数中完成的, connect 函 ...

  9. JSON实现序列化dump和dumps方法,JSON实现反序列化loads和load方法

    通过文件操作,我们可以将字符串写入到一个本地文件.但是,如果是一个对象(例如列表.字典.元组等),就无 法直接写入到一个文件里,需要对这个对象进行序列化,然后才能写入到文件里. 设计一套协议,按照某种 ...

  10. Spring Boot核心注解

    (1)@SpringBootApplication 代表SpringBoot的启动类 (2)@SpringBootConfiguration 通过bean对象来获取配置信息 (3)@Configura ...