zoukankan      html  css  js  c++  java
  • MBean代码例子

    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;
        }
    }
  • 相关阅读:
    java将Thu Jun 11 00:00:00 CST 2020类型转换为正常的时间类型
    SSM中JSP和thymeleaf同时使用
    java中thymeleaf获取项目根路径
    使用position:fixed定位的DIV元素,如何使其居中
    HTML设置textarea不可拖动
    使用js让Html页面弹出小窗口
    wow.js – 让页面滚动更有趣,使用方法
    CSS修改文本框placeholder提示的文字颜色
    JS监听用户按下ESC
    当div内所有元素都是浮动的时候,让容器(div)自动适应高度
  • 原文地址:https://www.cnblogs.com/wangyonglong/p/7347221.html
Copyright © 2011-2022 走看看