Skip to content

Instantly share code, notes, and snippets.

@ncdc
Created October 18, 2013 19:43
Show Gist options
  • Save ncdc/7047017 to your computer and use it in GitHub Desktop.
Save ncdc/7047017 to your computer and use it in GitHub Desktop.
VirtualMachine vm = VirtualMachine.attach("7851");
String javaHome = vm.getSystemProperties().getProperty("java.home");
String agentJar = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
vm.loadAgent(agentJar, "com.sun.management.jmxremote");
String localConnectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (localConnectorAddress == null) {
// Check system properties
localConnectorAddress = vm.getSystemProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
vm.detach();
JMXServiceURL jmxURL = new JMXServiceURL(localConnectorAddress);
JMXConnector connector = JMXConnectorFactory.connect(jmxURL);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName hostObjectName = new ObjectName("Catalina:type=Host,host=localhost");
mbsc.invoke(hostObjectName, "stop", null, null);
mbsc.invoke(hostObjectName, "start", null, null);
connector.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment