11 Commits

Author SHA1 Message Date
601611a11f update README.md.
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:48:36 +00:00
8d364b99f2 [update] jsonUtil优化
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:39:48 +00:00
127c6902bf [update] 判断空优化
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2024-02-22 11:39:23 +00:00
007d8d2f16 [update] SysUtil 增加JVM内存判断等待锁
Signed-off-by: 夜雪剑辰 <1107047387@qq.com>
2023-12-26 09:40:38 +00:00
41a9f1b507 Merge remote-tracking branch 'origin/jre11' into jre11
# Conflicts:
#	src/main/java/com/yexuejc/base/util/JsonUtil.java
2023-09-20 10:02:40 +08:00
bf731e753a 增加StrUtil.printStackTrace方法:把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace() 2023-09-20 10:01:58 +08:00
8d5e653d2b 增加StrUtil.printStackTrace方法:把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace() 2023-09-20 10:01:27 +08:00
c4fc4bd4f8 [update]FileUtil优化分页读取 2023-07-19 20:14:15 +08:00
yexuejc007
667ad26971 [update] ObjUtil.copy方法优化(即使copy不成功,也不报错,出警告日志)。PR:copy源和目标对象中的属性类型不一致,copy不成功 2023-06-15 14:24:05 +08:00
yexuejc007
a260b41fae Merge remote-tracking branch 'origin/jre11' into jre11 2023-06-13 17:56:08 +08:00
yexuejc007
0754660c45 [feat] ObjUtil 增加对象的深度copy 2023-06-13 17:55:37 +08:00
7 changed files with 462 additions and 107 deletions

View File

@@ -1,11 +1,11 @@
yexuejc-base 基于jdk8常用工具包
yexuejc-base 基于jdk11常用工具包
----------------------
源码地址:<br>
github:https://github.com/yexuejc/yexuejc-base
gitee:https://gitee.com/jzsw-it/yexuejc-base
### 说明
1. 支持环境java81.5.0开始支持java11请使用`x.x.x-jre11`版本)
1. 支持环境java111.5.0开始支持java11请使用`1.5.x-jre11`版本)
2. 该工具包基于springboot提取按理说适用于所有java工程
3. 其中依赖jjwt相关、validation-api排除请使用
> ```
@@ -23,7 +23,7 @@ gitee:https://gitee.com/jzsw-it/yexuejc-base
7.`1.5.0`开始,版本分为`1.5.0-jre8``1.5.0-jre11`分别对于jre8和jre11使用
### 使用
>yexuejc.base.version=1.5.0-jre8
>yexuejc.base.version=1.5.1-jre11
pom.xml
```

View File

@@ -120,4 +120,8 @@ public class ReadFileBean<T> {
public String lineScavenge(String lineData) {
return this.lineScavenger.apply(lineData);
}
public boolean hasNext() {
return this.fileLength > this.pointer;
}
}

View File

@@ -504,7 +504,7 @@ public class FileUtil {
List<String> datas = new ArrayList<>();
int row = 1;
String line;
while ((line = randomAccessFile.readLine()) != null && row < readFileBean.getReadRowNum()) {
while ((line = randomAccessFile.readLine()) != null && row <= readFileBean.getReadRowNum()) {
row++;
readFileBean.setPointer(randomAccessFile.getFilePointer());
datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset()))); }

View File

