JavaScript implementation: how to write beautiful conditional expression>>>
Project requirements: through JMX monitoring zookeeper, the effect is similar to jconsole, but presented in the form of web. In the process of using, when JMX in the web container connects to mbeanserver to obtain the connection, it always fails. The prompt message is: Java. Lang. classnotfoundexception: org. Apache. Naming. Java. Javaurlcontextfactory. But it can be accessed normally when calling through the main method
The code is as follows:
String jmxURL = "service:jmx:rmi:///jndi/rmi://" + ip + ":" + jmxport + "/jmxrmi";
// jmxurl
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
Map<String, String[]> map = new HashMap<String, String[]>();
String[] credentials = new String[] { userName, password };
map.put("jmx.remote.credentials", credentials);
connector = JMXConnectorFactory.connect(serviceURL, map);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
After searching for information from the Internet, from an article( http://canofy.iteye.com/blog/758748 )The method to get mbeanserverconnection is modified, and then it can run normally. The modified code is as follows:
Registry registry = LocateRegistry.getRegistry(host, Integer.parseInt(port));
RMIServer stub = null;
if (stub == null) {
stub = (RMIServer) registry.lookup("jmxrmi");
}
Map<String, String[]> map = new HashMap<String, String[]>();
String[] credentials = new String[] { username, password };
map.put("jmx.remote.credentials", credentials);
connector = new RMIConnector(stub, null);
connector.connect(map);
mbsc = connector.getMBeanServerConnection();
There are two ways to get mbeanserverconnection. In general, both ways are OK. For some special applications, API means that the second method is best used