JMX Connectors
JMX有两种connector:
1、 RMI Connector
2、 JMXMP Connector (JMX message protocol Connector)
RMI Connector支持RMI Transports , Java Remote Method Protocol (JRMP) 和 Inter-Object Request Broker Protocol (IIOP)
下面的例子将利用 RMI Connector 在server 和client 之间建立连接 。
server的主要功能:
1、 建立 Mbean server
2、 建立 RMI connector server
Client 的主要功能:
1、 建立 RMI connector
2、 通过 RMI connector 在Mbean server 注册一个 Mbean
所包含的文件:
Server.java
SimpleStandardMBean.java
SimpleStandard.java
ClientListener.java
Client.java
运行环境:
首先去sun下载两个压缩包:
1、jmx-1_2_1-bin
2、jmxremote-1_0_1-bin
以下程序需要这两个包里面的lib/*.jar
源程序:
Server.java
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
public class Server {
public static void main(String[] args) {
try {
// Instantiate the MBean server
//
System.out.println("/nCreate the MBean server");
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Create an RMI connector server
//
System.out.println("/nCreate an RMI connector server");
JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://localhost:9999/server");
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(
url, //url
null, //environment map
mbs); //MBeanServer
// Start the RMI connector server
//
System.out.println("/nStart the RMI connector server");
cs.start();
System.out.println("/nRMI connector server successfully started");
System.out.println("/nWaiting for incoming connections...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
为了便于观察Server的情况,可以在Server.java 加入 htmlAdapterServer 通过浏览器察看,具体情况为在 MBeanServer mbs = MBeanServerFactory.createMBeanServer();下面添加代码:可能这么说比较不清楚,直接把全部代码写出来吧,虽然比较多的代码让人看的不舒服了 : )
///更改后的Server.java
Server.java
/*
* Created on 2004-12-25
* @author roson
*/
import javax.management.*;
import javax.management.MBeanServerFactory;
import javax.management.remote.*;
import jmxInAction.Logger;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class Server {
public static void main(String[] args) {
try
{
System.out.println("Create the MBean server");
MBeanServer mbs=MBeanServerFactory.createMBeanServer();
System.out.println("/n/tCREATE, REGISTER and START a new HTML adaptor:");
HtmlAdaptorServer html = new HtmlAdaptorServer();
ObjectName html_name = null;
try
{
html_name = new ObjectName("Logger:name=html,port=8082");
System.out.println("/tOBJECT NAME = " + html_name);
mbs.registerMBean(html, html_name);
}
catch(Exception e)
{
System.out.println("/t!!! Could not create the HTML adaptor !!!");
e.printStackTrace();
return;
}
html.start();
System.out.println("Create an RMI Connector server");
JMXServiceURL url=new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
//JMXServiceURL url=new JMXServiceURL("jmxmp",null,5555);
// jmxmp与rmi connector 不同的地方
JMXConnectorServer cs=JMXConnectorServerFactory.newJMXConnectorServer(
url, //url
null, //environment map
mbs); //MBeanServer
cs.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 更改后的Server.java ^^^^^^^^^^^^^^^^^^^^^^^^^^
更改后就可以通过浏览器来察看Server的情况了。http://localhost:8082 为浏览器地址栏
SimpleStandardMBean.java
public interface SimpleStandardMBean {
public String getState(); //读状态State
public void setState(String s); //写状态State
public int getNbChanges(); //状态更改次数
public void reset(); //重至
}
SimpleStandard.java
import javax.management.*;
public class SimpleStandard extends NotificationBroadcasterSupport
implements SimpleStandardMBean{
private String state="initial state";
private int nbChanges=0;
private int nbResets=0;
public String getState() {
return state;
}
public void setState(String state) {
this.state=state;
nbChanges++;
}
public int getNbChanges() {
return nbChanges;
}
public void reset() {
AttributeChangeNotification acn=new AttributeChangeNotification(
this, //source
0, //sequenceNumber
0, //timeStamp
"NbChanges reset", //message
"NbChanges", //属性名
"Integer", //属性的类型
new Integer(nbChanges), //属性更改前的值
new Integer(0)); //属性更改后的值
state="initial state";
nbChanges=0;
nbResets++;
sendNotification(acn);
}
}
ClientListener.java
import javax.management.*;
public class ClientListener implements NotificationListener{
public void handleNotification(Notification notification, Object handback) {
System.out.println("/nReceived notification:"+notification);
}
}
Client.java
import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class Client {
public static void main(String[] args) {
try {
// Create an RMI connector client and
// connect it to the RMI connector server
//
System.out.println("/nCreate an RMI connector client and " +
"connect it to the RMI connector server");
JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://localhost:9999/server");
//JMXServiceURL url=new JMXServiceURL("jmxmp",null,5555);
//jmxmp connector 与 rmi connector不同的地方,此时jmxp connector的端口号为5555
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
// Get an MBeanServerConnection
//
System.out.println("/nGet an MBeanServerConnection");
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
// Get domains from MBeanServer
//
System.out.println("/nDomains:");
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("/tDomain[" + i + "] = " + domains[i]);
}
// Create SimpleStandard Mbean 注册了个Mbean
//
ObjectName mbeanName = new ObjectName("MBeans:type=SimpleStandard");
System.out.println("/nCreate SimpleStandard MBean...");
mbsc.createMBean("SimpleStandard", mbeanName, null, null);
// Get MBean count
//
System.out.println("/nMBean count = " + mbsc.getMBeanCount());
// Get State attribute
//
System.out.println("/nState = " +
mbsc.getAttribute(mbeanName, "State"));
// Set State attribute
//
mbsc.setAttribute(mbeanName,
new Attribute("State", "changed state"));
// Get State attribute
//
// Another way of interacting with a given MBean is through a
// dedicated proxy instead of going directly through the MBean
// server connection
//
SimpleStandardMBean proxy = (SimpleStandardMBean)
MBeanServerInvocationHandler.newProxyInstance(
mbsc,
mbeanName,
SimpleStandardMBean.class,
false);
System.out.println("/nState = " + proxy.getState());
// Add notification listener on SimpleStandard MBean
//
ClientListener listener = new ClientListener();
System.out.println("/nAdd notification listener...");
mbsc.addNotificationListener(mbeanName, listener, null, null);
// Invoke "reset" in SimpleStandard MBean
//
// Calling "reset" makes the SimpleStandard MBean emit a
// notification that will be received by the registered
// ClientListener.
//
System.out.println("/nInvoke reset() in SimpleStandard MBean...");
mbsc.invoke(mbeanName, "reset", null, null);
// Sleep for 2 seconds in order to have time to receive the
// notification before removing the notification listener.
//
System.out.println("/nWaiting for notification...");
Thread.sleep(2000);
// Remove notification listener on SimpleStandard MBean
//
System.out.println("/nRemove notification listener...");
mbsc.removeNotificationListener(mbeanName, listener);
// Unregister SimpleStandard MBean
//
System.out.println("/nUnregister SimpleStandard MBean...");
mbsc.unregisterMBean(mbeanName);
// Close MBeanServer connection
//
System.out.println("/nClose the connection to the server");
jmxc.close();
System.out.println("/nBye! Bye!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
在client中注册了个Mbean 这个在命令行是看不到它所在的域名,因为是先连接Server,查看Server所有的域名,接着再注册的。不过,可以在浏览器中看到情况的变化。刷新下试试 : )
运行程序:
1、 将 运行环境 中提到的几个 jar 包放在classpath ,(我是用eclipse 来做的,觉得很方便)
2、 编译。
如果是RMI Connector 就要用 rmiregister : 在命令提示行输入:rmiregistry 9999
Jmxmp 就不需要了。
3、 运行 Server.java ,接着运行 Client.java
摘自 jmxremote-1_0_1-bin/doc/tutorial