Skip to content
Merged

1 #320

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions EasyExcel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.netease.lowcode.extensions</groupId>
<artifactId>EasyExcel</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down Expand Up @@ -108,6 +108,10 @@
<artifactId>codewave-file-connector-library</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -118,7 +122,7 @@
<version>1.7.1</version>
<configuration>
<jarWithDependencies>false</jarWithDependencies>
<rewriteVersion>true</rewriteVersion>
<!-- <rewriteVersion>true</rewriteVersion>-->
</configuration>
<executions>
<execution>
Expand Down
23 changes: 19 additions & 4 deletions async_function_pool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

<groupId>com.netease</groupId>
<artifactId>async_function_pool</artifactId>
<version>1.0.4</version>
<version>1.1.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nasl.ide.version>3.3</nasl.ide.version>
<nasl.ide.version>3.8</nasl.ide.version>
</properties>
<dependencies>
<!--本案例是本地系统引入nasl-metadata-collector-0.8.0.jar的方式。
Expand All @@ -35,6 +35,21 @@
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<artifactId>lcap-annotation</artifactId>
<groupId>com.netease.lowcode</groupId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -57,10 +72,10 @@
<plugin>
<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.4.2</version>
<version>1.4.3</version>
<configuration>
<jarWithDependencies>false</jarWithDependencies>
<rewriteVersion>false</rewriteVersion>
<rewriteVersion>true</rewriteVersion>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.netease.lib.tasks.annotation;

import com.netease.lowcode.annotation.LCAPLogicAnnotation;
import com.netease.lowcode.core.annotation.NaslAnnotation;

