1 7 package test.sample.simple; 8 9 import javax.management.MBeanServerConnection ; 10 import javax.management.MBeanServerDelegateMBean ; 11 import javax.management.MBeanServerInvocationHandler ; 12 import javax.management.ObjectName ; 13 import javax.management.NotificationListener ; 14 import javax.management.Notification ; 15 import javax.management.remote.JMXConnector ; 16 import javax.management.remote.JMXConnectorFactory ; 17 import javax.management.remote.JMXServiceURL ; 18 19 22 public class Client 23 { 24 public static void main(String [] args) throws Exception 25 { 26 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector"); 28 29 JMXConnector connector = JMXConnectorFactory.connect(url); 30 31 MBeanServerConnection connection = connector.getMBeanServerConnection(); 32 33 ObjectName delegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 34 Object proxy = MBeanServerInvocationHandler.newProxyInstance(connection, delegateName, MBeanServerDelegateMBean .class, true); 35 MBeanServerDelegateMBean delegate = (MBeanServerDelegateMBean ) proxy; 36 37 System.out.println("MBeanServer vendor is " + delegate.getImplementationVendor()); 38 System.out.println("MBeanServer version is " + delegate.getImplementationVersion()); 39 System.out.println("MBeanServer specification name is " + delegate.getSpecificationName()); 40 System.out.println("MBeanServer specification vendor is " + delegate.getSpecificationVendor()); 41 System.out.println("MBeanServer specification version is " + delegate.getSpecificationVersion()); 42 System.out.println("MBeanServer MBeanCount is " + connection.getMBeanCount()); 43 44 ObjectName objName = new ObjectName ("test:name=sample"); 45 connection.createMBean(Sample.class.getName(), objName); 46 Object ret = connection.invoke(objName, "doSomething", new Object [] {"foo"}, new String [] {String .class.getName()}); 47 System.out.println("Return to doSomething() is " + ret); 48 49 NotificationListener listener = new Listener (); 50 connection.addNotificationListener(objName,listener, null, null); 51 52 ret = connection.invoke(objName, "doSomething", new Object [] {"bar"}, new String [] {String .class.getName()}); 53 System.out.println("Return to doSomething() is " + ret); 54 55 Thread.currentThread().sleep(5000); 57 58 connection.removeNotificationListener(objName, listener); 59 60 System.out.println("Removed notification listener."); 61 62 } 63 64 65 public static class Listener implements NotificationListener 66 { 67 68 76 public void handleNotification(Notification notification, Object handback) 77 { 78 System.out.println("Got notification: " + notification); 79 } 80 } 81 } | Popular Tags |