mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add pid
This commit is contained in:
parent
3489cb285c
commit
ca1563ee34
@ -3,18 +3,20 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.0.M3 (2022-04-11)
|
||||
# 5.8.0.M3 (2022-04-12)
|
||||
|
||||
### ❌不兼容特性
|
||||
* 【core 】 StreamProgress#progress方法参数变更为2个(pr#594@Gitee)
|
||||
* 【core 】 SimpleCache的raw key使用Mutable
|
||||
* 【core 】 ArrayUtil.join删除已经弃用的无用原始类型重载
|
||||
* 【core 】 删除Holder类,ReUtil.extractMultiAndDelPre方法参数改为Mutable
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 CopyOptions支持以Lambda方式设置忽略属性列表(pr#590@Gitee)
|
||||
* 【core 】 增加中文姓名正则及其校验(pr#592@Gitee)
|
||||
* 【core 】 Snowflake支持sequence使用随机数(issue#I51EJY@Gitee)
|
||||
* 【core 】 JarClassLoader增加构造(pr#593@Gitee)
|
||||
* 【core 】 增加Pid,以便获取单例pid
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复UserAgentUtil识别Linux出错(issue#I50YGY@Gitee)
|
||||
|
@ -54,7 +54,7 @@ public class JdkInterceptor implements InvocationHandler, Serializable {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 结束执行回调
|
||||
if (aspect.after(target, method, args, result)) {
|
||||
return result;
|
||||
|
@ -1,43 +0,0 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.lang.mutable.MutableObj;
|
||||
|
||||
/**
|
||||
* 为不可变的对象引用提供一个可变的包装,在java中支持引用传递。
|
||||
* @author Looly
|
||||
*
|
||||
* @param <T> 所持有值类型
|
||||
*/
|
||||
public final class Holder<T> extends MutableObj<T>{
|
||||
private static final long serialVersionUID = -3119568580130118011L;
|
||||
|
||||
/**
|
||||
* 新建Holder类,持有指定值,当值为空时抛出空指针异常
|
||||
*
|
||||
* @param <T> 被持有的对象类型
|
||||
* @param value 值,不能为空
|
||||
* @return Holder
|
||||
*/
|
||||
public static <T> Holder<T> of(T value) throws NullPointerException{
|
||||
if(null == value){
|
||||
throw new NullPointerException("Holder can not hold a null value!");
|
||||
}
|
||||
return new Holder<>(value);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public Holder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param value 被包装的对象
|
||||
*/
|
||||
public Holder(T value) {
|
||||
super(value);
|
||||
}
|
||||
//--------------------------------------------------------------------------- Constructor end
|
||||
}
|
51
hutool-core/src/main/java/cn/hutool/core/lang/Pid.java
Executable file
51
hutool-core/src/main/java/cn/hutool/core/lang/Pid.java
Executable file
@ -0,0 +1,51 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
/**
|
||||
* 进程ID单例封装<br>
|
||||
* 第一次访问时调用{@link ManagementFactory#getRuntimeMXBean()}获取PID信息,之后直接使用缓存值
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public enum Pid {
|
||||
INSTANCE;
|
||||
|
||||
private final int pid;
|
||||
|
||||
Pid() {
|
||||
this.pid = getPid();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取PID值
|
||||
*
|
||||
* @return pid
|
||||
*/
|
||||
public int get() {
|
||||
return this.pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前进程ID,首先获取进程名称,读取@前的ID值,如果不存在,则读取进程名的hash值
|
||||
*
|
||||
* @return 进程ID
|
||||
* @throws UtilException 进程名称为空
|
||||
*/
|
||||
private static int getPid() throws UtilException {
|
||||
final String processName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
if (StrUtil.isBlank(processName)) {
|
||||
throw new UtilException("Process name is blank!");
|
||||
}
|
||||
final int atIndex = processName.indexOf('@');
|
||||
if (atIndex > 0) {
|
||||
return Integer.parseInt(processName.substring(0, atIndex));
|
||||
} else {
|
||||
return processName.hashCode();
|
||||
}
|
||||
}
|
||||
}
|
@ -4,11 +4,11 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Holder;
|
||||
import cn.hutool.core.lang.PatternPool;
|
||||
import cn.hutool.core.lang.RegexPool;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.core.lang.mutable.Mutable;
|
||||
import cn.hutool.core.lang.mutable.MutableObj;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
|
||||
@ -311,8 +311,9 @@ public class ReUtil {
|
||||
* @param contentHolder 被匹配的内容的Holder,value为内容正文,经过这个方法的原文将被去掉匹配之前的内容
|
||||
* @param template 生成内容模板,变量 $1 表示group1的内容,以此类推
|
||||
* @return 新字符串
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static String extractMultiAndDelPre(Pattern pattern, Holder<CharSequence> contentHolder, String template) {
|
||||
public static String extractMultiAndDelPre(Pattern pattern, Mutable<CharSequence> contentHolder, String template) {
|
||||
if (null == contentHolder || null == pattern || null == template) {
|
||||
return null;
|
||||
}
|
||||
@ -342,7 +343,7 @@ public class ReUtil {
|
||||
* @param template 生成内容模板,变量 $1 表示group1的内容,以此类推
|
||||
* @return 按照template拼接后的字符串
|
||||
*/
|
||||
public static String extractMultiAndDelPre(String regex, Holder<CharSequence> contentHolder, String template) {
|
||||
public static String extractMultiAndDelPre(String regex, Mutable<CharSequence> contentHolder, String template) {
|
||||
if (null == contentHolder || null == regex || null == template) {
|
||||
return null;
|
||||
}
|
||||
|
@ -3,12 +3,12 @@ package cn.hutool.core.util;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Pid;
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -289,16 +289,7 @@ public class RuntimeUtil {
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public static int getPid() throws UtilException {
|
||||
final String processName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
if (StrUtil.isBlank(processName)) {
|
||||
throw new UtilException("Process name is blank!");
|
||||
}
|
||||
final int atIndex = processName.indexOf('@');
|
||||
if (atIndex > 0) {
|
||||
return Integer.parseInt(processName.substring(0, atIndex));
|
||||
} else {
|
||||
return processName.hashCode();
|
||||
}
|
||||
return Pid.INSTANCE.get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,4 +37,10 @@ public class RuntimeUtilTest {
|
||||
public void getUsableMemoryTest(){
|
||||
Assert.assertTrue(RuntimeUtil.getUsableMemory() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPidTest(){
|
||||
int pid = RuntimeUtil.getPid();
|
||||
Assert.assertTrue(pid > 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user