JMX

JMX簡介

JMX(Java Management Extensions)是一個爲應用程序植入管理功能的框架。JMX是一套標準的代理和服務,實際上,用戶能夠在任何Java應用程序中使用這些代理和服務實現管理。這是官方文檔上的定義,我看過不少次也沒法很好的理解。我我的的理解是JMX讓程序有被管理的功能,例如你開發一個WEB網站,它是在24小時不間斷運行,那麼你確定會對網站進行監控,如天天的UV、PV是多少;又或者在業務高峯的期間,你想對接口進行限流,就必須去修改接口併發的配置值。html

 JMX架構圖

 

 

案例:java

Mbean架構

package com.hpe.jmx;
//MBean接口
public interface HelloMBean {

    public String getName();

    public void setName(String name);

    public String getAge();

    public void setAge(String age);

    public String helloWorld();

    public void helloWorld(String str);

    public void getTelephone();
}
package com.hpe.jmx;

import java.security.PublicKey;
//Mbean的實現
public class Hello implements HelloMBean {

    private String name;

    private String age;
    public Hello(String name,String age){
        this.name = name;
        this.age = age;
    }

    public void getTelephone() {
        System.out.println("get Telephone");
    }

    public String helloWorld() {
        System.out.println("hello world");
        return "gaojy";
    }

    public void helloWorld(String str) {
        System.out.println("helloWorld:" + str);
    }

    public String getName() {
        System.out.println("get name 123");
        return name;
    }

    public void setName(String name) {
        System.out.println("set name 123");
        this.name = name;
    }

    public String getAge() {
        System.out.println("get age 123");
        return age;
    }

    public void setAge(String age) {
        System.out.println("set age 123");
        this.age = age;
    }
}

適配層:MBeanServer,主要是提供對資源的註冊和管理。併發

接入層:提供遠程訪問的入口。框架

package com.hpe.jmx;

import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class helloAgent { public static void main(String[] args) throws JMException, Exception { //適配層 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName helloName = new ObjectName("jmxBean:name=hello"); // create mbean and register mbean //建立並註冊Mbean server.registerMBean(new Hello("gaojy","24"), helloName); try { //這個步驟很重要,註冊一個端口,綁定url後用於客戶端經過rmi方式鏈接JMXConnectorServer LocateRegistry.createRegistry(9999); // URL路徑的結尾能夠隨意指定,但若是須要用Jconsole來進行鏈接,則必須使用jmxrmi JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi"); JMXConnectorServer jcs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server); System.out.println("begin rmi start"); jcs.start(); System.out.println("rmi start"); } catch (Exception e) { // TODO: handle exception  } Thread.sleep(60 * 60 * 1000); } }

 

客戶端:dom

package com.hpe.jmx;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {
    public static void main(String[] args) throws Exception {

        JMXServiceURL url = new JMXServiceURL(
                "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        // ObjectName的名稱與前面註冊時候的保持一致
        ObjectName mbeanName = new ObjectName("jmxBean:name=hello");

        System.out.println("Domains ......");
        String[] domains = mbsc.getDomains();

        for (int i = 0; i < domains.length; i++) {
            System.out.println("doumain[" + i + "]=" + domains[i]);
        }

        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // 設置指定Mbean的特定屬性值
        // 這裏的setAttribute、getAttribute操做只能針對bean的屬性
        // 例如對getName或者setName進行操做,只能使用Name,須要去除方法的前綴
        mbsc.setAttribute(mbeanName, new Attribute("Name", "杭州"));// 遠程設置
        mbsc.setAttribute(mbeanName, new Attribute("Age", "1990"));
        String age = (String) mbsc.getAttribute(mbeanName, "Age"); // 遠程獲取
        String name = (String) mbsc.getAttribute(mbeanName, "Name");
        System.out.println("age=" + age + ";name=" + name);

        // 遠程方法調用
        HelloMBean proxy = MBeanServerInvocationHandler.newProxyInstance(mbsc,
                mbeanName, HelloMBean.class, false);
        proxy.helloWorld();
        proxy.helloWorld("migu");
        proxy.getTelephone();
        // invoke調用bean的方法,只針對非設置屬性的方法
        // 例如invoke不能對getName方法進行調用
        mbsc.invoke(mbeanName, "getTelephone", null, null);
        mbsc.invoke(mbeanName, "helloWorld",
                new String[] { "I'll connect to JMX Server via client2" },
                new String[] { "java.lang.String" });
        mbsc.invoke(mbeanName, "helloWorld", null, null);
    }
}

 

參考網站

http://www.cnblogs.com/dongguacai/p/5900507.htmlthis

相關文章
相關標籤/搜索