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. LOTO实践【干货】电压比较器的快速应用

    LOTO实践[干货]电压比较器的快速应用 话不多说先上图: 最近出差去客户那边做设备调试,现场如上图,其中我负责的技术部分包含了一个掉落物监测的功能.硬件上的原理比较简单,发射板上的红外发射头阵列,以 ...

  2. Java学习(二十二)

    学了一个在css中叫font的样式: 感觉还是挺好用的 不过要注意如果把font放在最后,其他会使用默认值,可能会覆盖掉前面的 例如新学的行高 在font中语法是 font:30px/40px &qu ...

  3. 我罗斯方块第二次作业(Player类)

    我罗斯方块第二次作业 我的任务 完成player类的编写 player类的测试 我的计划 类的设计: Player类作为一个玩家类,需要处理和玩家有关的所有信息,以及维护玩家的游戏页面map.关于玩家 ...

  4. Python基础(数据类型与变量、字符串和编码)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # name = 200 # if name > 100: # print(name,'大于100' ...

  5. GitHub出现Permission denied (publickey)

    Permission denied (publickey) 没有权限的publickey 重新生成一次ssh key即可解决 ssh-keygen -t rsa -C "这里输入你的邮箱&q ...

  6. Python 爬取 书籍

    ... import requests from bs4 import BeautifulSoup def gethtml(url,h): r = requests.get(url,headers=h ...

  7. 使用pmml跨平台部署机器学习模型Demo——房价预测

      基于房价数据,在python中训练得到一个线性回归的模型,在JavaWeb中加载模型完成房价预测的功能. 一. 训练.保存模型 工具:PyCharm-2017.Python-39.sklearn2 ...

  8. [hdu7044]Fall with Fake Problem

    二分$T$​​​​​​​​和$S$​​​​​​​​第一个不同的位置,即需要对于$s$​​​​​​​​,判定是否存在$T[1,s]=S[1,s]$​​​​​​​​且满足条件的$T$​​​​ (注:这里的 ...

  9. [at4631]Jewels

    如果要选某颜色,必然会选该颜色最大的两个,那么不妨将这两个宝石权值修改为两者的平均数,显然不影响两者的和,也即不影响答案 接下来,将所有宝石按权值从大到小排序,并在权值相同时按颜色编号从小到大(使颜色 ...

  10. [atAGC022D]Shopping

    称0到$L$的方向为左,同时为了方便,可以假设$0<t_{i}\le 2L$ 当我们确定是进入店中的方向,根据这个店的位置以及购物时间,不难确定出来时火车经过0或$L$的次数,由于$0<t ...