NIFI-5175 Updated NiFi compiled on Java 1.8 to run on Java 9

The bootstrap process (RunNiFi) detects Java 9 and adds "--add-modules=java.xml.bind" to the command to start NiFi
Updated OSUtils to detect Java 9 and reflectively invoke the Process.pid() method to get the PID of the NiFi process
Added java debug variable to nifi.sh to allow debugging of the bootstrap process (RunNiFi)

This closes #2708

Signed-off-by: Mike Thomsen <mikerthomsen@gmail.com>
This commit is contained in:
Jeff Storck 2018-02-12 15:58:35 -05:00 committed by Mike Thomsen
parent 775cf42560
commit 807e1e5c7a
3 changed files with 40 additions and 5 deletions

View File

@ -1032,6 +1032,10 @@ public class RunNiFi {
cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
cmd.add("-Dapp=NiFi");
cmd.add("-Dorg.apache.nifi.bootstrap.config.log.dir=" + nifiLogDir);
if (!System.getProperty("java.version").startsWith("1.")) {
// running on Java 9+, java.xml.bind module must be made available
cmd.add("--add-modules=java.xml.bind");
}
cmd.add("org.apache.nifi.NiFi");
if (isSensitiveKeyPresent(props)) {
Path sensitiveKeyFile = createSensitiveKeyFile(confDir);

View File

@ -18,6 +18,8 @@
package org.apache.nifi.bootstrap.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import com.sun.jna.Pointer;
@ -94,14 +96,40 @@ public final class OSUtils {
* Purpose for the Logger is to log any interaction for debugging.
*/
public static Long getProcessId(final Process process, final Logger logger) {
if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
return getUnicesPid(process, logger);
/*
* NIFI-5175: NiFi built with Java 1.8 and running on Java 9. Reflectively invoke Process.pid() on
* the given process instance to get the PID of this Java process. Reflection is required in this scenario
* due to NiFi being compiled on Java 1.8, which does not have the Process API improvements available in
* Java 9.
*
* Otherwise, if NiFi is running on Java 1.8, attempt to get PID using capabilities available on Java 1.8.
*
* TODO: When minimum Java version updated to Java 9+, this class should be removed with the addition
* of the pid method to the Process API.
*/
Long pid = null;
if (!System.getProperty("java.version").startsWith("1.")) {
try {
Method pidMethod = process.getClass().getMethod("pid");
pidMethod.setAccessible(true);
Object pidMethodResult = pidMethod.invoke(process);
if (Long.class.isAssignableFrom(pidMethodResult.getClass())) {
pid = (Long) pidMethodResult;
} else {
logger.debug("Could not determine PID for child process because returned PID was not " +
"assignable to type " + Long.class.getName());
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
logger.debug("Could not find PID for child process due to {}", e);
}
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
pid = getUnicesPid(process, logger);
} else if (process.getClass().getName().equals("java.lang.Win32Process")
|| process.getClass().getName().equals("java.lang.ProcessImpl")) {
return getWindowsProcessId(process, logger);
pid = getWindowsProcessId(process, logger);
}
return null;
return pid;
}
}

View File

@ -303,9 +303,12 @@ run() {
BOOTSTRAP_PID_PARAMS="-Dorg.apache.nifi.bootstrap.config.pid.dir='${NIFI_PID_DIR}'"
BOOTSTRAP_CONF_PARAMS="-Dorg.apache.nifi.bootstrap.config.file='${BOOTSTRAP_CONF}'"
# uncomment to allow debugging of the bootstrap process
#BOOTSTRAP_DEBUG_PARAMS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
BOOTSTRAP_DIR_PARAMS="${BOOTSTRAP_LOG_PARAMS} ${BOOTSTRAP_PID_PARAMS} ${BOOTSTRAP_CONF_PARAMS}"
run_nifi_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} org.apache.nifi.bootstrap.RunNiFi $@"
run_nifi_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} ${BOOTSTRAP_DEBUG_PARAMS} org.apache.nifi.bootstrap.RunNiFi $@"
if [ -n "${run_as_user}" ]; then
# Provide SCRIPT_DIR and execute nifi-env for the run.as user command