提交修改

This commit is contained in:
quyixiao 2025-09-16 00:58:55 +08:00
parent 3412fb7cd1
commit c48945995f
2 changed files with 236 additions and 20 deletions

View File

@ -0,0 +1,24 @@
package com.heyu.api.data.utils;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Future;
@Data
public class AsynRunDTO {
private Map<String, Future<R>> map;
public void run(){
Map<String,Object> data = new HashMap<>();
map.forEach((key, future) -> Optional.ofNullable(SanUtils.get(future)).ifPresent(osResp -> {
data.put(key, osResp);
}));
}
}

View File

@ -6,12 +6,14 @@ import com.heyu.api.common.test.AriseUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ClassUtils;
import org.junit.Test;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.*;
@Slf4j
public class SanUtils {
@ -53,10 +55,7 @@ public class SanUtils {
}
public static <T, R, V> List<R> setNull(List<V> values, LBiFunction0<T, R> ... functions ) {
public static <T, R, V> List<R> setNull(List<V> values, LBiFunction0<T, R>... functions) {
List<R> list = new ArrayList<>();
try {
if (CollectionUtils.isEmpty(values)) {
@ -67,8 +66,8 @@ public class SanUtils {
for (LBiFunction0<T, R> function : functions) {
Method method = getSetMethod(function);
for (V value : values) {
Object [] objects= new Object[]{null};
method.invoke(value,objects);
Object[] objects = new Object[]{null};
method.invoke(value, objects);
}
}
return list;
@ -79,8 +78,6 @@ public class SanUtils {
}
public static <T, R, V> List<R> list2list(List<V> values, LBiFunction0<T, R> function) {
List<R> list = new ArrayList<>();
try {
@ -163,7 +160,6 @@ public class SanUtils {
}
private static <T, R> Method getSetMethod(LBiFunction0<T, R> function) {
try {
Method method = function.getClass().getDeclaredMethods()[1];
@ -172,7 +168,7 @@ public class SanUtils {
String methodName = serializedLambda.getImplMethodName();
String className = serializedLambda.getImplClass().replace("/", ".");
methodName = "set"+methodName.substring(3);
methodName = "set" + methodName.substring(3);
Class<?> clazz = Class.forName(className);
@ -270,7 +266,6 @@ public class SanUtils {
}
/**
* 基本类型包装类解析
*/
@ -288,7 +283,7 @@ public class SanUtils {
return NumberUtils.objToDoubleDefault(value, null);
} else if (parameterType == Byte.class || parameterType == byte.class) {
return NumberUtils.objToByteDefault(value, null);
} else if (parameterType == BigDecimal.class) {
} else if (parameterType == BigDecimal.class) {
return NumberUtils.objToBigDecimalDefault(value, null);
}
} else if (parameterType == Boolean.class || parameterType == boolean.class) {
@ -296,20 +291,20 @@ public class SanUtils {
} else if (parameterType == Character.class || parameterType == char.class) {
return NumberUtils.objToCharacterDefault(value, null);
} else if (parameterType == String.class) {
return value !=null ? value.toString() : null ;
}else if (parameterType == Date.class && value instanceof Date){
return value != null ? value.toString() : null;
} else if (parameterType == Date.class && value instanceof Date) {
return value;
} else if (parameterType == Date.class && value instanceof String) {
Object target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH_MM_SS);
if (target == null) {
target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD);
if(target == null){
if (target == null) {
target = DateUtils.parseStr2Date((String) value, YYYY);
if(target == null){
if (target == null) {
target = DateUtils.parseStr2Date((String) value, YYYY_MM);
if(target == null){
if (target == null) {
target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH);
if(target == null){
if (target == null) {
target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH_MM);
}
}
@ -357,6 +352,197 @@ public class SanUtils {
return primitiveTypes.contains(clazz) ? true : false;
}
public static <T> T get(Future<T> t) {
return get(t, 3000L);
}
public static <T> T get(Future<T> t, long timeout) {
if (t != null) {
try {
return t.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
return null;
}
private static ExecutorService threadPool;
static {
threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
public static <T, R, V> List<R> parallel2List(List<V> params, LBiFunction0<T, R> function) {
List<R> list = new ArrayList<>();
Map<String, Future<R>> map = new HashMap<>();
for (int i = 0; i < params.size(); i++) {
V v = params.get(i);
Future<R> countFuture = threadPool.submit(getAsynCallable(function, v));
map.put(i + "", countFuture);
}
Map<String, R> agentResult = new HashMap<>();
map.forEach((key, future) -> Optional.ofNullable(get(future)).ifPresent(osResp -> {
agentResult.put(key, osResp);
}));
for (Map.Entry<String, R> entry : agentResult.entrySet()) {
list.add(entry.getValue());
}
return list;
}
public static <T, R, V, K> Map<K, R> parallel2Map(List<V> params, LBiFunction0<T, R> function, LBiFunction0<T, K> functionKey) {
Map<K, Future<R>> map = new HashMap<>();
for (int i = 0; i < params.size(); i++) {
V v = params.get(i);
Future<R> countFuture = threadPool.submit(getAsynCallable(function, v));
K key = null;
Object object = getKeyDeValue(v);
Method method = getMethod(functionKey);
method.setAccessible(true);
try {
int modifiers = method.getModifiers();
if (Modifier.isStatic(modifiers)) {
key = (K) method.invoke(null, object);
} else {
key = (K) method.invoke(object);
}
} catch (Exception e) {
e.printStackTrace();
}
map.put(key, countFuture);
}
Map<K, R> agentResult = new HashMap<>();
map.forEach((key, future) -> Optional.ofNullable(get(future)).ifPresent(osResp -> {
agentResult.put(key, osResp);
}));
return agentResult;
}
private static Object getKeyDeValue(Object v) {
if (v instanceof Map) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) v).entrySet()) {
return getKeyDeValue(entry.getValue());
}
} else if (v instanceof List) {
return getKeyDeValue(((List<?>) v).get(0));
} else if (!isBasicDataTypes(v.getClass())) {
return v;
}
return null;
}
private static <T, P, R, E> Callable getAsynCallable(LBiFunction0<T, R> function0, P value) {
return new Callable<R>() {
@Override
public R call() throws Exception {
Object object = null;
try {
Method method = getMethod(function0);
int modifiers = method.getModifiers();
if (Modifier.isStatic(modifiers)) {
object = method.invoke(null, value);
} else {
object = method.invoke(value);
}
return (R) object;
} catch (Exception e) {
log.error("线程池异常",e);
} finally {
}
return null;
}
};
}
private static Callable getAsynRun(Runnable runnable) {
return new Callable() {
@Override
public R call() throws Exception {
Object object = null;
try {
runnable.run();
} catch (Exception e) {
log.error("线程池异常",e);
}
return null;
}
};
}
public static AsynRunDTO build(Runnable ... target){
Map<String, Future<R>> map = new HashMap<>();
for (int i = 0; i < target.length; i++) {
Runnable v = target[i];
Future<R> countFuture = threadPool.submit(getAsynRun(v));
map.put(i + "", countFuture);
}
AsynRunDTO asynRunDTO = new AsynRunDTO();
asynRunDTO.setMap(map);
return asynRunDTO;
}
@Test
public void test(){
build(
()->{
System.out.println("1");
},
()->{
System.out.println("2");
}
).run();
}
public static void main(String[] args) {
AriseUser ai = new AriseUser();
ai.setName("张三");
@ -375,9 +561,15 @@ public class SanUtils {
list.add(ai2);
list.add(ai3);
setNull(list, AriseUser::getAge);
Map<Integer, AriseUser> objectMap = parallel2Map(list, SanUtils::getName, AriseUser::getAge);
System.out.println(JSON.toJSONString(objectMap));
System.out.println(JSON.toJSONString(list));
}
public static AriseUser getName(AriseUser ariseUser) {
return ariseUser;
}