JMX简单示例

以下文章摘自网络仅供学习用

1.编写一个以MBean结尾的接口SimpleStandardMBean:

public interface SimpleStandardMBean {

public void setName(String name);

public String getName(String name);

public void reSet();

public int getAge();
}

2.编写实现类SimpleStandard:

public class SimpleStandard implements SimpleStandardMBean{

public String name;
public int getAge() {
return 24;
}

public String getName(String name) {
this.name=name;
System.out.println("----------调用了getName方法----------");
return this.name;
}

public void reSet() {
System.out.println("----------reSet()-----------");
}

public void setName(String name) {
this.name=name;
}

public void aaa(){
System.out.println("888888888888888888888");
}

}

3.编写服务端,把SimpleStandard注册到MBean中:

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class Server {

public static void main(String[] args) {

MBeanServer mbServer=MBeanServerFactory.createMBeanServer();
System.out.println("------MBean服务器创建成功--------");
String defaultDomain=mbServer.getDefaultDomain();
SimpleStandard simpleStandard=new SimpleStandard();
ObjectName objectname=null;
try {
objectname=new ObjectName(defaultDomain+":name=guyong");
mbServer.registerMBean(simpleStandard, objectname);
System.out.println("-------向MBean注册成功--------");
} catch (Exception e) {
e.printStackTrace();
}

/**
* 创建一个jmx连接服务器
*/
try {
JMXServiceURL jmxurl=new JMXServiceURL("service:jmx:jms:///tcp://192.9.200.200:61616");
Map<String ,String> map=new HashMap();
map.put("jmx.remote.protocol.provider.pkgs", "org.logicblaze.lingo.jmx.remote.provider");
JMXConnectorServer jmxConServer=JMXConnectorServerFactory.newJMXConnectorServer(jmxurl, map, mbServer);
System.out.println("--------JMX连接服务器创建成功--------");
System.out.println("--------启动JMX服务----------");
jmxConServer.start();
System.out.println("--------启动成功-------------");
} catch (Exception e) {
e.printStackTrace();
}

}
}

4.编写客户端,调用服务端注册的类中的方法:

import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanServerConnection;
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 {
//创建jmx连接
JMXServiceURL jmxUrl=new JMXServiceURL("service:jmx:jms:///tcp://192.9.200.200:61616");
Map<String ,String> map=new HashMap();
map.put("jmx.remote.protocol.provider.pkgs", "org.logicblaze.lingo.jmx.remote.provider");
JMXConnector jmxConn=JMXConnectorFactory.connect(jmxUrl,map);
//获得MBean服务连接
MBeanServerConnection mbsConn=jmxConn.getMBeanServerConnection();
String defaultDedomain=mbsConn.getDefaultDomain();
ObjectName objname=null;
objname=new ObjectName(defaultDedomain + ":name=guyong");
Object[] para={"guyong"};//参数名
String[] paraType={"java.lang.String"};//参数类型
Object str=mbsConn.invoke(objname, "getName", para, paraType);//调用服务端的方法
System.out.println("name=" + str.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(jmx)