public class ServerImpl
{
public final long startTime;
public ServerImpl()
{
startTime = System.currentTimeMillis();
}
} public class ServerMonitor implements ServerMonitorMBean
{
private final ServerImpl target;
public ServerMonitor(ServerImpl target)
{
this.target = target;
}
public long getUpTime()
{
return System.currentTimeMillis() - target.startTime;
}
} import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
public class Main
{
private static ObjectName objectName ;
private static MBeanServer mBeanServer;
public static void main(String[] args) throws Exception
{
init();
manage();
}
private static void init() throws Exception
{
ServerImpl serverImpl = new ServerImpl();
ServerMonitor serverMonitor = new ServerMonitor(serverImpl);
mBeanServer = MBeanServerFactory.createMBeanServer();
objectName = new ObjectName("objectName:id=ServerMonitor1");
mBeanServer.registerMBean(serverMonitor,objectName);
}
private static void manage() throws Exception
{
Long upTime = (Long) mBeanServer.getAttribute(objectName,"upTime");
System.out.println(upTime);
}
}
DynamicMBean:动态代理构建
import javax.management.*;
import java.lang.reflect.*;
public class ServerMonitor implements DynamicMBean
{
private final ServerImpl target;
private MBeanInfo mBeanInfo;
public ServerMonitor(ServerImpl target)
{
this.target = target;
}
// 实现获取被管理的 ServerImpl 的 upTime
public long upTime()
{
return System.currentTimeMillis() - target.startTime;
}
//javax.management.MBeanServer 会通过查询 getAttribute("Uptime") 获得
"Uptime" 属性值
public Object getAttribute(String attribute) throws AttributeNotFoundException,
MBeanException, ReflectionException
{
if(attribute.equals("UpTime"))
{
return upTime();
} return null;
}
// 给出 ServerMonitor 的元信息。
public MBeanInfo getMBeanInfo()
{
if (mBeanInfo == null)
{
try
{
Class cls = this.getClass();
// 用反射获得 "upTime" 属性的读方法
Method readMethod = cls.getMethod("upTime", new Class[0]);
// 用反射获得构造方法
Constructor constructor = cls.getConstructor(new Class[]{ServerImpl.class});
// 关于 "upTime" 属性的元信息 : 名称为 UpTime,只读属性 ( 没有写方法 )。
MBeanAttributeInfo upTimeMBeanAttributeInfo = new MBeanAttributeInfo("UpTime", "The time span since server start",readMethod, null);
// 关于构造函数的元信息
MBeanConstructorInfo mBeanConstructorInfo = new MBeanConstructorInfo("Constructor for ServerMonitor", constructor);
//ServerMonitor 的元信息,为了简单起见,在这个例子里,
// 没有提供 invocation 以及 listener 方面的元信息
mBeanInfo = new MBeanInfo(cls.getName(),"Monitor that controls the server",
new MBeanAttributeInfo[] { upTimeMBeanAttributeInfo },
new MBeanConstructorInfo[] { mBeanConstructorInfo },
null, null);
}
catch (Exception e)
{
throw new Error(e);
}
} return mBeanInfo;
}
public AttributeList getAttributes(String[] arg0)
{
return null;
}
public Object invoke(String arg0, Object[] arg1, String[] arg2) throws MBeanException,ReflectionException
{
return null;
}
public void setAttribute(Attribute arg0) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
{
return;
}
public AttributeList setAttributes(AttributeList arg0)
{
return null;
}
}
Open MBean:开放式构建管理

