mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-12-02 03:38:08 +08:00
add: 添加获取当前进程号的帮助方法
This commit is contained in:
parent
bde173610e
commit
0da2b0c9f4
25
nutzboot-core/src/main/java/org/nutz/boot/tools/NbTools.java
Normal file
25
nutzboot-core/src/main/java/org/nutz/boot/tools/NbTools.java
Normal file
@ -0,0 +1,25 @@
|
||||
package org.nutz.boot.tools;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class NbTools {
|
||||
|
||||
/**
|
||||
* 获取进程id
|
||||
* @param fallback 如果获取失败,返回什么呢?
|
||||
* @return 进程id
|
||||
*/
|
||||
public static String getProcessId(final String fallback) {
|
||||
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
final int index = jvmName.indexOf('@');
|
||||
if (index < 1) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
return Long.toString(Long.parseLong(jvmName.substring(0, index)));
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user