Skip to content

Commit

Permalink
Update management agent logic and comments for java 7 vs 8 vs 9 (#457)
Browse files Browse the repository at this point in the history
* restructured loadjmxagent

* added version log msg

* reverted to maven target 1.7

* updated comments

* updated formatting

* updated format

* updated more formatting
  • Loading branch information
cmetz100 authored Jul 27, 2023
1 parent 9e2e39d commit 72a3923
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/main/java/org/datadog/jmxfetch/AttachApiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,32 @@ private String getJmxUrlForProcessRegex(String processRegex)
}

// management-agent.jar has been removed in java 8+
// Once JMXFetch drops java7 support, this should be simplified to simply invoke
// vm.startLocalManagementAgent
// Once JMXFetch drops java 7 support, this should be simplified to simply invoke
// vm.startLocalManagementAgent which is accessible in java 8 if tools.jar is added
// to the classpath and java 9+ by default
// ref https://bugs.openjdk.org/browse/JDK-8179063
private void loadJmxAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException {
String agent =
vm.getSystemProperties().getProperty("java.home")
+ File.separator
+ "lib"
+ File.separator
+ "management-agent.jar";
try {
vm.loadAgent(agent);
} catch (Exception e) {
log.warn("Error initializing JMX agent from management-agent.jar", e);

Method method = com.sun.tools.attach.VirtualMachine
.class.getMethod("startLocalManagementAgent");
log.info("Found startLocalManagementAgent API, attempting to use it.");
method.invoke(vm);
} catch (NoSuchMethodException noMethodE) {
log.warn("startLocalManagementAgent method not found, must be on java 7", noMethodE);
String agent = vm.getSystemProperties().getProperty("java.home")
+ File.separator
+ "lib"
+ File.separator
+ "management-agent.jar";
try {
Method method = com.sun.tools.attach.VirtualMachine
.class.getMethod("startLocalManagementAgent");
log.info("Found startLocalManagementAgent API, attempting to use it.");
method.invoke(vm);
} catch (NoSuchMethodException noMethodE) {
log.warn("startLocalManagementAgent method not found, must be on java7", noMethodE);
} catch (Exception reflectionE) {
log.warn("Error invoking the startLocalManagementAgent method", reflectionE);
vm.loadAgent(agent);
} catch (Exception e) {
log.warn("Error initializing JMX agent from management-agent.jar", e);
}
} catch (Exception reflectionE) {
log.warn("Error invoking the startLocalManagementAgent method", reflectionE);
}


}
}
2 changes: 2 additions & 0 deletions src/main/java/org/datadog/jmxfetch/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static Connection createConnection(Map<String, Object> connectionParams)

if (connectionParams.get(PROCESS_NAME_REGEX) != null) {
try {
// AttachNotSupportedException is accessible in java 7 and 8 through tools.jar
// and java 9+ by default
Class.forName("com.sun.tools.attach.AttachNotSupportedException");
} catch (ClassNotFoundException e) {
throw new IOException(
Expand Down

0 comments on commit 72a3923

Please sign in to comment.