jmx实现代码
程序员文章站
2024-03-14 13:50:52
...
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 {
}
}
}
上一篇: python 对png图片压缩
下一篇: Lwuit实现九宫图特效程序