feat: 测试线程变量

master
baize 2024-05-13 21:53:13 +08:00
parent b71163984e
commit 10a34edcd4
23 changed files with 226 additions and 16 deletions

View File

@ -3,7 +3,7 @@ package com.muyu.data.source.client.pool;
/** /**
* *
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/29 029 14:30 * @Date 2024/4/29 029 14:30
*/ */

View File

@ -3,7 +3,7 @@ package com.muyu.data.source.client.pool;
/** /**
* *
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/29 029 14:41 * @Date 2024/4/29 029 14:41
*/ */
public interface BasePool<T> { public interface BasePool<T> {

View File

@ -3,7 +3,7 @@ package com.muyu.data.source.client.pool;
/** /**
* Mysql * Mysql
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/29 029 15:17 * @Date 2024/4/29 029 15:17
*/ */
public class MysqlConnException extends RuntimeException{ public class MysqlConnException extends RuntimeException{

View File

@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Mysql * Mysql
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/29 029 14:36 * @Date 2024/4/29 029 14:36
*/ */
@Component @Component

View File

@ -14,7 +14,7 @@ import lombok.Data;
/** /**
* mysql * mysql
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/29 029 14:24 * @Date 2024/4/29 029 14:24
*/ */
@Data @Data

View File

@ -0,0 +1,15 @@
package com.muyu.data.source.domain.model;
/**
* A
*
* @author DeKangLiu
* Date 2024/5/13 16:40
*/
public class A implements Base{
@Override
public void execution() {
DataModel dataModel = ThreadConstant.get();
System.out.println(dataModel.getValue());
}
}

View File

@ -0,0 +1,15 @@
package com.muyu.data.source.domain.model;
/**
* B
*
* @author DeKangLiu
* Date 2024/5/13 16:46
*/
public class B implements Base{
@Override
public void execution() {
DataModel dataModel = ThreadConstant.get();
dataModel.setValue("李四");
}
}

View File

@ -0,0 +1,12 @@
package com.muyu.data.source.domain.model;
/**
* Base
*
* @author DeKangLiu
* Date 2024/5/13 16:57
*/
public interface Base {
public void execution();
}

View File

@ -0,0 +1,19 @@
package com.muyu.data.source.domain.model;
/**
* C
*
* @author DeKangLiu
* Date 2024/5/13 16:47
*/
public class C implements Base{
@Override
public void execution() {
DataModel dataModel = ThreadConstant.get();
if ("张三".equals(dataModel.getValue())){
System.out.println("是张三");
}else {
System.out.printf("不是张三,而是%s%n", dataModel.getValue());
}
}
}

View File

@ -0,0 +1,45 @@
package com.muyu.data.source.domain.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* DataModel
*
* @author DeKangLiu
* Date 2024/5/13 16:37
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DataModel {
/**
*
*/
private String key;
/**
*
*/
private Object value;
/**
* -
*/
private String sourceType;
/**
* -
*/
private String processType;
/**
*
*/
private Class<?> processClass;
}

View File

@ -0,0 +1,38 @@
package com.muyu.data.source.domain.model;
import java.util.concurrent.ConcurrentHashMap;
/**
* Main
*
* @author DeKangLiu
* Date 2024/5/13 16:51
*/
public class Main {
private static final ConcurrentHashMap<String, Base> ruleEngineMap = new ConcurrentHashMap<>();
static {
init();
}
public static void init(){
ruleEngineMap.put("a",new A());
ruleEngineMap.put("b",new B());
ruleEngineMap.put("c",new C());
}
public static void main(String[] args) {
DataModel dataModel = new DataModel();
dataModel.setValue("王五");
dataModel.setKey("name");
dataModel.setProcessClass(String.class);
dataModel.setSourceType("varchar");
dataModel.setProcessType("String");
ThreadConstant.set(dataModel);
Base a = ruleEngineMap.get("a");
Base b = ruleEngineMap.get("b");
Base c = ruleEngineMap.get("c");
a.execution();
b.execution();
c.execution();
ThreadConstant.remove();
}
}

View File

@ -0,0 +1,24 @@
package com.muyu.data.source.domain.model;
/**
* ThreadContsant
*
* @author DeKangLiu
* Date 2024/5/13 16:40
*/
public class ThreadConstant {
private static ThreadLocal<DataModel> threadLocal = new ThreadLocal<>();
public static DataModel get() {
return threadLocal.get();
}
public static void set(DataModel dataModel) {
threadLocal.set(dataModel);
}
public static void remove() {
threadLocal.remove();
}
}

View File

@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
/** /**
* RuleContentModel * RuleContentModel
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/5/2 002 14:27 * @Date 2024/5/2 002 14:27
*/ */
@Data @Data

View File

@ -22,7 +22,7 @@ import java.util.List;
/** /**
* ruleengine * ruleengine
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Data @Data

View File

@ -5,7 +5,7 @@ import lombok.Data;
/** /**
* RuleContentReq * RuleContentReq
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/5/2 002 14:30 * @Date 2024/5/2 002 14:30
*/ */
@Data @Data

View File

@ -12,7 +12,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
/** /**
* ruleengine * ruleengine
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Data @Data

View File

@ -12,7 +12,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
/** /**
* ruleengine * ruleengine
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Data @Data

View File

@ -12,7 +12,7 @@ import com.muyu.common.core.web.domain.BaseEntity;
/** /**
* ruleengine * ruleengine
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Data @Data

View File

@ -10,7 +10,7 @@ import org.springframework.scheduling.annotation.EnableAsync;
/** /**
* MuYuRuleEngineApplication * MuYuRuleEngineApplication
* *
* @author WangLei * @author DeKangLiu
* @Date 2024/4/24 024 10:07 * @Date 2024/4/24 024 10:07
*/ */
@EnableCustomConfig @EnableCustomConfig

View File

@ -22,7 +22,7 @@ import com.muyu.common.core.web.page.TableDataInfo;
/** /**
* Controller * Controller
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Api(tags = "规则引擎") @Api(tags = "规则引擎")
@ -153,4 +153,9 @@ public class RuleengineController extends BaseController {
public Result updateEngineCodeIng(@RequestBody Engineversion engineversion) { public Result updateEngineCodeIng(@RequestBody Engineversion engineversion) {
return ruleengineService.updateEngineCodeIng(engineversion); return ruleengineService.updateEngineCodeIng(engineversion);
} }
@GetMapping("/engineGeneration")
public Result engineGeneration(@RequestParam String codeIng) {
return ruleengineService.engineGeneration(codeIng);
}
} }

View File

@ -13,7 +13,7 @@ import java.util.List;
/** /**
* Mapper * Mapper
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
public interface RuleengineMapper extends BaseMapper<Ruleengine> { public interface RuleengineMapper extends BaseMapper<Ruleengine> {

View File

@ -12,7 +12,7 @@ import com.muyu.ruleengine.domain.req.RuleContentReq;
/** /**
* Service * Service
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
public interface RuleengineService extends IService<Ruleengine> { public interface RuleengineService extends IService<Ruleengine> {
@ -39,4 +39,6 @@ public interface RuleengineService extends IService<Ruleengine> {
Result updateRuleStatus(Ruleengine ruleengine); Result updateRuleStatus(Ruleengine ruleengine);
Result updateEngineCodeIng(Engineversion engineversion); Result updateEngineCodeIng(Engineversion engineversion);
Result engineGeneration(String codeIng);
} }

View File

@ -28,7 +28,7 @@ import javax.tools.*;
/** /**
* Service * Service
* *
* @author WangLei * @author DeKangLiu
* @date 2024-04-24 * @date 2024-04-24
*/ */
@Slf4j @Slf4j
@ -195,6 +195,41 @@ public class RuleengineServiceImpl extends ServiceImpl<RuleengineMapper, Ruleeng
return Result.success("编码保存成功"); return Result.success("编码保存成功");
} }
@Override
public Result engineGeneration(String codeIng) {
//指定目录
String targetDirectory ="D:\\";
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardFileManager = systemJavaCompiler.getStandardFileManager(null, null, null);
log.info(codeIng);
// JavaSourceFromString javaSourceFromString = new JavaSourceFromString(codeIng);
// JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
// // 创建一个内存中的源文件
// JavaFileObject sourceFileObject = new JavaSourceFromString("Test"+ruleId, content);
//
// // 编译选项
// Iterable<String> options = Arrays.asList("-d", targetDirectory);
//
// // 编译源代码
// JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, Arrays.asList(sourceFileObject));
// boolean success = task.call();
//
// if (success) {
// ruleengineMapper.addRulecontent(ruleContentReq);
// System.out.println("编译成功");
// } else {
// System.out.println("编译失败");
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// return Result.success("编译成功");
return null;
}
public static class MyClassLoader extends ClassLoader { public static class MyClassLoader extends ClassLoader {
public Class<?> defineClassFromBytes(String name, byte[] data) { public Class<?> defineClassFromBytes(String name, byte[] data) {