JMX connector
remote Java client 通过connector来访问MBean,connector的client side的接口和MBean的接口保持一致。
两部分:
connector client : 创建与server之间的连接。通常处于和server不同的JVM上,或处于不同的机器上。
connector server : 与MBean Server一起,监听来自client的请求。
connection protocol是基于RMI的 : JMX client远程连接到MBeanServer,操作MBean,就像操作本地的对象一样。
out-of-the-box RMI connector
JavaSE提供了out-of-the-box RMI connector,用户就不需要自定义特定的connector了。
通过特定的Java启动参数来启动程序,就可以启动程序的out-of-the-box management agent。
如此,实现了JMX technology的远程程序就可以连接到程序并进程操作,比如JConsole。
对于前面的Standard MBean,只需启动时加上参数:
-Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false注: 此为测试程序,因此没有authenticate。
使用JConsole:
前面说的测试都是用JRE自带的JConsole,下面来编写自己的Client来访问MBean。
package test.xue.mbean.client; import java.io.IOException; import java.util.Set; import javax.management.JMX; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import test.xue.mbean.HelloMBean; public class MBeanClient { public static void main(String[] args) throws IOException, MalformedObjectNameException, NullPointerException { JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://192.168.1.102:9999/jmxrmi"); JMXConnector connector = JMXConnectorFactory.connect(url); MBeanServerConnection connection = connector.getMBeanServerConnection(); // list domains String[] domains = connection.getDomains(); for (String domain : domains) { System.out.println("domain : " + domain); } // list ObjectNames Set<ObjectName> names = connection.queryNames(null, null); for (ObjectName name : names) { System.out.println("ObjectName : " + name); } // remote operation ObjectName name = new ObjectName("test.xue.mbean:type=Hello"); HelloMBean mbean = JMX.newMBeanProxy(connection, name, HelloMBean.class); mbean.sayHello(); System.out.println("1 + 2 ="+mbean.add(1, 2)); // close connection connector.close(); } }
domain : JMImplementation domain : com.sun.management domain : java.lang domain : test.xue.mbean domain : java.util.logging ObjectName : java.lang:type=Memory ObjectName : java.lang:type=GarbageCollector,name=Copy ObjectName : test.xue.mbean:type=Hello ObjectName : java.lang:type=MemoryPool,name=Code Cache ObjectName : java.lang:type=Runtime ObjectName : java.lang:type=ClassLoading ObjectName : java.lang:type=MemoryPool,name=Perm Gen [shared-rw] ObjectName : java.lang:type=Threading ObjectName : java.lang:type=MemoryPool,name=Perm Gen [shared-ro] ObjectName : java.util.logging:type=Logging ObjectName : java.lang:type=Compilation ObjectName : java.lang:type=MemoryPool,name=Eden Space ObjectName : com.sun.management:type=HotSpotDiagnostic ObjectName : java.lang:type=GarbageCollector,name=MarkSweepCompact ObjectName : java.lang:type=MemoryPool,name=Survivor Space ObjectName : java.lang:type=OperatingSystem ObjectName : java.lang:type=MemoryPool,name=Perm Gen ObjectName : java.lang:type=MemoryPool,name=Tenured Gen ObjectName : java.lang:type=MemoryManager,name=CodeCacheManager ObjectName : JMImplementation:type=MBeanServerDelegate 1 + 2 =3