@NaslAnnotation(
applyTo = {NaslAnnotation.Component.LOGIC} // 用于声明这是一个逻辑注解
)
public class AsyncLogicAnnotation extends LCAPLogicAnnotation {
@NaslAnnotation.Property(
title = "是否开加入多线程执行",
defaultValue = "false"
)
public Boolean useAnno;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netease.lib.tasks.api;

import com.alibaba.fastjson.JSONObject;
import com.netease.lib.tasks.model.ThreadResultDTO;
import com.netease.lowcode.core.annotation.NaslLogic;
import org.slf4j.Logger;
Expand Down Expand Up @@ -35,12 +36,14 @@ public class FunctionManagerApi {
* 运行中任务注册
* 任务id,任务
*/
private final Map<String, CompletableFuture<String>> runningTaskRegister = new ConcurrentHashMap<>();


private final Map<String, CompletableFuture<Object>> runningTaskRegister = new ConcurrentHashMap<>();
@Resource(name = "libraryCommonTaskExecutor")
private Executor contextAwareExecutor;

public void putRunningTask(String taskId, CompletableFuture<Object> future) {
runningTaskRegister.put(taskId, future);
}

/**
* 初始化注册逻辑
*
Expand Down Expand Up @@ -70,7 +73,15 @@ public String asyncRunLogic(String logicKey, String requestStr) {
logger.error("asyncRunLogic not exist: {}", logicKey);
return null;
}
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> function.apply(requestStr), contextAwareExecutor);
CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {
try {
return function.apply(requestStr);
} catch (Exception e) {
// 使用你的日志框架记录异常
logger.error("异步任务执行异常: {}", e.getMessage(), e);
}
return null;
}, contextAwareExecutor);
logger.info("asyncRunLogic success: {}", logicKey);
String taskId = UUID.randomUUID().toString();
runningTaskRegister.put(taskId, future);
Expand All @@ -89,14 +100,20 @@ public List<ThreadResultDTO> syncGetLogicResult(List<String> taskIdList) {
taskIdList.forEach(taskId -> {
ThreadResultDTO resultDTO = new ThreadResultDTO();
resultDTO.setTaskId(taskId);
CompletableFuture<String> functionWeak = runningTaskRegister.get(taskId);
CompletableFuture<Object> functionWeak = runningTaskRegister.get(taskId);
if (functionWeak == null) {
resultDTO.setTaskStatus(3);
resultDTOList.add(resultDTO);
return;
}
CompletableFuture future = runningTaskRegister.get(taskId);
String result = (String) future.join();
Object resObj = future.join();
String result;
if (resObj instanceof String) {
result = (String) resObj;
} else {
result = JSONObject.toJSONString(resObj);
}
runningTaskRegister.remove(taskId);
resultDTO.setTaskResult(result);
resultDTO.setTaskStatus(1);
Expand All @@ -117,15 +134,21 @@ public List<ThreadResultDTO> asyncGetLogicResult(List<String> taskIdList) {
taskIdList.forEach(taskId -> {
ThreadResultDTO resultDTO = new ThreadResultDTO();
resultDTO.setTaskId(taskId);
CompletableFuture<String> functionWeak = runningTaskRegister.get(taskId);
CompletableFuture<Object> functionWeak = runningTaskRegister.get(taskId);
if (functionWeak == null) {
resultDTO.setTaskStatus(3);
resultDTOList.add(resultDTO);
return;
}
CompletableFuture future = runningTaskRegister.get(taskId);
if (future.isDone()) {
String result = (String) future.join();
Object resObj = future.join();
String result;
if (resObj instanceof String) {
result = (String) resObj;
} else {
result = JSONObject.toJSONString(resObj);
}
runningTaskRegister.remove(taskId);
resultDTO.setTaskResult(result);
resultDTO.setTaskStatus(1);
Expand All @@ -143,7 +166,15 @@ public List<ThreadResultDTO> asyncGetLogicResult(List<String> taskIdList) {
@NaslLogic
public Boolean asyncRunLogicNoResult(Function<String, String> asyncfunction, String requestStr) {
try {
contextAwareExecutor.execute(() -> asyncfunction.apply(requestStr));
contextAwareExecutor.execute(() -> {
try {
asyncfunction.apply(requestStr);
} catch (Exception e) {
// 使用你的日志框架记录异常
logger.error("异步任务执行异常: {}", e.getMessage(), e);
}
}
);
return true;
} catch (RejectedExecutionException e) {
logger.error("Async task rejected for request: {}", requestStr, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.netease.lib.tasks.aspect;

import com.netease.lib.tasks.annotation.AsyncLogicAnnotation;
import com.netease.lib.tasks.api.FunctionManagerApi;
import com.netease.lib.tasks.util.AsyncLogicAnnotationConfigUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import static java.lang.Thread.sleep;

@Aspect
@Component
public class ExtensionAsyncLogicExcuteAspect {
private final static Logger log = LoggerFactory.getLogger("LCAP_EXTENSION_LOGGER");
@Resource
private FunctionManagerApi functionManagerApi;

@Resource(name = "libraryCommonTaskExecutor")
private Executor contextAwareExecutor;

@Around("execution(* *.*.*.service.*LogicService.*(..))")
public Object aroundCustomizeServiceMethods(ProceedingJoinPoint joinPoint) throws Throwable {
List<String> allUseAsyncAnnoLogicNames = AsyncLogicAnnotationConfigUtil
.listAlluseAnnoLogicNames(AsyncLogicAnnotation.class.getSimpleName());
if (!allUseAsyncAnnoLogicNames.contains(joinPoint.getSignature().getName())) {
return joinPoint.proceed();
}
CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {
try {
sleep(10*1000);
return joinPoint.proceed();
} catch (Throwable e) {
log.error("ExtensionAsyncLogicExcuteAspect error", e);
return null;
}
}, contextAwareExecutor);
String taskId = UUID.randomUUID().toString();
functionManagerApi.putRunningTask(taskId, future);
return taskId;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.netease.lib.tasks.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AsyncLogicAnnotationConfigUtil {
private final static Logger log = LoggerFactory.getLogger("LCAP_EXTENSION_LOGGER");

private static final String ANNOTATION_METADATA_LOGIC = "annotation/annotation_metadata_logic_";
private static Map<String, List<String>> useAnnoLogicNames = new HashMap<>();

public static JSONArray readAnnotationFile(String annoName) {
try {
ClassPathResource resource = new ClassPathResource(ANNOTATION_METADATA_LOGIC + annoName + ".json");
InputStream inputStream = resource.getInputStream();
byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
JSONArray array = JSONObject.parseArray(new String(bdata, StandardCharsets.UTF_8));
return array;
} catch (Exception e) {
log.error("readAnnotationFile error", e);
// 处理异常
return null;
}
}

public static List<String> listAlluseAnnoLogicNames(String annoName) {
if (useAnnoLogicNames != null) {
List<String> result = useAnnoLogicNames.get(annoName);
if (result != null) {
return result;
}
}
JSONArray annoArray = readAnnotationFile(annoName);
if (annoArray == null) {
useAnnoLogicNames.put(annoName, new ArrayList<>());
return new ArrayList<>();
}
List<String> result = new ArrayList<>();
annoArray.forEach(anno -> {
JSONObject annoObj = (JSONObject) anno;
String logicName = annoObj.getString("logicName");
JSONObject annotationProperties = annoObj.getJSONObject("annotationProperties");
if ("true".equals(annotationProperties.getString("useAnno"))) {
result.add(logicName);
}
});
useAnnoLogicNames.put(annoName, result);
return result;
}
}
2 changes: 1 addition & 1 deletion common-es-search-tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.netease</groupId>
<artifactId>common-es-search-tool</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Loading
Loading