分享
 
 
 

JMX Connectors

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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 源代码:jmxremote-1_0_1-bin\src\Basic\rmi\*.java

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有