Skip to content

Commit 807e1e5

Browse files
jtstorckMikeThomsen
authored andcommitted
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>
1 parent 775cf42 commit 807e1e5

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

  • nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap
  • nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin

nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/RunNiFi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ public boolean accept(final File dir, final String filename) {
10321032
cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
10331033
cmd.add("-Dapp=NiFi");
10341034
cmd.add("-Dorg.apache.nifi.bootstrap.config.log.dir=" + nifiLogDir);
1035+
if (!System.getProperty("java.version").startsWith("1.")) {
1036+
// running on Java 9+, java.xml.bind module must be made available
1037+
cmd.add("--add-modules=java.xml.bind");
1038+
}
10351039
cmd.add("org.apache.nifi.NiFi");
10361040
if (isSensitiveKeyPresent(props)) {
10371041
Path sensitiveKeyFile = createSensitiveKeyFile(confDir);

nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/util/OSUtils.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.nifi.bootstrap.util;
1919

2020
import java.lang.reflect.Field;
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
2123

2224
import org.slf4j.Logger;
2325
import com.sun.jna.Pointer;
@@ -94,14 +96,40 @@ private static Long getWindowsProcessId(final Process process, final Logger logg
9496
* Purpose for the Logger is to log any interaction for debugging.
9597
*/
9698
public static Long getProcessId(final Process process, final Logger logger) {
97-
if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
98-
return getUnicesPid(process, logger);
99+
/*
100+
* NIFI-5175: NiFi built with Java 1.8 and running on Java 9. Reflectively invoke Process.pid() on
101+
* the given process instance to get the PID of this Java process. Reflection is required in this scenario
102+
* due to NiFi being compiled on Java 1.8, which does not have the Process API improvements available in
103+
* Java 9.
104+
*
105+
* Otherwise, if NiFi is running on Java 1.8, attempt to get PID using capabilities available on Java 1.8.
106+
*
107+
* TODO: When minimum Java version updated to Java 9+, this class should be removed with the addition
108+
* of the pid method to the Process API.
109+
*/
110+
Long pid = null;
111+
if (!System.getProperty("java.version").startsWith("1.")) {
112+
try {
113+
Method pidMethod = process.getClass().getMethod("pid");
114+
pidMethod.setAccessible(true);
115+
Object pidMethodResult = pidMethod.invoke(process);
116+
if (Long.class.isAssignableFrom(pidMethodResult.getClass())) {
117+
pid = (Long) pidMethodResult;
118+
} else {
119+
logger.debug("Could not determine PID for child process because returned PID was not " +
120+
"assignable to type " + Long.class.getName());
121+
}
122+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
123+
logger.debug("Could not find PID for child process due to {}", e);
124+
}
125+
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
126+
pid = getUnicesPid(process, logger);
99127
} else if (process.getClass().getName().equals("java.lang.Win32Process")
100128
|| process.getClass().getName().equals("java.lang.ProcessImpl")) {
101-
return getWindowsProcessId(process, logger);
129+
pid = getWindowsProcessId(process, logger);
102130
}
103131

104-
return null;
132+
return pid;
105133
}
106134

107135
}

nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,12 @@ run() {
303303
BOOTSTRAP_PID_PARAMS="-Dorg.apache.nifi.bootstrap.config.pid.dir='${NIFI_PID_DIR}'"
304304
BOOTSTRAP_CONF_PARAMS="-Dorg.apache.nifi.bootstrap.config.file='${BOOTSTRAP_CONF}'"
305305

306+
# uncomment to allow debugging of the bootstrap process
307+
#BOOTSTRAP_DEBUG_PARAMS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
308+
306309
BOOTSTRAP_DIR_PARAMS="${BOOTSTRAP_LOG_PARAMS} ${BOOTSTRAP_PID_PARAMS} ${BOOTSTRAP_CONF_PARAMS}"
307310

308-
run_nifi_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} org.apache.nifi.bootstrap.RunNiFi $@"
311+
run_nifi_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} ${BOOTSTRAP_DEBUG_PARAMS} org.apache.nifi.bootstrap.RunNiFi $@"
309312

310313
if [ -n "${run_as_user}" ]; then
311314
# Provide SCRIPT_DIR and execute nifi-env for the run.as user command

0 commit comments

Comments
 (0)