add: 添加获取当前进程号的帮助方法

This commit is contained in:
Wendal Chen 2018-02-06 15:32:48 +08:00
parent bde173610e
commit 0da2b0c9f4

View 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;
}
}