public static void jmx() {
String ip = "127.0.0.1";
int port = 1090;
String user = "monitorRole";
String pw = "password";
String[] credentials = new String[] { user, pw };
Map<String, Object> environment = new HashMap<String, Object>();
environment.put("jmx.remote.credentials", credentials);
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
environment.put(Context.PROVIDER_URL, "rmi://" + ip + ":" + port);
try {
// JMXServiceURL address = new JMXServiceURL("service:jmx:rmi://" + ip + ":" + port + "/jndi/rmi://" + ip + ":" + port + "/jmxrmi");
// JMXConnector connector = JMXConnectorFactory.connect(address, environment);
// MBeanServerConnection conn = connector.getMBeanServerConnection();
Registry registry = LocateRegistry.getRegistry(ip, port);
RMIServer stub = (RMIServer) registry.lookup("jmxrmi");
JMXConnector jmxc = new RMIConnector(stub, environment);
jmxc.connect();
MBeanServerConnection conn = jmxc.getMBeanServerConnection();
ObjectName delegate = ObjectName.getInstance("Catalina:j2eeType=WebModule,name=//localhost/examples,J2EEApplication=none,J2EEServer=none");
Object workDir = conn.getAttribute(delegate, "workDir");
System.out.println(workDir);
//destroy,stop,start,init,reload
// conn.invoke(delegate, "reload", null, null);
conn.invoke(delegate, "stop", null, null);
Thread.sleep(10000);
conn.invoke(delegate, "start", null, null);
jmxc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
|