ModelMBean:
public class Server
{
private long startTime;
public Server() { }
public int start()
{
startTime = System.currentTimeMillis();
return 0;
}
public long getUpTime()
{
return System.currentTimeMillis() - startTime;
}
}
import javax.management.*;
import javax.management.modelmbean.*;
public class Main
{
public static void main(String[] args) throws Exception
{
MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer();
RequiredModelMBean serverMBean = (RequiredModelMBean) mBeanServer.instantiate(
"javax.management.modelmbean.RequiredModelMBean");
ObjectName serverMBeanName = new ObjectName("server: id=Server");
serverMBean.setModelMBeanInfo(getModelMBeanInfoForServer(serverMBeanName));
Server server = new Server();
serverMBean.setManagedResource(server, "ObjectReference");
ObjectInstance registeredServerMBean = mBeanServer.registerMBean((Object) serverMBean, serverMBeanName);
serverMBean.invoke("start",null, null);
Thread.sleep(1000);
System.out.println(serverMBean.getAttribute("upTime"));
Thread.sleep(5000);
System.out.println(serverMBean.getAttribute("upTime"));
}
private static ModelMBeanInfo getModelMBeanInfoForServer(ObjectName objectName)throws Exception
{
ModelMBeanAttributeInfo[] serverAttributes = new ModelMBeanAttributeInfo[1];
Descriptor upTime = new DescriptorSupport(
new String[] {
"name=upTime",
"descriptorType=attribute",
"displayName=Server upTime",
"getMethod=getUpTime",
});
serverAttributes[0] = new ModelMBeanAttributeInfo(
"upTime",
"long",
"Server upTime",
true,
false,
false,
upTime
);
ModelMBeanOperationInfo[] serverOperations = new ModelMBeanOperationInfo[2];
Descriptor getUpTimeDesc = new DescriptorSupport(
new String[] {
"name=getUpTime",
"descriptorType=operation",
"class=modelmbean.Server",
"role=operation"
});
MBeanParameterInfo[] getUpTimeParms = new MBeanParameterInfo[0];
serverOperations[0] = new ModelMBeanOperationInfo("getUpTime",
"get the up time of the server",
getUpTimeParms,
"java.lang.Long",MBeanOperationInfo.ACTION,
getUpTimeDesc
);
Descriptor startDesc = new DescriptorSupport(
new String[] {
"name=start",
"descriptorType=operation",
"class=modelmbean.Server",
"role=operation"
});
MBeanParameterInfo[] startParms = new MBeanParameterInfo[0];
serverOperations[1] = new ModelMBeanOperationInfo(
"start",
"start(): start server",
startParms,
"java.lang.Integer",
MBeanOperationInfo.ACTION,
startDesc
);
ModelMBeanInfo serverMMBeanInfo = new ModelMBeanInfoSupport(
"modelmbean.Server",
"ModelMBean for managing an Server",
serverAttributes,
null,
serverOperations,
null
);
//Default strategy for the MBean.
Descriptor serverDescription = new DescriptorSupport(
new String[]
{
("name=" + objectName),
"descriptorType=mbean",
("displayName=Server"),
"type=modelmbean.Server",
"log=T",
"logFile=serverMX.log",
"currencyTimeLimit=10"
}
);
serverMMBeanInfo.setMBeanDescriptor(serverDescription);
return serverMMBeanInfo;
}
}

MBean代码例子的更多相关文章

  1. 30个php操作redis常用方法代码例子

    From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...

  2. 30 个 php 操作 redis 常用方法代码例子

    这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类型. list 类型和 set 类型的数据 , 需要的朋友可以参 ...

  3. 最小包围多边形(凸包;最小包围点集)——C代码例子

    本文来自:http://alienryderflex.com/smallest_enclosing_polygon/ 这个C代码例子需要一群2维点集,如下图所示: 要获得包含这些点的最小多边形如下图所 ...

  4. 捕鱼达人代码例子下载地址 mac版

    捕鱼达人代码例子下载地址  mac版: http://pan.baidu.com/share/link?shareid=1431898404&uk=3189484501

  5. 捕鱼达人代码例子下载地址 Win版

    捕鱼达人代码例子下载地址 Win版:: http://pan.baidu.com/share/link?shareid=1601576904&uk=3189484501

  6. 一些 NSArray 的基本操作代码例子

    一些 NSArray 的基本操作代码例子 数组可以说是软件开发人员每天都要面对的基本操作,下面就分享一些 NSArray 的基本操作代码例子供苹果开发初学者参考,每段代码第一行会以注释方式说明该段代码 ...

  7. jeecg 主-附表生成代码例子

    jeecg 主-附表生成代码例子 - CSDN博客https://blog.csdn.net/u010411264/article/details/51243277 JEECG Online Codi ...

  8. 神经网络:caffe特征可视化的代码例子

    caffe特征可视化的代码例子 不少读者看了我前面两篇文章 总结一下用caffe跑图片数据的研究流程 deep learning实践经验总结2--准确率再次提升,到达0.8.再来总结一下 之后.想知道 ...

  9. 爱漂泊人生 30个php操作redis常用方法代码例子

    http://www.justwinit.cn/post/8789/ 背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcach ...

