package mbeanTest;

import java.lang.reflect.Method;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.ReflectionException; public class HelloWorldDynamic implements DynamicMBean
{
public String hello; public HelloWorldDynamic()
{
this.hello = "Hello World! I am a Dynamic MBean";
} public HelloWorldDynamic(String hello)
{
this.hello = hello;
} public String getHello()
{
return hello;
} public Object getInstance()
{
return new Object();
} public void setHello(String hello)
{
this.hello = hello;
} @Override
public Object getAttribute(String attribute)
throws AttributeNotFoundException, MBeanException,
ReflectionException
{
// 设置getAttribute的执行逻辑
if ("getInstance".equals(attribute))
{
return getInstance();
}
if("gh".equals(attribute))
{
return "fffffff";
} return null;
} @Override
public AttributeList getAttributes(String[] attributes)
{
// TODO Auto-generated method stub
return null;
} MBeanInfo info = null; @Override
public MBeanInfo getMBeanInfo()
{
try
{
Class cls = this.getClass();
// 用反射获得 "getHello" 属性的读方法
// DynamicMBean中,
Method readMethod = cls.getMethod("getHello", new Class[0]);
MBeanAttributeInfo attribute = new MBeanAttributeInfo("gh",
" the first attribute ", readMethod, null);
// 执行java类的method需要的一些元数据,由MBeanOperationInfo提供
MBeanOperationInfo operation = new MBeanOperationInfo(
" the first operation ", cls.getMethod("getInstance", null));
info = new MBeanInfo(cls.getName(), " this is a dynamic MBean ",
new MBeanAttributeInfo[]
{ attribute }, null, new MBeanOperationInfo[]
{ operation }, null);
} catch (Exception e)
{
System.out.println(e);
}
return info;
} @Override
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException
{
System.out.println(" the HelloWorldDynamic's method invoke ");
return null;
} @Override
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException
{ } @Override
public AttributeList setAttributes(AttributeList attributes)
{
return null;
} }

HelloWorldDynamic的更多相关文章

  1. JmxTest

    package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...

随机推荐

  1. Python打包成exe,文件太大问题解决办法

    Python打包成exe,文件太大问题解决办法 原因 解决办法 具体步骤 情况一:初次打包 情况二:再次打包 原因 由于使用pyinstaller打包.py文件时,会把很多已安装的无关库同时打包进去, ...

  2. MAC安装vue.js

    一.下载node node下载地址:https://nodejs.org/en/download/ 下载后点击安装即可. node -v 检查安装是否成功 二.安装 淘宝镜像 (npm) npm in ...

  3. yum设置取消代理

    unset http_proxy unset https_proxy 暂时取消代理,若永久取消代理,需要修改/etc/yum.conf 去掉 proxy=http://proxyhost:8080

  4. 关于Python中用户输入字符串(与变量名相同)无法作为变量名引用的理解以及解决方案

    在用户登录账号时,我需要在字典中查找是否存在其账号名称以及密码是否正确. 所以,我想将用户输入的账号赋值给变量,在字典中查找是否有此指值. 代码如下: 1 Ya = {'姓名': 'Ya', 'pas ...

  5. 题解 P6345 [CCO 2017]接雨滴

    解题思路 NOIP在即,感觉即将退役,或许是最后一篇题解了... 求水的体积不是特别好求,我们考虑求出 水+柱子 的体积最后再减去柱子的体积. 发现对于最高的柱子在中间的情况其实可以把最高柱子一侧的柱 ...

  6. Java安全之基于Tomcat的通用回显链

    Java安全之基于Tomcat的通用回显链 写在前面 首先看这篇文还是建议简单了解下Tomcat中的一些概念,不然看起来会比较吃力.其次是回顾下反射中有关Field类的一些操作. * Field[] ...

  7. C#练习4

    //错误的程序using System; class Test { unsafe static void WriteLocations(byte[]arr) { fixed(byte*p_arr=ar ...

  8. MYSQL数据库重新初始化

    前言 我们在日常开发过程中,可能会遇到各种mysql服务无法启动的情况,各种百度谷歌之后,依然不能解决的时候,可以考虑重新初始化mysql.简单说就是重置,"恢复出厂设置".重置之 ...

  9. 谱文混排之lilypond-book

    Lilypond有自带的谱文混排的工具lilypond-book,但是作为外行,一直很难搞清楚这个操作是怎样做的.很久之前请教过别人,但介于我的个人能力,只有粗浅理解,操作不得要领.在许多信息的拼凑之 ...

  10. java8两个字段进行排序问题

    //这个解决问题 Comparator<Anjianxinxi> getLianriqi = Comparator.comparing(Anjianxinxi::getLianriqi). ...