HelloWorldModelMBean
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的更多相关文章
- JmxTest
package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...
随机推荐
- selenium2.x 与 selenium3.x 最大区别
一.selenium2.x 与 selenium3.x 最大区别 (1) 从3.0版本selenium开始使用火狐浏览器完成web自动化就需要用到驱动包了. (2) 而2.0版本的selenium使用 ...
- java 三大特性_继承、封装、多态_day005
一.继承: java的三大特性之一.两个类之间通过extends关键字来描述父子关系,子类便可拥有父类的公共方法和公共属性.子类可以继承父类的方法和属性,子类也可以自己定义没有的方法或者通过覆盖父类的 ...
- 学习JS的第一天--初识JS
1.初识JS a.我的第一个JS程序: document.write("Hello JS")://这段代码是输出到body中就是直接打开就可以看到: console.log(&qu ...
- python3.7 pip
Is pip the same for 3.4+ No, it's not. A single pip installation serves a single Python distribution ...
- tabulate
ValueError: headers for a list of dicts is not a dict or a keyword from: https://bitbucket.org/astan ...
- C++概述及知识点总结
经过一段时间的学习,以前从没有接触过C++这个高逼格的语言的小白,逐渐对C++有了更深的了解和认识,C++是c语言的升级版,Bjarne Stroustrup在剑桥大学计算机中心工作.他使用过Simu ...
- 菜鸡的Java笔记 第三十一 扩展结构
.新特性 可变参数的意义以及实现 那么下面通过一个简单的思考来分析可变参数的存在意义 范例:如果说现在要定义一个方法,这个方法可以实现任意多个 ...
- Java设计模式之(三)——建造者模式
1.什么是建造者模式 Separate the construction of a complex object from its representation so that the same co ...
- [loj3504]支配
令$S_{x}$表示$x$支配的节点集合,可以暴力枚举$x$并求出$S_{x}$(删去$x$后从1开始dfs,复杂度为$o(nm)$),进而反过来即可求出受支配集$D_{x}$ 结论1:若$z\in ...
- 论文翻译:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domain
提出了模型和损失函数 论文名称:扩展卷积密集连接神经网络用于时域实时语音增强 论文代码:https://github.com/ashutosh620/DDAEC 引用:Pandey A, Wang D ...