1 package demo.hw.jmxconsole; 2 3 import java.util.Iterator ; 4 import java.util.Set ; 5 import javax.management.Attribute ; 6 import javax.management.MBeanAttributeInfo ; 7 import javax.management.MBeanInfo ; 8 import javax.management.MBeanServerConnection ; 9 import javax.management.ObjectName ; 10 import javax.management.remote.JMXConnector ; 11 import javax.management.remote.JMXConnectorFactory ; 12 import javax.management.remote.JMXServiceURL ; 13 14 public final class Client { 15 16 private static MBeanServerConnection mbsc; 17 18 private Client() { 19 20 } 21 public static void main(String [] args) { 22 try { 23 24 JMXServiceURL url = new JMXServiceURL ( 25 "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/server"); 26 JMXConnector jmxc = JMXConnectorFactory.connect(url, null); 27 28 mbsc = jmxc.getMBeanServerConnection(); 29 30 String domain = "org.objectweb.celtix.instrumentation"; 31 32 echo("\n The Celtix Management Demo MBeanServer has " + mbsc.getMBeanCount() + " MBeans"); 33 echo("\n There are :"); 34 35 Set names = mbsc.queryNames(null, null); 36 for (Iterator i = names.iterator(); i.hasNext();) { 37 ObjectName on = (ObjectName )i.next(); 38 echo("\n\t get ObjectName = " + on + "\n\t Its Attributes are :"); 39 getInstrumentationAttributes(on); 40 } 41 42 ObjectName serviceMBeanName = new ObjectName ( 43 domain + ":type=ServerMBean,Bus=celtix,name=ServerMBean"); 44 echo("\n >>> get the ServiceMBean infor <<<"); 45 46 echo("\nServiceMBean.ServiceName = " + mbsc.getAttribute(serviceMBeanName, "ServiceName")); 47 echo("\nServiceMBean.Address = " + mbsc.getAttribute(serviceMBeanName, "Address")); 48 49 ObjectName greeterInstrumentation = new ObjectName ( 50 domain + ":type=GreeterInstrumentation,Bus=celtix,name=Demo.Management"); 51 52 echo("\n >>> get the GreeterInstrumentation counter infor <<<"); 53 54 getInstrumentationAttributes(greeterInstrumentation); 55 56 57 mbsc.setAttribute(greeterInstrumentation, 58 new Attribute ("PingMeCounter", new Integer ("20"))); 59 echo("\n >>> set the GreeterInstrumentation PingMeCounter to be 20 <<<"); 60 echo("\n >>> get the GreeterInstrumentation counter infor <<<"); 61 62 getInstrumentationAttributes(greeterInstrumentation); 63 64 Object [] params = new Object [1]; 65 params[0] = "JMXConsoleName"; 66 String [] signature = new String [1]; 67 signature[0] = "java.lang.String"; 68 mbsc.invoke(greeterInstrumentation, 69 "setSayHiReturnName", params, signature); 70 echo("\n >>> invoke the GreeterInstrumentation setSayHiReturnName method"); 71 echo("\n JMXConsole runs Successfully!"); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 public static void getInstrumentationAttributes(ObjectName name) throws Exception { 78 79 MBeanInfo info = mbsc.getMBeanInfo(name); 80 MBeanAttributeInfo [] attrs = info.getAttributes(); 81 if (attrs == null) { 82 return; 83 } 84 for (int i = 0; i < attrs.length; i++) { 85 if (attrs[i].isReadable()) { 86 try { 87 Object o = mbsc.getAttribute(name, attrs[i].getName()); 88 echo("\n\t\t" + attrs[i].getName() + " = " + o); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } 92 } 93 } 94 } 95 96 public static void echo(String msg) { 97 System.out.print(msg); 98 } 99 100 101 } 102 | Popular Tags |