提交 3ad56ca2 authored 作者: 吕本才's avatar 吕本才

修改获取pid的jdk版本

上级 d5f654d0
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
<artifactId>ruoshui-core</artifactId> <artifactId>ruoshui-core</artifactId>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
...@@ -134,11 +134,11 @@ ...@@ -134,11 +134,11 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<source>${java.version}</source> <source>9</source>
<target>${java.version}</target> <target>9</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
...@@ -10,6 +10,8 @@ import org.slf4j.LoggerFactory; ...@@ -10,6 +10,8 @@ import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
...@@ -38,10 +40,27 @@ public class ProcessUtil { ...@@ -38,10 +40,27 @@ public class ProcessUtil {
} }
} else if (Platform.isLinux() || Platform.isAIX()) { } else if (Platform.isLinux() || Platform.isAIX()) {
try { try {
Class<?> clazz = Class.forName("java.lang.UNIXProcess"); // Class<?> clazz = Class.forName("java.lang.UNIXProcess");
field = clazz.getDeclaredField("pid"); // field = clazz.getDeclaredField("pid");
field.setAccessible(true); // field.setAccessible(true);
pid = (Integer) field.get(process); // pid = (Integer) field.get(process);
// 服务器上的jdk版本大于8 ,不兼容当前的模式
if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
try {
// 仅Java 8兼容:保留原逻辑(可选)
Field pidField = process.getClass().getDeclaredField("pid");
pidField.setAccessible(true);
pid = pidField.getLong(process);
} catch (Exception e) {
throw new RuntimeException("获取进程ID失败", e);
}
} else if (process instanceof ProcessHandle) {
// Java 9+ 推荐方式:ProcessHandle API
pid = ((ProcessHandle) process).pid();
} else {
throw new UnsupportedOperationException("不支持当前JDK的进程ID获取");
}
} catch (Throwable e) { } catch (Throwable e) {
logger.error("get process id for unix error {0}", e); logger.error("get process id for unix error {0}", e);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论