Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
E
etl-ruoshui-bigdata-server
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
etl-ruoshui
etl-ruoshui-bigdata-server
Commits
25fe27c4
提交
25fe27c4
authored
6月 18, 2026
作者:
吕本才
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(ProcessUtil): 修复Linux/AIX环境进程ID获取逻辑
1. 调整注释格式统一风格 2. 新增NoSuchMethodError降级处理逻辑,兼容更多JDK版本 3. 修正缩进不规范问题 4. 优化异常处理流程
上级
7d3974a1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
33 行增加
和
9 行删除
+33
-9
ProcessUtil.java
...core/src/main/java/com/ruoshui/core/util/ProcessUtil.java
+33
-9
没有找到文件。
ruoshui-core/src/main/java/com/ruoshui/core/util/ProcessUtil.java
浏览文件 @
25fe27c4
...
@@ -40,31 +40,55 @@ public class ProcessUtil {
...
@@ -40,31 +40,55 @@ 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 ,不兼容当前的模式
// 服务器上的jdk版本大于8 ,不兼容当前的模式
String
processName
=
process
.
getClass
().
getName
();
String
processName
=
process
.
getClass
().
getName
();
logger
.
info
(
"processName: {}"
,
processName
);
logger
.
info
(
"processName: {}"
,
processName
);
if
(
processName
.
equals
(
"java.lang.UNIXProcess"
))
{
if
(
processName
.
equals
(
"java.lang.UNIXProcess"
))
{
try
{
try
{
// 仅Java 8兼容:保留原逻辑(可选)
// 仅Java 8兼容:保留原逻辑(可选)
Field
pidField
=
process
.
getClass
().
getDeclaredField
(
"pid"
);
Field
pidField
=
process
.
getClass
().
getDeclaredField
(
"pid"
);
pidField
.
setAccessible
(
true
);
pidField
.
setAccessible
(
true
);
pid
=
pidField
.
getLong
(
process
);
pid
=
pidField
.
getLong
(
process
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
error
(
"jdk8-erro-get process id for unix error {0}"
,
e
);
throw
new
RuntimeException
(
"获取进程ID失败"
,
e
);
throw
new
RuntimeException
(
"获取进程ID失败"
,
e
);
}
}
}
else
if
(
process
instanceof
ProcessHandle
)
{
}
else
if
(
process
instanceof
ProcessHandle
)
{
logger
.
error
(
"java9+process instanceof ProcessHandle "
);
// Java 9+ 推荐方式:ProcessHandle API
// Java 9+ 推荐方式:ProcessHandle API
pid
=
((
ProcessHandle
)
process
).
pid
();
pid
=
((
ProcessHandle
)
process
).
pid
();
}
else
{
}
else
{
throw
new
UnsupportedOperationException
(
"不支持当前JDK的进程ID获取"
);
logger
.
error
(
"jdk8 换方法执行"
);
Class
<?>
clazz
=
Class
.
forName
(
"java.lang.UNIXProcess"
);
Field
declaredField
=
clazz
.
getDeclaredField
(
"pid"
);
declaredField
.
setAccessible
(
true
);
pid
=
declaredField
.
getInt
(
process
);
// throw new UnsupportedOperationException("不支持当前JDK的进程ID获取");
}
}
}
catch
(
NoSuchMethodError
e
)
{
// 降级走 JDK 8 反射逻辑
try
{
Class
<?>
clazz
=
Class
.
forName
(
"java.lang.UNIXProcess"
);
Field
declaredField
=
clazz
.
getDeclaredField
(
"pid"
);
declaredField
.
setAccessible
(
true
);
pid
=
declaredField
.
getInt
(
process
);
}
catch
(
Exception
ex
)
{
// 最终容错,返回-1不中断任务
pid
=
-
1
;
logger
.
error
(
"get process id for unix error {0}"
,
e
);
// 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 "
,
e
);
throw
new
UnsupportedOperationException
(
"不支持当前JDK的进程ID获取"
);
}
}
}
}
return
String
.
valueOf
(
pid
);
return
String
.
valueOf
(
pid
);
...
@@ -89,7 +113,7 @@ public class ProcessUtil {
...
@@ -89,7 +113,7 @@ public class ProcessUtil {
command
=
"kill "
+
pid
;
command
=
"kill "
+
pid
;
}
}
try
{
try
{
//杀掉进程
//
杀掉进程
process
=
Runtime
.
getRuntime
().
exec
(
command
);
process
=
Runtime
.
getRuntime
().
exec
(
command
);
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
(),
StandardCharsets
.
UTF_8
));
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
(),
StandardCharsets
.
UTF_8
));
String
line
;
String
line
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论