KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > hw > jmxconsole > Client


1 package demo.hw.jmxconsole;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Set JavaDoc;
5 import javax.management.Attribute JavaDoc;
6 import javax.management.MBeanAttributeInfo JavaDoc;
7 import javax.management.MBeanInfo JavaDoc;
8 import javax.management.MBeanServerConnection JavaDoc;
9 import javax.management.ObjectName JavaDoc;
10 import javax.management.remote.JMXConnector JavaDoc;
11 import javax.management.remote.JMXConnectorFactory JavaDoc;
12 import javax.management.remote.JMXServiceURL JavaDoc;
13
14 public final class Client {
15
16     private static MBeanServerConnection JavaDoc mbsc;
17
18     private Client() {
19         
20     }
21     public static void main(String JavaDoc[] args) {
22         try {
23
24             JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(
25                       "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/server");
26             JMXConnector JavaDoc jmxc = JMXConnectorFactory.connect(url, null);
27
28             mbsc = jmxc.getMBeanServerConnection();
29
30             String JavaDoc 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 JavaDoc names = mbsc.queryNames(null, null);
36             for (Iterator JavaDoc i = names.iterator(); i.hasNext();) {
37                 ObjectName JavaDoc on = (ObjectName JavaDoc)i.next();
38                 echo("\n\t get ObjectName = " + on + "\n\t Its Attributes are :");
39                 getInstrumentationAttributes(on);
40             }
41
42             ObjectName JavaDoc serviceMBeanName = new ObjectName JavaDoc(
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 JavaDoc greeterInstrumentation = new ObjectName JavaDoc(
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 JavaDoc("PingMeCounter", new Integer JavaDoc("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 JavaDoc[] params = new Object JavaDoc[1];
65             params[0] = "JMXConsoleName";
66             String JavaDoc[] signature = new String JavaDoc[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 JavaDoc e) {
73             e.printStackTrace();
74         }
75     }
76
77     public static void getInstrumentationAttributes(ObjectName JavaDoc name) throws Exception JavaDoc {
78
79         MBeanInfo JavaDoc info = mbsc.getMBeanInfo(name);
80         MBeanAttributeInfo JavaDoc[] 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 JavaDoc o = mbsc.getAttribute(name, attrs[i].getName());
88                     echo("\n\t\t" + attrs[i].getName() + " = " + o);
89                 } catch (Exception JavaDoc e) {
90                     e.printStackTrace();
91                 }
92             }
93         }
94     }
95     
96     public static void echo(String JavaDoc msg) {
97         System.out.print(msg);
98     }
99
100     
101 }
102
Popular Tags