001: package demo.hw.jmxconsole;
002:
003: import java.util.Iterator;
004: import java.util.Set;
005: import javax.management.Attribute;
006: import javax.management.MBeanAttributeInfo;
007: import javax.management.MBeanInfo;
008: import javax.management.MBeanServerConnection;
009: import javax.management.ObjectName;
010: import javax.management.remote.JMXConnector;
011: import javax.management.remote.JMXConnectorFactory;
012: import javax.management.remote.JMXServiceURL;
013:
014: public final class Client {
015:
016: private static MBeanServerConnection mbsc;
017:
018: private Client() {
019:
020: }
021:
022: public static void main(String[] args) {
023: try {
024:
025: JMXServiceURL url = new JMXServiceURL(
026: "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/server");
027: JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
028:
029: mbsc = jmxc.getMBeanServerConnection();
030:
031: String domain = "org.objectweb.celtix.instrumentation";
032:
033: echo("\n The Celtix Management Demo MBeanServer has "
034: + mbsc.getMBeanCount() + " MBeans");
035: echo("\n There are :");
036:
037: Set names = mbsc.queryNames(null, null);
038: for (Iterator i = names.iterator(); i.hasNext();) {
039: ObjectName on = (ObjectName) i.next();
040: echo("\n\t get ObjectName = " + on
041: + "\n\t Its Attributes are :");
042: getInstrumentationAttributes(on);
043: }
044:
045: ObjectName serviceMBeanName = new ObjectName(domain
046: + ":type=ServerMBean,Bus=celtix,name=ServerMBean");
047: echo("\n >>> get the ServiceMBean infor <<<");
048:
049: echo("\nServiceMBean.ServiceName = "
050: + mbsc
051: .getAttribute(serviceMBeanName,
052: "ServiceName"));
053: echo("\nServiceMBean.Address = "
054: + mbsc.getAttribute(serviceMBeanName, "Address"));
055:
056: ObjectName greeterInstrumentation = new ObjectName(
057: domain
058: + ":type=GreeterInstrumentation,Bus=celtix,name=Demo.Management");
059:
060: echo("\n >>> get the GreeterInstrumentation counter infor <<<");
061:
062: getInstrumentationAttributes(greeterInstrumentation);
063:
064: mbsc.setAttribute(greeterInstrumentation, new Attribute(
065: "PingMeCounter", new Integer("20")));
066: echo("\n >>> set the GreeterInstrumentation PingMeCounter to be 20 <<<");
067: echo("\n >>> get the GreeterInstrumentation counter infor <<<");
068:
069: getInstrumentationAttributes(greeterInstrumentation);
070:
071: Object[] params = new Object[1];
072: params[0] = "JMXConsoleName";
073: String[] signature = new String[1];
074: signature[0] = "java.lang.String";
075: mbsc.invoke(greeterInstrumentation, "setSayHiReturnName",
076: params, signature);
077: echo("\n >>> invoke the GreeterInstrumentation setSayHiReturnName method");
078: echo("\n JMXConsole runs Successfully!");
079: } catch (Exception e) {
080: e.printStackTrace();
081: }
082: }
083:
084: public static void getInstrumentationAttributes(ObjectName name)
085: throws Exception {
086:
087: MBeanInfo info = mbsc.getMBeanInfo(name);
088: MBeanAttributeInfo[] attrs = info.getAttributes();
089: if (attrs == null) {
090: return;
091: }
092: for (int i = 0; i < attrs.length; i++) {
093: if (attrs[i].isReadable()) {
094: try {
095: Object o = mbsc.getAttribute(name, attrs[i]
096: .getName());
097: echo("\n\t\t" + attrs[i].getName() + " = " + o);
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102: }
103: }
104:
105: public static void echo(String msg) {
106: System.out.print(msg);
107: }
108:
109: }
|