jmx实现代码


package test.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent {
private MBeanServer mbs = null;

public HelloAgent() {
//create a MBeanServer
mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
//create an adapter
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
//create a MBean
HelloWorld hw = new HelloWorld("hello boys!");

ObjectName adapterName = null;
ObjectName helloWorldName = null;

try {
adapterName = new ObjectName(
"HelloAgentW:name=htmladaffpter,port=9092");
//regisetr the adapter to the MBeanServer
mbs.registerMBean(adapter, adapterName);
//declare the port which the adapter user
adapter.setPort(9092);
//start the adapter
adapter.start();

helloWorldName = new ObjectName("HelloAgentW:name=hffelloWorld1");
mbs.registerMBean(hw, helloWorldName);

} catch (Exception e) {
e.printStackTrace();
}

}

public static void main(String args[]) {
//declare the agent and start the adapter
HelloAgent agent = new HelloAgent();

}

}

package test.jmx;

public class HelloWorld implements HelloWorldMBean {
public String hello;

public HelloWorld() {
this.hello = "Hello World! I am a Standard MBean";
}

public HelloWorld(String hello) {
this.hello = hello;
}


public String getHello() {
return hello;
}

public void setHello(String hello) {
this.hello = hello;
}

public void sayHello() {
System.out.println(hello);
}
}

package test.jmx;

public interface HelloWorldMBean {
public void sayHello();
public void setHello(String hello);
public String getHello();

}

package test.jmx;

import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.RmiConnectorServer;

public class RMIAgent {

public static void main(String[] args) {
MBeanServer mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
RmiConnectorServer connector = new RmiConnectorServer();
ObjectName connectorName = null;
try {
connectorName = new ObjectName("HelloAgent:name=RMIConnector");
mbs.registerMBean(connector, connectorName);
HelloWorld hw = new HelloWorld("hello boys!");
ObjectName helloWorldName = new ObjectName(
"HelloAgent:name=helloWorld1");

mbs.registerMBean(hw, helloWorldName);
connector.start();
} catch (Exception e) {
e.printStackTrace();
}

}

}

package test.jmx;

import javax.management.Attribute;
import javax.management.ObjectName;

import com.sun.jdmk.comm.RmiConnectorAddress;
import com.sun.jdmk.comm.RmiConnectorClient;

public class RMIManager {

public static void main(String[] args) {
RmiConnectorClient client = new RmiConnectorClient();
RmiConnectorAddress address = new RmiConnectorAddress();
try {
client.connect(address);
ObjectName helloWorldName = ObjectName
.getInstance("HelloAgent:name=helloWorld1");
client.invoke(helloWorldName, "sayHello", null, null);
client.setAttribute(helloWorldName, new Attribute("Hello",
new String("hello girls!")));
client.invoke(helloWorldName, "sayHello", null, null);
} catch (Exception e) {
e.printStackTrace();
} finally {

}
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值