8234484: Add ability to configure third port for remote JMX

Reviewed-by: dfuchs
This commit is contained in:
Dongbo He 2020-01-15 16:00:01 +08:00 committed by Fei Yang
parent 529587547c
commit f129cc4328
3 changed files with 34 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -55,6 +55,8 @@ public class AgentConfigurationError extends Error {
"agent.err.invalid.jmxremote.port";
public static final String INVALID_JMXREMOTE_RMI_PORT =
"agent.err.invalid.jmxremote.rmi.port";
public static final String INVALID_JMXREMOTE_LOCAL_PORT =
"agent.err.invalid.jmxremote.local.port";
public static final String PASSWORD_FILE_NOT_SET =
"agent.err.password.file.notset";
public static final String PASSWORD_FILE_NOT_READABLE =

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -116,6 +116,8 @@ public final class ConnectorBootstrap {
"com.sun.management.jmxremote.host";
public static final String RMI_PORT =
"com.sun.management.jmxremote.rmi.port";
public static final String LOCAL_PORT =
"com.sun.management.jmxremote.local.port";
public static final String CONFIG_FILE_NAME =
"com.sun.management.config.file";
public static final String USE_LOCAL_ONLY =
@ -540,13 +542,35 @@ public final class ConnectorBootstrap {
}
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Properties props = null;
try {
JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0);
// Do we accept connections from local interfaces only?
Properties props = Agent.getManagementProperties();
if (props == null) {
props = Agent.getManagementProperties();
if (props == null) {
props = new Properties();
}
} catch (Exception e) {
throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
}
// User can specify a port to be used to start local connector server.
// Random one will be allocated if port is not specified.
int localPort = 0;
String localPortStr = props.getProperty(PropertyNames.LOCAL_PORT);
try {
if (localPortStr != null) {
localPort = Integer.parseInt(localPortStr);
}
} catch (NumberFormatException x) {
throw new AgentConfigurationError(INVALID_JMXREMOTE_LOCAL_PORT, x, localPortStr);
}
if (localPort < 0) {
throw new AgentConfigurationError(INVALID_JMXREMOTE_LOCAL_PORT, localPortStr);
}
try {
JMXServiceURL url = new JMXServiceURL("rmi", localhost, localPort);
// Do we accept connections from local interfaces only?
String useLocalOnlyStr = props.getProperty(
PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY);
boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue();

View File

@ -25,6 +25,8 @@
# For setting the JMX RMI agent port use the following line
# com.sun.management.jmxremote.port=<port-number>
#
# For setting the JMX local server port use the following line
# com.sun.management.jmxremote.local.port=<port-number>
#####################################################################
# Optional Instrumentation