zoukankan      html  css  js  c++  java
  • JMX浅谈

    一 JMX 是什么?

    JMX(Java Management Extensions,即Java管理扩展)

    JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及服务。通常使用JMX来监控系统的运行状态或管理系统的某些方面,比如清空缓存、重新加载配置文件等
    优点可以非常容易的使应用程序具有被管理
    伸缩性的架构每个JMX Agent服务可以很容易的放入到Agent中,每个JMX的实现都提供几个核心的Agent服务,你也可以自己编写服务,服务可以很容易的部署,取消部署。

    主要提供接口,允许有不同的实现
    二 JMX 实例分析
    分为服务器端和客户端
    服务器端
    Monitor.java

    public class Monitor {
        public Monitor(){
            
        }
        public static void main(String[] args) {
            start();
        }
     
        public static void start() {
            new Thread(new Runnable() {
                public void run() {

    Thread.currentThread().setContextClassLoader(Monitor.class.getClassLoader()); MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName memory; try { memory = new ObjectName(Constants.MEMORY_MANAGE_BEAN); //注册待监控Object class server.registerMBean(new MemoryManager(), memory); //监控端口 int serverPort = Constants.MONITOR_SERVANT_PORT; LocateRegistry.createRegistry(serverPort); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/jmxrmi"); JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); cs.start(); System.out.println("----jmx server start ----- "); } catch (Exception e) { System.out.println("----Fail to start server----"); e.printStackTrace(); } } }).start(); } }

    被调用类

    接口MemoryManagerMXBean.java

    public interface MemoryManagerMXBean {
        public void stop();  
     
        public long getTotalPhysicalMemorySize(int mask);
        
        public static class MemoryStateMask{
            public static final int OVERALL_MEMORY_USE = 1;  
            public static final int TYPE_ALLTIME_MEMORY_USE = 2;  /
        }
    
        public String getMemoryState(int memoryStateMask);
    }

    MemoryManager.java

    public class MemoryManager implements MemoryManagerMXBean{    
        public MemoryManager(){ 
        } 
        @Override
        public String getMemoryState(int mask) {
            String result = "";
            switch(mask){
            case MemoryStateMask.OVERALL_MEMORY_USE:  
                result = getOverallMemoryState(); 
                break;
            case MemoryStateMask.TYPE_ALLTIME_MEMORY_USE:
                result = MonitorRuntime.getTypeMemoryUse(MonitorRuntime.TYPE_MEMORY_USE_OPTION.ALLTIME_USE);
                break;
            } 
            
            return result;
        } 
     private long getUsedMemory(){
            Runtime rt = Runtime.getRuntime();
            long usedMem = rt.totalMemory() - rt.freeMemory();
            return usedMem;
        }
    }
     

    客户端调用:
    JmxWebContainerConnector.java

    public class JmxWebContainerConnector {
        private MBeanServerConnection mbsc;
        private ObjectName mbeanName;
        public JmxWebContainerConnector() throws Exception {
            try {
                int serverPort = Constants.MONITOR_SERVANT_PORT;
                JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + serverPort + "/jmxrmi");
                JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
                this.mbsc = jmxc.getMBeanServerConnection();
                this.mbeanName = new ObjectName(Constants.MEMORY_MANAGE_BEAN);
            } catch (Exception e) {
                System.err.println("。。。。连接服务器失败。。。。");
                throw new Exception(e);
            }
        }public MemoryState getMemoryState() throws Exception {
            int overallMemoryUseMask = 1;
            String json = (String) mbsc.invoke(mbeanName, "getMemoryState", new Object[] { overallMemoryUseMask }, new String[] { "int" });
            MemoryState state = MemoryState.fromJmxJSON(json);
            return state;
        }
        
    }
     
  • 相关阅读:
    MySQL统计逗号分隔字段元素的个数
    MySQL学习(十)索引
    MySQL学习(十一)为什么不推荐使用uuid和雪花id
    1.计算机系统概论
    MySQL学习(十二)排序原理
    MySQL学习(九)frm与ibd了解
    MySQL学习(十一)B树与B+树了解
    MySQL 某一列的值加入到另一列
    MySQL学习(十三)InnoDB
    IOSios微信打开网页键盘弹起后页面上滑,导致弹框里的按钮响应区域错位
  • 原文地址:https://www.cnblogs.com/zabulon/p/5813235.html
Copyright © 2011-2022 走看看