最近想做一个通用,可扩展的数据采集框架,想把这个框架构建在JMX上,想选用MX4j,结果居然在jdk1.6下运行examples会提示类找不到,明明这个类是存在,比如下面的例子会提示mx4j.tools.naming.NamingService这个类找不到,郁闷!搞了一下午,后来好不容易才找到原来mx4j跟jdk1.6有一些不兼容,解决办法是运行虚拟机时候加入参数,意思是用自定义的MBServer替换默认的
“-Djavax.management.builder.initial=mx4j.server.MX4JMBeanServerBuilder"。
package mx4j.examples.remote.simple;
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;
/**
* This example shows the simplest way to setup a JSR 160 connector server.
* It uses the standard JSR 160 RMIConnectorServer, and if you're familiar with
* RMI, you'll know that a JNDI server like the rmiregistry is needed
* in order to register the server stub that will be looked up by the client.
*
* @version $Revision: 1.3 $
*/
public class Server
{
public static void main(String[] args) throws Exception
{
// The MBeanServer
MBeanServer server = MBeanServerFactory.createMBeanServer();
// Register and start the rmiregistry MBean, needed by JSR 160 RMIConnectorServer
ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry");
server.createMBean("mx4j.tools.naming.NamingService", namingName, null);
server.invoke(namingName, "start", null, null);
int namingPort = ((Integer)server.getAttribute(namingName, "Port")).intValue();
String jndiPath = "/jmxconnector";
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort + jndiPath);
// Create and start the RMIConnectorServer
JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
connectorServer.start();
System.out.println("Server up and running");
}
}