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. SpringMVC注解知识点

    SpringMVC注解知识点 SpringMVC原生知识点: 上一篇: https://www.cnblogs.com/yiur-bgy/p/14088883.html 注解版 1.新建一个Moudl ...

  2. fork()和vfork()的区别,signal函数用法,exec()系列函数的用法小结

    一:fork()和vfork()的区别:    fork()函数可以创建子进程,有两个返回值,即调用一次返回两个值,一个是父进程调用fork()后的返回值,该返回值是刚刚创建的子进程的ID;另一个是子 ...

  3. 官宣 .NET RC 2

    我们很高兴发布 .NET 6 RC(Release Candidate) 2.它是生产环境中支持的两个"go live"候选版本中的第二个. 在过去的几个月里,团队一直专注于质量的 ...

  4. win10开机自启

    所有用户的开机自启文件夹 英文路径 # ProgramData目录是隐藏的 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 中 ...

  5. <C#任务导引教程>练习八

    //66,静态成员与非静态成员的访问方式using System;class Text{    public int x;    public static int y;//静态数据成员    voi ...

  6. [bzoj1079]着色方案

    由于最终的染色只与ci为几的个数有关,因此定义状态f[a][b][c][d][e][p]表示有a个ci=1,b个ci=2,--,有e个ci=5,上一次选择了ci=p的.状态的转移:发现p会让p-1少选 ...

  7. [nowcoder5668J]Operating on the Tree

    考虑令$a_{i}$为i的位置,$p_{i}=0/1$表示第i个点的贡献,那么$p_{x}=0$当且仅当存在与其相邻的点$y$满足$a_{y}<a_{x}$且$p_{y}=1$ 树形dp,定义状 ...

  8. redis可以设置过期key回调实现延时队列

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  9. CSS动画--让div动起来

    CSS动画 今天在写代码时候,遇到了css动画效果如何实现的问题,经过查阅和实践,总结出一下结论. transition transition 指定动画变化的对应属性 以及动画的执行时间. 例如:tr ...

  10. Kubernetes容器编排探索与实践v1.22.1-上半部分

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com Kubernetes官网地址 https://kubernetes.io Kubernetes GitHub源码地址 htt ...