Java 怎么实现与JMX代理交互的JMX客户端

2019年7月18日19:55:31 评论 70

1.简介

Java 怎么实现与JMX代理交互的JMX客户端?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import javax.management.AttributeChangeNotification;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {

    
    public static class ClientListener implements NotificationListener {
        public void handleNotification(Notification notification,
                Object handback) {
            echo("\nReceived notification:");
            echo("\tClassName: " + notification.getClass().getName());
            echo("\tSource: " + notification.getSource());
            echo("\tType: " + notification.getType());
            echo("\tMessage: " + notification.getMessage());
            if (notification instanceof AttributeChangeNotification) {
                AttributeChangeNotification acn = (AttributeChangeNotification) notification;
                echo("\tAttributeName: " + acn.getAttributeName());
                echo("\tAttributeType: " + acn.getAttributeType());
                echo("\tNewValue: " + acn.getNewValue());
                echo("\tOldValue: " + acn.getOldValue());
            }
        }
    }

    
    public static void main(String[] args) throws Exception {
        
        
        
        echo("\nCreate an RMI connector client and "
                + "connect it to the RMI connector server");
        JMXServiceURL url = new JMXServiceURL(
                "service:jmx:rmi:
        JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

        
        
        ClientListener listener = new ClientListener();

        
        
        echo("\nGet an MBeanServerConnection");
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        waitForEnterPressed();

        
        
        echo("\nDomains:");
        String domains[] = mbsc.getDomains();
        Arrays.sort(domains);
        for (String domain : domains) {
            echo("\tDomain = " + domain);
        }
        waitForEnterPressed();

        
        
        echo("\nMBeanServer default domain = " + mbsc.getDefaultDomain());

        
        
        echo("\nMBean count = " + mbsc.getMBeanCount());

        
        
        echo("\nQuery MBeanServer MBeans:");
        Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(
                null, null));
        for (ObjectName name : names) {
            echo("\tObjectName = " + name);
        }
        waitForEnterPressed();

        
        
        

        echo("\n>>> Perform operations on Hello MBean <<<");

        
        
        ObjectName mbeanName = new ObjectName("com.example:type=Hello");

        
        
        
        HelloMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName,
                HelloMBean.class, true);

        
        
        echo("\nAdd notification listener...");
        mbsc.addNotificationListener(mbeanName, listener, null, null);

        
        
        echo("\nCacheSize = " + mbeanProxy.getCacheSize());

        
        
        
        
        
        mbeanProxy.setCacheSize(150);

        
        
        echo("\nWaiting for notification...");
        sleep(2000);

        
        
        echo("\nCacheSize = " + mbeanProxy.getCacheSize());

        
        
        echo("\nInvoke sayHello() in Hello MBean...");
        mbeanProxy.sayHello();

        
        
        echo("\nInvoke add(2, 3) in Hello MBean...");
        echo("\nadd(2, 3) = " + mbeanProxy.add(2, 3));

        waitForEnterPressed();

        
        
        

        echo("\n>>> Perform operations on QueueSampler MXBean <<<");

        
        
        ObjectName mxbeanName = new ObjectName(
                "com.example:type=QueueSampler");

        
        
        
        QueueSamplerMXBean mxbeanProxy = JMX.newMXBeanProxy(mbsc,
                mxbeanName, QueueSamplerMXBean.class);

        
        
        QueueSample queue1 = mxbeanProxy.getQueueSample();
        echo("\nQueueSample.Date = " + queue1.getDate());
        echo("QueueSample.Head = " + queue1.getHead());
        echo("QueueSample.Size = " + queue1.getSize());

        
        
        echo("\nInvoke clearQueue() in QueueSampler MXBean...");
        mxbeanProxy.clearQueue();

        
        
        QueueSample queue2 = mxbeanProxy.getQueueSample();
        echo("\nQueueSample.Date = " + queue2.getDate());
        echo("QueueSample.Head = " + queue2.getHead());
        echo("QueueSample.Size = " + queue2.getSize());

        waitForEnterPressed();

        
        
        echo("\nClose the connection to the server");
        jmxc.close();
        echo("\nBye! Bye!");
    }

    private static void echo(String msg) {
        System.out.println(msg);
    }

    private static void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void waitForEnterPressed() {
        try {
            echo("\nPress <Enter> to continue...");
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



 
 
interface HelloMBean {
    
    
    

    public void sayHello();

    public int add(int x, int y);

    
    
    

    
    public String getName();

    
    public int getCacheSize();

    public void setCacheSize(int size);
}


 

3.总结

示例代码演示了 Java 怎么实现与JMX代理交互的JMX客户端,具体步骤参见示例代码。

  • A+
所属分类:JMX

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: