feat 重构连接池
parent
5e68e0dd94
commit
00534e271a
|
@ -22,7 +22,7 @@ public class ServiceNameConstants {
|
|||
*/
|
||||
public static final String FILE_SERVICE = "muyu-file";
|
||||
|
||||
public static final String MUYU_KVT = "muyu-dataSource";
|
||||
public static final String MUYU_KVT = "muyu-source";
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-modules</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-data-standard</artifactId>
|
||||
<description>muyu-data-standard数据标准包</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 项目核心包-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.common.data.standard;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 数据模型 DataModel
|
||||
*
|
||||
* @author LeYang
|
||||
* on 2024/5/8
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class DataModel {
|
||||
|
||||
|
||||
/**
|
||||
* 数据键
|
||||
*/
|
||||
private String key;
|
||||
|
||||
/**
|
||||
* 数据值
|
||||
*/
|
||||
private Object value;
|
||||
|
||||
/**
|
||||
* 源标准-枚举 数据库的标准
|
||||
*/
|
||||
private String sourceType;
|
||||
|
||||
/**
|
||||
* 处理标准 枚举
|
||||
*/
|
||||
|
||||
private String processType;
|
||||
|
||||
/**
|
||||
* 处理类型
|
||||
*/
|
||||
private Class<?> processClass;
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.muyu.common.data.standard;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 一页 DataSetModel
|
||||
*
|
||||
* @author LeYang
|
||||
* on 2024/5/8
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class DataSetModel {
|
||||
//[[DataModel,DataModel,DataModel],[DataModel,DataModel,DataModel]
|
||||
/**
|
||||
* 集合
|
||||
*/
|
||||
private RecordModel[] dataSetModel = null;
|
||||
|
||||
public int setLength;
|
||||
|
||||
|
||||
public static DataSetModel build(int dataSetModelLength){
|
||||
return build(new RecordModel[dataSetModelLength]);
|
||||
}
|
||||
|
||||
public static DataSetModel build(RecordModel[] dataSetModel){
|
||||
return DataSetModel.builder()
|
||||
.dataSetModel(dataSetModel)
|
||||
.setLength(dataSetModel.length)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.muyu.common.data.standard;
|
||||
|
||||
import com.muyu.common.data.util.EtlUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 记录模型 RecordModel
|
||||
*
|
||||
* @author LeYang
|
||||
* on 2024/5/8
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class RecordModel {
|
||||
//[DataModel,DataModel,DataModel]
|
||||
|
||||
/**
|
||||
* 不用数组处理 1.集合由动态扩容 减少扩容次数
|
||||
* 2.初始化 数组的话一个就是一个 集合是一个的话 是16 多占15个 初始化是16
|
||||
* 指定主键
|
||||
*/
|
||||
private String[] keys;
|
||||
|
||||
/**
|
||||
* 主键数据
|
||||
*/
|
||||
private DataModel[] keyForValues;
|
||||
|
||||
/**
|
||||
* 记录数据 由多个模型组成
|
||||
*/
|
||||
private DataModel[] dataModelArr=null;
|
||||
|
||||
/**
|
||||
* 记录长度
|
||||
*/
|
||||
private int recordLength;
|
||||
|
||||
public static RecordModel build(int dataModelLength,String[] keys){
|
||||
return build(new DataModel[dataModelLength],keys);
|
||||
}
|
||||
|
||||
public static RecordModel build(DataModel[] dataModelArr,String[] keys){
|
||||
return RecordModel.builder()
|
||||
.dataModelArr(dataModelArr)
|
||||
.recordLength(dataModelArr.length)
|
||||
.keys(keys)
|
||||
.keyForValues(
|
||||
Arrays.stream(dataModelArr).filter(dataModel -> EtlUtils.valAsArr(keys,dataModel.getKey()))
|
||||
.toArray(value -> new DataModel[0]))
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.common.data.util;
|
||||
|
||||
/**
|
||||
* etl工具类 EtlUtils
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/5/15 14:26
|
||||
*/
|
||||
public class EtlUtils {
|
||||
|
||||
/**
|
||||
* 判断可以key是否存在数组当中
|
||||
* @param keys key数值
|
||||
* @param key key值
|
||||
* @return 是否存在
|
||||
*/
|
||||
public static boolean valAsArr(String[] keys,String key){
|
||||
for (String _key : keys) {
|
||||
if (_key.equals(key)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -9,8 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -26,26 +24,22 @@ public class KvtClientRunner implements ApplicationRunner {
|
|||
private RemoteDataManagerService remoteDataManagerService;
|
||||
|
||||
|
||||
static HashMap<String, Connection> map = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
public void run(ApplicationArguments args) {
|
||||
|
||||
|
||||
Result<List<Kvt>> listResult = remoteDataManagerService.selectKvt();
|
||||
List<Kvt> data = listResult.getData();
|
||||
Result<List<Kvt>> list = remoteDataManagerService.selectKvt();
|
||||
List<Kvt> data = list.getData();
|
||||
data = data.stream().filter(kvt -> kvt.getType() == 1).toList();
|
||||
if(data != null) {
|
||||
data.stream().forEach(kvt -> {
|
||||
DataSourceConfig.init(kvt) ;
|
||||
log.info("初始化成功");
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
log.warn("数据源为空");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.muyu.source.connection.pool;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 连接池上下文 ConnectionPoolContext
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/5/15 15:05
|
||||
*/
|
||||
@Log4j2
|
||||
public class ConnectionPoolContext {
|
||||
|
||||
private ConnectionPoolContext(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 进行存储所有连接的容器
|
||||
*/
|
||||
private final static ConcurrentHashMap<String, DruidDataSource> connectionPoolContext=
|
||||
new ConcurrentHashMap<>(16);
|
||||
|
||||
/**
|
||||
* 新增连接池
|
||||
* @param key 键
|
||||
* @param druidDataSource 连接池
|
||||
*/
|
||||
public static void setConnection(String key,DruidDataSource druidDataSource){
|
||||
if (connectionPoolContext.containsKey(key)){
|
||||
throw new ServiceException(
|
||||
StringUtils.format("连接池key-[{}]已经存在",key)
|
||||
);
|
||||
}
|
||||
connectionPoolContext.put(key,druidDataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过key获取连接池
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public static DruidDataSource getConnection(String key){
|
||||
return connectionPoolContext.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过key删除连接池
|
||||
* @param key
|
||||
*/
|
||||
public static void removeConnection(String key){
|
||||
try( DruidDataSource druidDataSource= connectionPoolContext.remove(key)){
|
||||
druidDataSource.close();
|
||||
}catch (RuntimeException e){
|
||||
log.warn("连接池关闭异常:[{}]-[{}]",key,e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
package com.muyu.source.connection.service;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.muyu.source.domain.Kvt;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.muyu.source.connection.pool.ConnectionPoolContext.getConnection;
|
||||
|
||||
/**
|
||||
* 连接池的管理 ConnectionPoolManagement
|
||||
*
|
||||
* @author Yangle
|
||||
* Date 2024/5/15 15:19
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class ConnectionPoolManagement {
|
||||
private final static ThreadLocal<Map<DruidPooledConnection,String>> connectionToKey
|
||||
= new ThreadLocal<>();
|
||||
|
||||
public Map<DruidPooledConnection,String> getConnectionToKey() {
|
||||
Map<DruidPooledConnection, String> dataMap = connectionToKey.get();
|
||||
if (dataMap == null){
|
||||
synchronized (connectionToKey){
|
||||
dataMap =new HashMap<>();
|
||||
connectionToKey.set(dataMap);
|
||||
}
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public void init(List<Kvt> kvts) {
|
||||
//初始化连接池
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*/
|
||||
public void createPool() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取
|
||||
*/
|
||||
public DruidPooledConnection get(Kvt kvt){
|
||||
return get(kvt.getKey());
|
||||
}
|
||||
|
||||
public DruidPooledConnection get(String key){
|
||||
DruidDataSource druidDataSource= getConnection(key);;
|
||||
DruidPooledConnection connection =null;
|
||||
try {
|
||||
connection = druidDataSource.getConnection();
|
||||
return connection;
|
||||
} catch (SQLException e) {
|
||||
log.warn("链接获取异常:[{}]-[{}]",key,e.getMessage(),e);
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
getConnectionToKey().put(connection,key);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 归还
|
||||
*/
|
||||
public void returnConnection(DruidPooledConnection connection){
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("归还连接异常:[{}]-[{}]", getConnectionToKey().get(connection),e.getMessage(),e);
|
||||
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
getConnectionToKey().remove(connection);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public void remove(DruidPooledConnection druidPooledConnection){
|
||||
|
||||
}
|
||||
|
||||
public void remove(String key){
|
||||
|
||||
}
|
||||
}
|
|
@ -175,4 +175,8 @@ public class Kvt extends BaseEntity {
|
|||
.build();
|
||||
}
|
||||
|
||||
public String getKey(){
|
||||
return id+name+systemName;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,8 +20,6 @@ public class DataManagerFactory implements FallbackFactory<RemoteDataManagerServ
|
|||
@Override
|
||||
public RemoteDataManagerService create(Throwable cause) {
|
||||
return new RemoteDataManagerService() {
|
||||
|
||||
|
||||
@Override
|
||||
public Result<List<Kvt>> selectKvt() {
|
||||
return Result.error(cause.getMessage());
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.muyu.source.service.KvtService;
|
|||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -39,6 +40,7 @@ Controller
|
|||
@Api(tags = "kvt")
|
||||
@RestController
|
||||
@RequestMapping("/kvt")
|
||||
@Log4j2
|
||||
public class KvtController extends BaseController {
|
||||
@Autowired
|
||||
private KvtService kvtService;
|
||||
|
@ -59,6 +61,7 @@ public class KvtController extends BaseController {
|
|||
@PostMapping("/selectKvt")
|
||||
public Result<List<Kvt>> selectKvt(){
|
||||
List<Kvt> list = kvtService.list();
|
||||
log.info(list);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,12 @@
|
|||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.2.16</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.muyu.engine.java;
|
||||
|
||||
|
||||
import com.muyu.engine.action.ActionDiscard;
|
||||
import com.muyu.engine.scope.DataModelEngine;
|
||||
|
||||
/**
|
||||
* @Author: yl
|
||||
* @date: 2024/5/6
|
||||
* @Description: dd-dd
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class Ss_dd extends DataModelEngine {
|
||||
@Override
|
||||
public void execution () {
|
||||
Object value = getValue();
|
||||
|
||||
if (value == null || "".equals(value) || "null".equals(value)) {
|
||||
throw new ActionDiscard();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -77,11 +77,11 @@ public class RuleEngineController extends BaseController {
|
|||
*/
|
||||
@RequiresPermissions("rule_engine:engine:add")
|
||||
@Log(title = "规则引擎", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("新增规则引擎")
|
||||
public Result add(@RequestBody RuleEngineSaveReq ruleEngineSaveReq) throws Exception {
|
||||
ruleEngineService.add(RuleEngine.saveBuild(ruleEngineSaveReq));
|
||||
return Result.success();
|
||||
return Result.success("添加成功");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -165,13 +167,32 @@ private RuleEngineService ruleEngineService;
|
|||
//
|
||||
// }
|
||||
|
||||
private String url ="D:\\work\\zglkh\\cloud-server\\muyu-modules\\muyu-rule-engine\\muyu-rule-engine-common\\src\\main\\java\\com\\muyu\\engine\\java";
|
||||
|
||||
|
||||
private String url ="D:\\work\\zglkh\\cloud-server\\muyu-modules\\muyu-rule-engine\\muyu-rule-engine-common\\src\\main\\java\\com\\muyu\\engine\\java\\";
|
||||
private String url1 ="D:\\work\\zglkh\\cloud-server\\muyu-modules\\muyu-rule-engine\\muyu-rule-engine-common\\target\\classes\\java\\";
|
||||
|
||||
//生产源文件
|
||||
public void writeCodeAdd(RuleEngineVersion ruleEngineVersion) {
|
||||
try {
|
||||
String originalVersionCode = ruleEngineVersion.getVersionCode();
|
||||
// 提取第一个字符并转换为大写
|
||||
String firstCharUpperCase = originalVersionCode.substring(0, 1).toUpperCase();
|
||||
// 截取从第二个字符到末尾的子串
|
||||
String restOfVersionCode = originalVersionCode.substring(1);
|
||||
// 将首字母大写和剩余部分拼接起来
|
||||
String newVersionCode = firstCharUpperCase + restOfVersionCode;
|
||||
|
||||
String className = newVersionCode;
|
||||
|
||||
//编译
|
||||
String className = ruleEngineVersion.getVersionCode();
|
||||
String path=url+className+".java";
|
||||
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
|
||||
// 写入内容
|
||||
bufferedWriter.write(ruleEngineVersion.getCodeIng());
|
||||
// 关闭流
|
||||
bufferedWriter.close();
|
||||
//编译内容
|
||||
String content = ruleEngineVersion.getCodeIng();
|
||||
// 指定输出目录
|
||||
|
@ -215,4 +236,56 @@ private RuleEngineService ruleEngineService;
|
|||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// private String url ="D:\\work\\zglkh\\cloud-server\\muyu-modules\\muyu-rule-engine\\muyu-rule-engine-common\\src\\main\\java\\com\\muyu\\engine\\java";
|
||||
// private String url1 ="D:\\work\\zglkh\\cloud-server\\muyu-modules\\muyu-rule-engine\\muyu-rule-engine-common\\target\\classes";
|
||||
// // 验证类名,防止路径遍历
|
||||
// // 生产源文件
|
||||
// public void writeCodeAdd(RuleEngineVersion ruleEngineVersion) {
|
||||
// try {
|
||||
//
|
||||
// String className = newVersionCode + ".java";
|
||||
// String content = ruleEngineVersion.getCodeIng();
|
||||
// JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
// StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
||||
// try {
|
||||
// // 写入文件
|
||||
// Path filePath = Paths.get(url, className);
|
||||
// Files.write(filePath, content.getBytes(StandardCharsets.UTF_8));
|
||||
// // 编译
|
||||
// Iterable<String> options = Arrays.asList("-d", url);
|
||||
// JavaFileObject sourceFileObject = new JavaSourceFromString(className, content);
|
||||
// JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, Arrays.asList(sourceFileObject));
|
||||
// boolean success = task.call();
|
||||
// log.info("编译结果: " + (success ? "成功" : "失败"));
|
||||
// } catch (Exception e) {
|
||||
// log.error("编译过程中发生错误", e);
|
||||
// } finally {
|
||||
// fileManager.close();
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.error("写入代码或处理过程中发生错误", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<module>muyu-dataSource</module>
|
||||
<module>muyu-rule-engine</module>
|
||||
<module>muyu-unit</module>
|
||||
<module>muyu-data-standard</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>muyu-modules</artifactId>
|
||||
|
|
Loading…
Reference in New Issue