@@ -1,17 +1,5 @@
package com.yexuejc.base.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yexuejc.base.converter.*;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
@@ -21,6 +9,30 @@ import java.util.Map;
import java.util.TimeZone;
import java.util.logging.Logger;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yexuejc.base.converter.LocalDateDeserializer;
import com.yexuejc.base.converter.LocalDateSerializer;
import com.yexuejc.base.converter.LocalDateTimeDeserializer;
import com.yexuejc.base.converter.LocalDateTimeSerializer;
import com.yexuejc.base.converter.TimestampDeserializer;
import com.yexuejc.base.converter.TimestampSerializer;
/**
* json工具类基于jackson
*
@@ -30,7 +42,7 @@ import java.util.logging.Logger;
* @date 2018/9/3 15:28
*/
public class JsonUtil {
private static Logger log = Logger.getLogger(JsonUtil.class.getName());
private static final Logger log = Logger.getLogger(JsonUtil.class.getName());
private JsonUtil() {
}
@@ -38,10 +50,9 @@ public class JsonUtil {
/**
* 作为单例全局使用
*/
private static JsonMapper jsonMapper = new JsonMapper();
private static final JsonMapper jsonMapper = new JsonMapper();
static {
JsonUtil.setJavaTimeModule(JsonUtil.jsonMapper);
JsonUtil.initDefaultObjectMapper(JsonUtil.jsonMapper);
}
@@ -82,14 +93,17 @@ public class JsonUtil {
* </pre>
*
* @param jsonMapper
* @return
*/
private static void initDefaultObjectMapper(ObjectMapper jsonMapper) {
private static ObjectMapper initDefaultObjectMapper(ObjectMapper jsonMapper) {
JsonUtil.setJavaTimeModule(jsonMapper);
//值为空时NON_NULL舍去字段ALWAYS:保留字段
jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jsonMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
jsonMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
return jsonMapper;
}
/**
@@ -109,8 +123,7 @@ public class JsonUtil {
*
* @return
*/
public static ObjectMapper acceptEmptyStringAsNullObject() {
JsonMapper jsonMapper = new JsonMapper();
public static ObjectMapper acceptEmptyStringAsNullObject(JsonMapper jsonMapper) {
setJavaTimeModule(jsonMapper);
//值为空时ALWAYS:保留字段默认字符串值为“”对象值为null
jsonMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
@@ -179,11 +192,11 @@ public class JsonUtil {
try {
pojo = jsonMapper.readValue(json, cls);
} catch (JsonParseException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + e);
} catch (JsonMappingException e) {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
}
return pojo;
@@ -201,13 +214,13 @@ public class JsonUtil {
try {
pojo = jsonMapper.readValue(json, cls);
} catch (JsonParseException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
} catch (JsonMappingException e) {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
} catch (Exception e) {
e.printStackTrace();
log.warning("json to Object Exception.\n" + StrUtil.printStackTrace(e));
}
return pojo;
@@ -227,11 +240,11 @@ public class JsonUtil {
try {
pojo = jsonMapper.readValue(json, javaType);
} catch (JsonParseException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
} catch (JsonMappingException e) {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
}
return pojo;
}
@@ -252,11 +265,11 @@ public class JsonUtil {
try {
pojo = jsonMapper.readValue(json, mapType);
} catch (JsonParseException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
} catch (JsonMappingException e) {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
}
return pojo;
}
@@ -273,7 +286,7 @@ public class JsonUtil {
try {
return jsonMapper.readValue(json, javaType);
} catch (JsonProcessingException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
}
return null;
}
@@ -293,11 +306,11 @@ public class JsonUtil {
try {
pojo = jsonMapper.readValue(json, javaType);
} catch (JsonParseException e) {
log.warning("json to Object JsonParseException.\n" + e.getMessage());
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
} catch (JsonMappingException e) {
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
} catch (IOException e) {
log.warning("json to Object IOException.\n" + e.getMessage());
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
}
return pojo;
}
@@ -313,7 +326,7 @@ public class JsonUtil {
try {
json = jsonMapper.writeValueAsString(pojo);
} catch (JsonProcessingException e) {
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
log.warning("json to Object JsonProcessingException.\n" + StrUtil.printStackTrace(e));
}
return json;
}
@@ -329,8 +342,9 @@ public class JsonUtil {
try {
json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (JsonProcessingException e) {
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
log.warning("json to Object JsonProcessingException.\n" + StrUtil.printStackTrace(e));
}
return json;
}
}

View File

@@ -4,9 +4,13 @@ import com.yexuejc.base.annotation.ToUeProperty;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* 对象工具:对类的操作
@@ -18,6 +22,8 @@ import java.util.*;
* @date 2018/12/28 15:54
*/
public class ObjUtil {
private static Logger log = Logger.getLogger(ObjUtil.class.getName());
private ObjUtil() {
}
@@ -260,4 +266,150 @@ public class ObjUtil {
return outer;
}
/**
* 复制对象属性值,包含父类 (不需要getter和setter)
* <p>includeField和excludeField同时传入走包含逻辑</p>
*
* @param source 源对象
* @param targetClass 目标对象class
* @param includeField 包含对象
* @param excludeField 排除对象
* @param <I>
* @param <O>
* @return 目标对象
* @throws Exception
*/
public static <I, O> O copy(I source, Class<O> targetClass, List<String> includeField, List<String> excludeField) throws Exception {
List<Field> allFields = getAllFields(source.getClass());
O o = targetClass.getDeclaredConstructor().newInstance();
if (StrUtil.isNotEmpty(excludeField) && StrUtil.isEmpty(includeField)) {
allFields = allFields.stream().filter(f -> !excludeField.contains(f.getName())).collect(Collectors.toList());
}
if (StrUtil.isNotEmpty(includeField)) {
allFields = allFields.stream().filter(f -> includeField.contains(f.getName())).collect(Collectors.toList());
}
allFields.forEach(f -> {
try {
try {
Field field = targetClass.getDeclaredField(f.getName());
if (field != null) {
f.setAccessible(true);
Object v = f.get(source);
f.setAccessible(false);
field.setAccessible(true);
field.set(o, v);
field.setAccessible(false);
}
} catch (NoSuchFieldException e) {
}
} catch (Exception e) {
log.warning(lowerCaseFirstChar(f.getName()) + " field copy failed. " + e);
log.log(Level.FINER, lowerCaseFirstChar(f.getName()) +
" field copy failed. The exception information is as follows:", e);
}
});
return o;
}
/**
* 深度复制对象
* <p>获取source的所有getXxx。xxx作为属性且包含父类的getXxx</p>
* <p>查找target的xxx属性进行反射设值</p>
*
* @param source 源对象
* @param targetClass 目标对象class
* @param invokeSetter 设置target属性值时是否使用setter方法设置
* @param <I>
* @param <O>
* @return 目标对象
*/
public static <I, O> O copy(I source, Class<O> targetClass, boolean invokeSetter) throws Exception {
List<Method> getterMethods = getAllGetterMethods(source.getClass(), "get");
O o = targetClass.getDeclaredConstructor().newInstance();
getterMethods.forEach(method -> {
String fieldName = method.getName().replace("get", "");
try {
Object v = method.invoke(source);
if (invokeSetter) {
try {
Method setterMethod = targetClass.getDeclaredMethod("set" + fieldName, method.getReturnType());
if (null != setterMethod) {
setterMethod.invoke(o, v);
}
} catch (NoSuchMethodException e) {
}
} else {
try {
Field field = targetClass.getDeclaredField(lowerCaseFirstChar(fieldName));
if (field != null) {
field.setAccessible(true);
field.set(o, v);
field.setAccessible(false);
}
} catch (NoSuchFieldException e) {
}
}
} catch (Exception e) {
log.warning(lowerCaseFirstChar(fieldName) + " field copy failed. " + e);
log.log(Level.FINER, lowerCaseFirstChar(fieldName) +
" field copy failed. The exception information is as follows:\n", e);
}
});
return o;
}
/**
* 获取所有方法,包含父类
*
* @param beanClass
* @param startsWith 方法的开头匹配(空,返回所有)
* @return
*/
public static List<Method> getAllGetterMethods(Class<?> beanClass, String startsWith) {
List<Method> methodList = new ArrayList<>();
Method[] methods = beanClass.getDeclaredMethods();
if (StrUtil.isNotEmpty(startsWith)) {
methodList.addAll(Arrays.stream(methods)
.filter(method -> method.getName().startsWith(startsWith))
.collect(Collectors.toList()));
} else {
methodList.addAll(Arrays.asList(methods));
}
Class<?> superclass = beanClass.getSuperclass();
if (superclass != null) {
methodList.addAll(getAllGetterMethods(superclass, startsWith));
}
return methodList;
}
/**
* 获取所有属性,包含父类
*
* @param beanClass
* @return
*/
public static List<Field> getAllFields(Class<?> beanClass) {
List<Field> fieldList = new ArrayList<>();
while (beanClass != null) {
fieldList.addAll(Arrays.asList(beanClass.getDeclaredFields()));
beanClass = beanClass.getSuperclass();
}
return fieldList;
}
/**
* 首字母小写
*
* @param str
* @return
*/
public static String lowerCaseFirstChar(String str) {
if (str == null || str.length() == 0) {
return str;
} else {
return str.substring(0, 1).toLowerCase() + str.substring(1);
}
}
}

View File

@@ -1,9 +1,19 @@
package com.yexuejc.base.util;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -19,39 +29,28 @@ public final class StrUtil {
private StrUtil() {
}
public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 判断字符串,数组,集合 是否为空
* 判断字符串,数组,集合 是否为空(null""[],{})
*
* @param obj
* @param obj 対象
* @return true:空;false:非空
* @return
*/
public static boolean isEmpty(Object obj) {
if (obj instanceof String) {
if (obj == null || "".equals((String) obj)) {
return true;
} else {
return false;
}
} else if (obj instanceof Object[]) {
if (obj == null || ((Object[]) obj).length == 0) {
return true;
} else {
return false;
}
} else if (obj instanceof Collection<?>) {
if (obj == null || ((Collection<?>) obj).size() == 0) {
return true;
} else {
return false;
}
} else {
if (obj == null) {
return true;
} else if (obj instanceof Optional) {
return ((Optional<?>) obj).isEmpty();
} else if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() == 0;
} else if (obj.getClass().isArray()) {
return Array.getLength(obj) == 0;
} else if (obj instanceof Collection) {
return ((Collection<?>) obj).isEmpty();
} else {
return false;
}
return obj instanceof Map && ((Map<?, ?>) obj).isEmpty();
}
}
@@ -111,40 +110,6 @@ public final class StrUtil {
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
}
/**
* 解析aa=bb&cc=dd&ee=ff格式的字符串返回HashMap
*
* @param urlencoded
* @return
*/
public static Map<String, String> parseUrlencoded(String urlencoded) {
if (isEmpty(urlencoded)) {
return null;
}
String[] entrys = urlencoded.split("&");
if (isEmpty(entrys)) {
return null;
}
Map<String, String> map = new HashMap<String, String>(16);
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
continue;
}
kv = entry.split("=");
if (isEmpty(kv)) {
continue;
}
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else {
map.put(kv[0], null);
}
}
return map;
}
/**
* 字符串转换方法 把字节数组转换成16进制字符串
*
@@ -254,8 +219,8 @@ public final class StrUtil {
/**
* 对ID32位进行编码
*
* @param id
* @return
* @param id 32位ID
* @return 编码后的64位ID
*/
public static String codeId(String id) {
if (id == null || id.length() != 32) {
@@ -282,8 +247,8 @@ public final class StrUtil {
/**
* 对ID32位进行解码
*
* @param coded
* @return
* @param coded 编码后的64位ID
* @return 解码后的32位ID
*/
public static String decodeId(String coded) {
if (coded == null || coded.length() != 64) {
@@ -297,6 +262,40 @@ public final class StrUtil {
return id.toString();
}
/**
* 解析aa=bb&cc=dd&ee=ff格式的字符串返回HashMap
*
* @param urlencoded
* @return
*/
public static Map<String, String> parseUrlencoded(String urlencoded) {
if (isEmpty(urlencoded)) {
return null;
}
String[] entrys = urlencoded.split("&");
if (isEmpty(entrys)) {
return null;
}
Map<String, String> map = new HashMap<String, String>(16);
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
continue;
}
kv = entry.split("=");
if (isEmpty(kv)) {
continue;
}
if (kv.length > 1) {
map.put(kv[0], kv[1]);
} else {
map.put(kv[0], null);
}
}
return map;
}
/**
* map parameters 转url parameters
*
@@ -370,7 +369,7 @@ public final class StrUtil {
/**
* 下划线字符
*/
public static final char UNDERLINE = '_';
private static final char UNDERLINE = '_';
/**
* 字符串下划线转驼峰格式
@@ -419,4 +418,69 @@ public final class StrUtil {
}
return sb.toString();
}
private static final String NEW_LINE = "\n";
private static final String ERROR_MESSAGE_FORMAT = "%s.%s(%s:%d)";
/**
* 把异常堆栈信息转化为字符串,同时把所有的errorMessage用\n方式拼接到printStackTrace前面 -> 替代 e.printStackTrace()
* <p>使用e.printStackTrace()不能作为字符串处理本方法可以转换e.printStackTrace()为字符串</p>
* <p><b>printStackTrace</b>信息从<i>Caused by:</i>C开始</p>
*
* @param cause
* @return
*/
public static String printStackTraceAndMessage(Throwable cause) {
if (cause != null) {
StringBuilder msg = new StringBuilder();
if (isNotEmpty(cause.getMessage())) {
msg.append(cause.getMessage()).append(NEW_LINE);
}
String causedMsg = printStackTrace(cause, (eMessage) -> {
if (isNotEmpty(eMessage)) {
msg.append(eMessage).append(NEW_LINE);
}
});
return msg.append(causedMsg).toString();
}
return "";
}
/**
* 把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace()
* <p>使用e.printStackTrace()不能作为字符串处理本方法可以转换e.printStackTrace()为字符串</p>
*
* @param cause
* @return
*/
public static String printStackTrace(Throwable cause) {
return printStackTrace(cause, (eMessage) -> {
});
}
private static String printStackTrace(Throwable cause, Consumer<String> consumer) {
if (cause != null) {
StringBuilder sb = new StringBuilder();
String cClass = cause.getClass().getName();
String eMessage = cause.getMessage();
StackTraceElement[] stackTrace = cause.getStackTrace();
Throwable caused = cause.getCause();
while (caused != null) {
cClass = caused.getClass().getName();
eMessage = caused.getMessage();
stackTrace = caused.getStackTrace();
caused = caused.getCause();
consumer.accept(eMessage);
}
sb.append("Caused by: ").append(cClass).append(": ").append(eMessage).append(NEW_LINE);
for (StackTraceElement element : stackTrace) {
sb.append("\tat ");
sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(),
element.getFileName(), element.getLineNumber()));
sb.append(NEW_LINE);
}
return sb.toString();
}
return "";
}
}

View File

@@ -3,7 +3,15 @@ package com.yexuejc.base.util;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 系统工具类
@@ -14,6 +22,7 @@ import java.util.concurrent.*;
* @date: 2017/12/28 16:12
*/
public class SysUtil {
private static Logger logger = Logger.getLogger(SysUtil.class.getName());
private static final String PROJECT_ROOT_PATH = "java.io.tmpdir";
private SysUtil() {
@@ -79,4 +88,116 @@ public class SysUtil {
*/
void execute();
}
/**
* 获取当前JVM所有线程
*
* @return
*/
public static List<Thread> getThreadList() {
List<Thread> threadList = new ArrayList<>();
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
while (currentGroup.getParent() != null) {
currentGroup = currentGroup.getParent();
}
int activeCount = currentGroup.activeCount();
Thread[] threads = new Thread[activeCount];
currentGroup.enumerate(threads);
for (Thread thread : threads) {
threadList.add(thread);
}
return threadList;
}
/** 线程锁 */
private static final Lock THREAD_LOCK = new ReentrantLock();
/** 已经等待的时间 */
private static final AtomicInteger SLEEP_TIME = new AtomicInteger(0);
/** 最大等待3分钟 */
private static final int MAX_SLEEP_TIME = 180;
/**
* 通过锁的方式判断jvm的内存如果超80%就等待最大3分钟JVM内存降低到80%再执行
*/
public static void checkJvmMemory() {
THREAD_LOCK.lock();
try {
while (jvmMemoryIsNotExecutable()) {
//jvm内存使用率达到80%,阻塞所有线程最大等待3分钟后放开
if (SLEEP_TIME.incrementAndGet() < MAX_SLEEP_TIME) {
TimeUnit.SECONDS.sleep(1);
} else {
break;
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
THREAD_LOCK.unlock();
SLEEP_TIME.set(0);
}
}
/**
* jvm内存是否大于80%(不能继续执行)
*
* @return true:是false:不是
*/
public static boolean jvmMemoryIsNotExecutable() {
Runtime runtime = Runtime.getRuntime();
//jvm总内存
long jvmTotalMemoryByte = runtime.totalMemory();
//jvm最大可申请
long jvmMaxMemoryByte = runtime.maxMemory();
//空闲空间
long freeMemoryByte = runtime.freeMemory();
double rate = (jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte;
if (rate >= 0.8) {
rate = (jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmMaxMemoryByte;
if (rate >= 0.8) {
//jvm内存使用率达到80%
print(jvmTotalMemoryByte, jvmMaxMemoryByte, freeMemoryByte);
return true;
}
}
return false;
}
private static final DecimalFormat RATE_DECIMAL_FORMAT = new DecimalFormat("#.##%");
private static void print(long jvmTotalMemoryByte, long jvmMaxMemoryByte, long freeMemoryByte) {
if (logger.isLoggable(Level.WARNING)) {
String sb = "\n=========================================================" +
"\nThread Name = " + Thread.currentThread().getName() +
"\nJVM Memory = " + formatByte(jvmTotalMemoryByte) +
"\nJVM Max Memory = " + formatByte(jvmMaxMemoryByte) +
"\nJVM Used Memory = " + formatByte(jvmTotalMemoryByte - freeMemoryByte) +
"\nJVM Free Memory = " + formatByte(freeMemoryByte) +
"\nJVM Memory Rate = " + RATE_DECIMAL_FORMAT.format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte) +
"\n=========================================================";
logger.warning(sb);
}
}
private static String formatByte(long byteNumber) {
//换算单位
double format = 1024.0;
double kbNumber = byteNumber / format;
if (kbNumber < format) {
return new DecimalFormat("#.##KB").format(kbNumber);
}
double mbNumber = kbNumber / format;
if (mbNumber < format) {
return new DecimalFormat("#.##MB").format(mbNumber);
}
double gbNumber = mbNumber / format;
if (gbNumber < format) {
return new DecimalFormat("#.##GB").format(gbNumber);
}
double tbNumber = gbNumber / format;
return new DecimalFormat("#.##TB").format(tbNumber);
}
}