FreeOZ论坛

标题: JMX Works as RPC Protocol [打印本页]

作者: key    时间: 19-10-2009 14:34
标题: JMX Works as RPC Protocol
JMX is not an RPC protocol, at least, it was not designed for
PRC purpose. However, JMX could work exactly like a RPC protocol,
such as RMI does. I would like to introduce how here.

References:
1. JMX Tutorial: http://java.sun.com/docs/books/tutorial/jmx/
2. Developing a custom JMX client: http://www.zdnetasia.com/techguide/java/0,39044898,61965454,00.htm
3. The JMX MXBean: http://www.javaworld.com/community/node/1186
4. Annotation Type MXBean: http://java.sun.com/javase/6/doc ... agement/MXBean.html
作者: key    时间: 19-10-2009 14:41
标题: 1. A Simple JMX Service
As you might already know, JMX has five types of
MBeans: Standard MBean, MXBean, Dynamic MBean, OpenMBean and Model MBean.
Developing Standard MBean is the easiest way to develop
a JMX service.

Please refer to the JMX Tutorial. You can find a simple JMX
program using Standard MBean technology there. Please look at
the classes/interfaces:
HelloMBean
Hello
HelloMain

OK, stop here, don't go further.
作者: key    时间: 19-10-2009 14:50
标题: 2. Remote Management
To simplify the client program, I would like to run the remote management server.
Please refer to:
http://java.sun.com/docs/books/tutorial/jmx/remote/jconsole.html

Here's what I input from my command line:

java -Dcom.sun.management.jmxremote.port=9999 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-cp dist/MBeanTest.jar
com.example.Main

Note that JMX remote management feature are totally runtime configurable.
That's so attractive.
作者: key    时间: 19-10-2009 14:55
标题: 3. A Simple Client
I'm going to run the MBean directly using RPC methods, just like what
I did with RMI's clients. So I using MBean Proxies here.

JMXServiceURL url = new ....
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName = new ObjectName("com.example:type=Hello");
HelloMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, HelloMBean.class, true);
mbeanProxy.sayHello();
jmxc.close();
作者: key    时间: 19-10-2009 15:01
标题: 4. Sending Custom Data Object Back & Forth
public class X implements Serializable {
}

public class X2 extends X {
   private String a;
   private int b;
   public X2(String a, int b) { this.a = a; this.b = b; }
   public String toString() { return "a = " + a + ", b = " + b; }
}

public class HelloMBean {
  ....
  public X2 doWithX(X x) { System.out.println("X = " + x); return new X2("Hello", (int)(Math.random() * 100)); }
}

....

Test the above code, you will fine there is no difference between JMX and RMI with respect of
transferring Data Objects.
作者: black_zerg    时间: 19-10-2009 15:19
提示: 作者被禁止或删除, 无法发言 这个是用来管理java软件的那个吧。好像tomcat可以用这个控制来着。可以开发一系列产品然后用一个软件console管理这些进程。
唉,现在总觉得对工作来说,似乎没太大意义搞这些比较精巧但是费时间的东西,特别小组里别人都没什么兴趣。

[ 本帖最后由 black_zerg 于 19-10-2009 15:21 编辑 ]
作者: key    时间: 19-10-2009 15:53
标题: 5. MXBeans
The example using MBean in 4 requires the complex Data Object
implements Serializable. Additionally, you can not use jConsole to
retrieve the value. You can avoid this disadvantage by applying MXBean.

Please refer to:
http://java.sun.com/javase/6/doc ... agement/MXBean.html

MemoryPoolMXBean proxy =
    JMX.newMXBeanProxy(
        mbeanServer,
        objectName,
        MemoryPoolMXBean.class);
String name = proxy.getName();
MemoryUsage usage = proxy.getUsage();
long used = usage.getUsed();

It's nearly the same as what you might do with Standard MBean.
However, you can use jConsole to management the value now.
作者: key    时间: 19-10-2009 16:13
标题: 6. MXBean -> MXBean
You can retrieve an MXBean via another MXBean, then invoke
the method of the later one. However, make sure you register
the returned MXBean, or you'll get an error of:

java.lang.reflect.UndeclaredThrowableException
Caused by: javax.management.openmbean.OpenDataException: No name for object: .....

Return the MXBean is one of the most important features
regarding RPC's.

public interface RetMXBean {
    public void dx();
}

public class Ret implements RetMXBean{

    private static Ret instance = new Ret();

    public static RetMXBean instance() {
        return instance;
    }
   
    public void dx() {
        System.out.println("Hello RetMXBean");
    }

}

public class HelloMain {
    public static void main(String [] args) throws Exception{
     ....
     mbs.registerMBean(Ret.instance(), new ObjectName("com.example.mbeans:type=RetMXBean"));
     ....
    }
}




欢迎光临 FreeOZ论坛 (https://hioz.im/ibbs/) Powered by Discuz! X3.2