随机推荐

  1. Java实体映射工具MapStruct使用详解

    1.序 通常在后端开发中经常不直接返回实体Entity类,经过处理转换返回前端,前端提交过来的对象也需要经过转换Entity实体才做存储:通常使用的BeanUtils.copyProperties方法 ...

  2. Code Runner for VS Code,下载量突破 3000 万!

    还记得五年前的夏天,我在巨硬写着世界上最好的语言,有时也需要带着游标卡尺写着另一门语言.然而,我对这两门语言都不熟悉,如果能在 VS Code 中方便快捷地运行各种语言,那岂不是很方便?于是,我就开发 ...

  3. PTA 7-4 最小生成树的唯一性 (35分)

    PTA 7-4 最小生成树的唯一性 (35分) 给定一个带权无向图,如果是连通图,则至少存在一棵最小生成树,有时最小生成树并不唯一.本题就要求你计算最小生成树的总权重,并且判断其是否唯一. 输入格式: ...

  4. [loj2470]有向图

    参考ExtremeSpanningTrees,考虑优化整体二分时求$g_{i}\in \{w_{mid},w_{mid+1}\}$的最优解 对于$m=n-1$的问题,不需要去网络流,可以直接树形dp ...

  5. 如何用webgl(three.js)搭建处理3D园区、3D楼层、3D机房管线问题(机房升级版)-第九课(一)

    写在前面的话: 说点啥好呢?就讲讲前两天的小故事吧,让我确实好好反省了一下. 前两天跟朋友一次技术对话,对方问了一下Geometry与BufferGeometry的具体不同,我一下子脑袋短路,没点到重 ...

  6. rocketmq 精华

    (ps:)通过本人语雀文档阅读体验更好哦--有目录 介绍 rocket mq 翻译成中文就是火箭消息队列,从名字就可以看出来,它是一个很快的消息队列... rocket mq 是 阿里巴巴研制的后面贡 ...

  7. CentOS编译openjdk

    编译openjdk 1. 下载openjdk源码 openjdk的官网是OpenJDK (java.net) 在网站左侧就能看到它的源码位置的链接 从图上可以看到,它的源码在两个位置有托管,Mercu ...

  8. [spring-core]作用域

    本文试图从原理上讲解Spring IoC容器的作用域机制,建议对着源码阅读,事半功倍. 0 引入问题 当我们谈到Spring作用域的时候,自然而然会想到如下作用域(来自spring-core官方文档) ...

  9. 快读模板 + #define 压缩for

    快读是一个很重要的模板 #define 压缩for是为了代码的简洁 这里贴一下模板 #define f(i , a , b) for(int i=(a) ; i <= (b) ; i++) us ...

  10. 洛谷 P6295 - 有标号 DAG 计数(生成函数+容斥+NTT)

    洛谷题面传送门 看到图计数的题就条件反射地认为是不可做题并点开了题解--实际上这题以我现在的水平还是有可能能独立解决的( 首先连通这个条件有点棘手,我们尝试把它去掉.考虑这题的套路,我们设 \(f_n ...