From 1205f84f732610763d46935c0ec31757005376f4 Mon Sep 17 00:00:00 2001 From: Jay <csj123456> Date: 星期二, 15 十月 2024 08:38:27 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java | 106 +++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 86 insertions(+), 20 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java index f9bfa10..8d75179 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java @@ -1,32 +1,29 @@ package com.iailab.module.model.mpk.controller.admin; -import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.converters.longconverter.LongStringConverter; -import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; -import com.iailab.framework.common.enums.CommonStatusEnum; +import com.iail.utils.RSAUtils; import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; import com.iailab.framework.common.pojo.CommonResult; -import com.iailab.framework.excel.core.handler.SelectSheetWriteHandler; -import com.iailab.framework.excel.core.util.ExcelUtils; +import com.iailab.framework.tenant.core.context.TenantContextHolder; +import com.iailab.module.model.mpk.common.MdkConstant; +import com.iailab.module.model.mpk.common.utils.DllUtils; import com.iailab.module.model.mpk.common.utils.Readtxt; import com.iailab.module.model.mpk.dto.MdkDTO; -import com.iailab.module.system.enums.common.SexEnum; +import com.iailab.module.model.mpk.dto.MdkRunDTO; import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.Parameters; -import org.springframework.security.access.prepost.PreAuthorize; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import java.io.File; import java.lang.reflect.Method; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.*; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import static com.iailab.framework.common.pojo.CommonResult.error; import static com.iailab.framework.common.pojo.CommonResult.success; @@ -37,10 +34,46 @@ * @createTime 2024年08月08日 */ @RestController +@Slf4j @RequestMapping("/model/mpk/api") public class MdkController { - @PostMapping("run") - public CommonResult<String> run(@RequestBody MdkDTO dto) { + @Value("${mpk.bak-file-path}") + private String mpkBakFilePath; + + + /** + * @description: 模型测试运行 + * @author: dzd + * @date: 2024/10/14 15:26 + **/ + @PostMapping("test") + public CommonResult<String> test(@RequestBody MdkDTO dto) { + Long tenantId = TenantContextHolder.getTenantId(); + // 备份文件 租户隔离 + String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; + + Class<?> clazz; + URLClassLoader classLoader; + try { + File jarFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar"); + if (!jarFile.exists()) { + throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath()); + } + File dllFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll"); + if (!dllFile.exists()) { + throw new RuntimeException("dll文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath()); + } + // 加载jar包 + classLoader = DllUtils.loadJar(jarFile.getAbsolutePath()); + // 实现类 + clazz = classLoader.loadClass(dto.getClassName()); + // 加载dll到实现类 + DllUtils.loadDll(clazz,dllFile.getAbsolutePath()); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("加载运行环境失败。"); + } + System.out.println("runTime=" + System.currentTimeMillis()); try { List<String> datas = dto.getDatas(); @@ -77,18 +110,51 @@ paramsArray[datas.size()] = HashMap.class; } - Class<?> clazz = Class.forName(dto.getClassName()); - Method method = clazz.getMethod(dto.getMethodName(), paramsArray); - HashMap result = (HashMap)method.invoke(clazz.newInstance(), paramsValueArray); + HashMap result = (HashMap) clazz.getDeclaredMethod(dto.getMethodName(), paramsArray).invoke(clazz.newInstance(), paramsValueArray); return success(JSON.toJSONString(result)); } catch (Exception ex) { ex.printStackTrace(); return error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"运行异常"); } finally { + if (classLoader != null) { + DllUtils.unloadDll(classLoader); + DllUtils.unloadJar(classLoader); + } System.gc(); } } + /** + * @description: 模型运行 + * @author: dzd + * @date: 2024/10/14 15:26 + **/ + @PostMapping("run") + public CommonResult<String> run(@RequestBody MdkRunDTO dto) { + if (RSAUtils.checkLisenceBean().getCode() != 1) { + log.error("Lisence 不可用!"); + return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"Lisence 不可用!"); + } else { + try { + URLClassLoader classLoader = DllUtils.getClassLoader(dto.getProjectId()); + if (null == classLoader) { + return CommonResult.error(GlobalErrorCodeConstants.ERROR_CONFIGURATION.getCode(),"请先发布项目!"); + } + + Class<?> clazz = classLoader.loadClass(dto.getClassName()); + Method method = clazz.getMethod(dto.getMethodName(), dto.getParamsClassArray()); + HashMap invoke = (HashMap) method.invoke(clazz.newInstance(), dto.getParamsValueArray()); + + // todo 将结果存入数据库 + + } catch (Exception e) { + log.error("模型运行失败",e); + return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"模型运行失败!"); + } + } + return CommonResult.success(); + } + @PostMapping("/import") @Operation(summary = "导入参数") public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception { -- Gitblit v1.9.3