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. Java-基础-HashMap

    1. 简介 Java8 HashMap结构(数组 + 列表 + 红黑树)如图: 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和 ...

  2. CentOS服务器的网络配置与部署

    1.系统安装与软件安装 1.1选择CentOs7.9release版本用作所研发系统部署服务器,官网以及所选择镜像为地址为:http://ftp.sjtu.edu.cn/centos/7.9.2009 ...

  3. Java之JNDI注入

    Java之JNDI注入 About JNDI 0x01 简介 JNDI(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JND ...

  4. Windows内核中的CPU架构-7-陷阱门(32-Bit Trap Gate)

    Windows内核中的CPU架构-7-陷阱门(32-Bit Trap Gate) 陷阱门和中断门几乎是一模一样的: (注:图里高32位中的第11位的值为D,其实是1) 除了高32位中的type字段的内 ...

  5. 站长管理服务器必读:Ftp、Ftps与Sftp三兄弟的不同与区别以及部署全指引

    文章标题: 站长管理服务器必读:Ftp.Ftps与Sftp三兄弟的不同与区别以及部署全指引 关键字 : ftp,sftp,freesshd,ftps 文章分类: 教程 创建时间: 2020年3月23日 ...

  6. 探讨 Rust 智能指针 | Vol.17

    分享主题:<探讨 Rust 智能指针>| Vol. 17 分享讲师:苏林 分享时间: 周日晚上 2021-11-14 20:30-21:30 腾讯会议地址: https://meeting ...

  7. js 函数和函数的参数

    /* * 函数 function *     - 函数也是一个对象 *     - 函数中可以封装一些功能(代码),在需要时可以执行这些功能(代码) *     - 函数中可以保存一些代码在需要的时候 ...

  8. 菜鸡的Java笔记 数字操作类

    数字操作类        Math 类的使用        Random 类的使用        BigInteger 和 BigDecimal 类的使用                Math 是一 ...

  9. 【AWS】通过对等网络打通VPC访问

    参考 什么是 VPC 对等? - Amazon Virtual Private Cloud 目的 有些服务,比如内网ALB,不公开的RDS仅允许VPC内部访问.如遇到跨账号.跨区域访问,则需要在两个v ...

  10. 使用 CSS 轻松实现一些高频出现的奇形怪状按钮

    背景 在群里会有同学问相关的问题,怎么样使用 CSS 实现一个内切角按钮呢.怎么样实现一个带箭头的按钮呢? 本文基于一些高频出现在设计稿中的,使用 CSS 实现稍微有点难度和技巧性的按钮,讲解使用 C ...