zoukankan      html  css  js  c++  java
  • HelloWorldDynamic

    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;
        }
        
        
        
        
    }   
  • 相关阅读:
    在Linux CentOS上编译并安装Clang 3.5.0
    在Linux CentOS 6.6上安装Python 2.7.9
    Mac OS X上用CoreCLR运行一个真正的.NET控制台程序
    在Mac OS X上用自己编译出的CoreCLR运行.NET程序
    Mac OS X上尝试编译CoreCLR源代码
    Linux上成功编译CoreCLR源代码
    CoreCLR中超过3万行代码的gc.cpp文件的来源
    Windows上成功编译CoreCLR源代码
    “CoreCLR is now Open Source”阅读笔记
    AutoMapper指定列名进行映射
  • 原文地址:https://www.cnblogs.com/wangyonglong/p/7427184.html
Copyright © 2011-2022 走看看