JConsole behind

A recent monitoring-related things, all put together local JAVA application management, but these applications JDK1.4, there are 1.5, 1.6, and before the application of JDK1.5 provided MBeanServer not PlatformMBeanServer, which time lead to some property to get information when monitoring the heap, threads, Runtime, etc. can not get. This transformation plan to run on JDK1.4 running application so that it uses PlatFormMBeanServer, but JDK1.4 does not seem to provide a way to get PlatFormMBeanServer, roughly think of the following ideas:
1. Upgrade JDK, but may affect the original application because when some of the syntax of JDK1.4 enum like these are not keywords.
2. If you can upgrade the JDK, the following points can achieve:
2.1 transformation of the original application, access PlatFormMBeanServer, so that the application of monitoring data and monitoring data on java.lang can get
2.2 using the JDK attach the way, according to local vmid gets its jmxUrl, and then get the properties, the need JDK1.6 up.

Attach code portion JDK:

// load the management agent into the target VM
private void loadManagementAgent() throws IOException {
VirtualMachine vm = null;
String name = String.valueOf(vmid);
try {
vm = VirtualMachine.attach(name);
} catch (AttachNotSupportedException x) {
IOException ioe = new IOException(x.getMessage());
ioe.initCause(x);
throw ioe;
}

String home = vm.getSystemProperties().getProperty("java.home");

// Normally in ${java.home}/jre/lib/management-agent.jar but might
// be in ${java.home}/lib in build environments.

String agent = home + File.separator + "jre" + File.separator +
"lib" + File.separator + "management-agent.jar";
File f = new File(agent);
if (!f.exists()) {
agent = home + File.separator + "lib" + File.separator +
"management-agent.jar";
f = new File(agent);
if (!f.exists()) {
throw new IOException("Management agent not found");
}
}

agent = f.getCanonicalPath();
try {
vm.loadAgent(agent, "com.sun.management.jmxremote");
} catch (AgentLoadException x) {
IOException ioe = new IOException(x.getMessage());
ioe.initCause(x);
throw ioe;
} catch (AgentInitializationException x) {
IOException ioe = new IOException(x.getMessage());
ioe.initCause(x);
throw ioe;
}

// get the connector address
Properties agentProps = vm.getAgentProperties();
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

vm.detach();
}

InputStream execute(String cmd, Object ... args)
throws AgentLoadException, IOException
{
assert args.length <= 3; // includes null

// create a pipe using a random name
int r = (new Random()).nextInt();
String pipename = "\\\\.\\pipe\\javatool" + r;
long hPipe = createPipe(pipename);

// check if we are detached - in theory it's possible that detach is invoked
// after this check but before we enqueue the command.
if (hProcess == -1) {
closePipe(hPipe);
throw new IOException("Detached from target VM");
}

try {
// enqueue the command to the process
enqueue(hProcess, stub, cmd, pipename, args);

// wait for command to complete - process will connect with the
// com
Published 56 original articles · won praise 0 · Views 7780

Guess you like

Origin blog.csdn.net/chainhou/article/details/84444737