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 {
}
}
}
jmx实现代码
最新推荐文章于 2018-11-06 18:55:36 发布