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. httprunner3源码解读(2)models.py

    源码目录结构 我们首先来看下models.py的代码结构 我们可以看到这个模块中定义了12个属性和22个模型类,我们依次来看 属性源码分析 import os from enum import Enu ...

  2. IDEA中Update resources和Update classes and resources、Redeploy、Restart server的区别

    选项 描述 update resources 所有更改的资源都会更新(HTML,JSP,JavaScript,CSS和图像文件) update classes and resources 更改的资源将 ...

  3. 谷粒 | 10 | 阿里云OSS存储对象服务

    阿里云OSS对象存储服务 准备工作 1.在service模块新建子模块service_oss 2.引入pom.xml文件中引入oss服务依赖 <dependencies> <!--a ...

  4. Linux usb 6. HC/UDC 测试

    目录 1. 背景介绍 2. Device (gadget zero) 2.1 gadget zero 创建 2.2 SourceSink Function 2.3 Loopback Function ...

  5. 大爽Python入门教程 1-4 习题

    大爽Python入门公开课教案 点击查看教程总目录 1 [思考]方向变换 小明同学站在平原上,面朝北方,向左转51次之后(每次只转90度), 小明面朝哪里?小明转过了多少圈? (360度为一圈,圈数向 ...

  6. Centos8 Docker部署 .Net6 项目

    .Net6项目发布 1.在VS中发布项目,并编写好Dockerfile文件 Dockerfile文件内容如下: FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS ...

  7. Python+selenium 之xpath定位

  8. 『学了就忘』Linux权限管理 — 53、ACL权限详解

    目录 1.什么是ACL权限 2.开启ACL 3.ACL权限的相关命令 (1)设定ACL权限 (2)查询文件的ACL权限 (3)设置文件ACL权限给用户组 (4)给文件夹和里边的文件同时赋予ACL权限 ...

  9. 关于"丢失的牛"这个题的教学反思

    某天上课讲到这样一个题:丢失的牛1~n,乱序排列,告诉从第二个位置到最后一个位置, 每个位置的前面的数字中比它小的数的个数,求每个位置的数字是多少N<=8000 FormatInput第一行给出 ...

  10. 【NOIP 2018】摆渡车

    前情提要 是的 我终于回来补坑了 一年了哇 你这个鸽子王 斜率优化版本 今天在复习斜率优化的时候才想起来这个题 定义就不设了 大家想看可以看上面那个原版 怎么斜率优化呢? 我们考虑\(i\)点是当前的 ...