From a23e66f179586eeffc55d116f5a3bb53eab9cb09 Mon Sep 17 00:00:00 2001 From: dongyukun <1208714201@qq.com> Date: 星期五, 03 一月 2025 14:00:17 +0800 Subject: [PATCH] 调度方案批量启禁 --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java | 82 +++++++++++++++++++++++++++++++++------- 1 files changed, 67 insertions(+), 15 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java index 94b3964..74df6bb 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java @@ -1,5 +1,8 @@ package com.iailab.module.model.mpk.common.utils; +import cn.hutool.core.io.FileUtil; +import com.iail.model.IAILModel; +import com.iail.utils.RSAUtils; import com.iailab.module.model.mpk.common.MdkConstant; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -9,14 +12,18 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Vector; +import java.util.stream.Collectors; @Slf4j public class DllUtils { private static HashMap<String, URLClassLoader> classLoaderCache = new HashMap<>(); + private static HashMap<String, Object> classCache = new HashMap<>(); + private static HashMap<String, Method> classMethodCache = new HashMap<>(); /** * @description: 加载dll到指定class下 @@ -144,6 +151,23 @@ unloadDll(urlClassLoader); unloadJar(urlClassLoader); classLoaderCache.remove(projectId); + removeClassCache(projectId); + removeClassMethodCache(projectId); + } + } + public static synchronized void removeClassCache(String projectId) { + for (String key : classCache.keySet()) { + if (key.startsWith(projectId)) { + classCache.remove(key); + } + } + } + + public static synchronized void removeClassMethodCache(String projectId) { + for (String key : classMethodCache.keySet()) { + if (key.startsWith(projectId)) { + classMethodCache.remove(key); + } } } @@ -181,27 +205,55 @@ String jarFilePath = bakPath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"; String dllFilePath = bakPath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"; - URLClassLoader urlClassLoader = null; - try { - // 加载新的jar - urlClassLoader = DllUtils.loadJar(jarFilePath); - } catch (Exception e) { - throw new RuntimeException("加载jar异常",e); - } + if (FileUtil.exist(jarFilePath) && FileUtil.exist(dllFilePath)) { + URLClassLoader urlClassLoader = null; + try { + // 加载新的jar + urlClassLoader = loadJar(jarFilePath); + } catch (Exception e) { + throw new RuntimeException("加载jar异常",e); + } - try { - // 加载新的dll - DllUtils.loadDll(urlClassLoader.loadClass("iail.mdk.model.common.Environment"),dllFilePath); - } catch (Exception e) { - DllUtils.unloadJar(urlClassLoader); - throw new RuntimeException("加载dll异常",e); + try { + // 加载新的dll + loadDll(urlClassLoader.loadClass("iail.mdk.model.common.Environment"),dllFilePath); + } catch (Exception e) { + unloadJar(urlClassLoader); + throw new RuntimeException("加载dll异常",e); + } + // 都加载成功后加入缓存 + addClassLoaderCache(projectId,urlClassLoader); } - // 都加载成功后加入缓存 - DllUtils.addClassLoaderCache(projectId,urlClassLoader); } } } } } + + public static HashMap<String, Object> run(IAILModel model, Object[] paramsValueArray, String projectId) throws Exception { + if (RSAUtils.checkLisenceBean().getCode() != 1) { + throw new SecurityException("Lisence 不可用!"); + } else if (model == null) { + throw new RuntimeException("模型文件不能为空!"); + } else { + String classCacheKey = projectId + "_" + model.getClassName(); + String methodParams = Arrays.stream(model.getParamsArray()).map(e -> e.getName()).collect(Collectors.joining(",")); + String classMethodCacheKey = classCacheKey + "." + model.getMethodName() + "(" + methodParams + ")"; + if (classCache.containsKey(classCacheKey) && classMethodCache.containsKey(classMethodCacheKey)) { + return (HashMap)classMethodCache.get(classMethodCacheKey).invoke( classCache.get(classCacheKey), paramsValueArray); + }else { + URLClassLoader classLoader = DllUtils.getClassLoader(projectId); + if (null == classLoader) { + throw new RuntimeException("dll未发布,classLoader为null"); + } + Class<?> clazz = classLoader.loadClass(model.getClassName()); + Object o = clazz.newInstance(); + Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray()); + classCache.put(classCacheKey,o); + classMethodCache.put(classMethodCacheKey,method); + return (HashMap)method.invoke(o, paramsValueArray); + } + } + } } -- Gitblit v1.9.3