找回密码
 FreeOZ用户注册
查看: 1616|回复: 7
打印 上一主题 下一主题

JMX Works as RPC Protocol

[复制链接]
跳转到指定楼层
1#
发表于 19-10-2009 14:34:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
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
回复  

使用道具 举报

2#
 楼主| 发表于 19-10-2009 14:41:02 | 只看该作者

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.
回复  

使用道具 举报

3#
 楼主| 发表于 19-10-2009 14:50:01 | 只看该作者

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.
回复  

使用道具 举报

4#
 楼主| 发表于 19-10-2009 14:55:36 | 只看该作者

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();
回复  

使用道具 举报

5#
 楼主| 发表于 19-10-2009 15:01:17 | 只看该作者

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.
回复  

使用道具 举报

6#
发表于 19-10-2009 15:19:33 | 只看该作者
提示: 作者被禁止或删除, 无法发言
这个是用来管理java软件的那个吧。好像tomcat可以用这个控制来着。可以开发一系列产品然后用一个软件console管理这些进程。
唉,现在总觉得对工作来说,似乎没太大意义搞这些比较精巧但是费时间的东西,特别小组里别人都没什么兴趣。

[ 本帖最后由 black_zerg 于 19-10-2009 15:21 编辑 ]
回复  

使用道具 举报

7#
 楼主| 发表于 19-10-2009 15:53:00 | 只看该作者

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.
回复  

使用道具 举报

8#
 楼主| 发表于 19-10-2009 16:13:06 | 只看该作者

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用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 2-11-2024 06:32 , Processed in 0.017975 second(s), 24 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表