新增:hpc动态命令参数替换逻辑新增
This commit is contained in:
@@ -10,7 +10,10 @@ public class HPCExecuteConfig extends BaseExecuteConfig {
|
||||
private String masterFileRegularStr = "^.*\\.xml$";
|
||||
// 先默认写死一个,后面前端配置传递
|
||||
private String inputFilesRegularStr="^.+\\.json$";
|
||||
// 节点的命令
|
||||
// 节点的命令 这个不用传递了,配置在后台表里的
|
||||
private String nodeExeCommand;
|
||||
|
||||
// app注册表的uuid
|
||||
private String uuid;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class SubmitHpcTaskRemoteReq implements Serializable {
|
||||
@@ -19,6 +20,9 @@ public class SubmitHpcTaskRemoteReq implements Serializable {
|
||||
@Schema(description = "计算软件")
|
||||
public String software;
|
||||
|
||||
@Schema(description = "计算软件uuid")
|
||||
public String softwareId;
|
||||
|
||||
@Schema(description = "计算任务类型")
|
||||
public String jobType;
|
||||
|
||||
@@ -70,4 +74,7 @@ public class SubmitHpcTaskRemoteReq implements Serializable {
|
||||
@Schema(description = "任务求解文件从文件路径,adapter里使用,executeMode=MANUAL")
|
||||
public List<String> manualInputFilePaths;
|
||||
|
||||
@Schema(description = "任务流用户传递的参数,用于动态替换命令")
|
||||
private Map<String,Object> params;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.sdm.common.utils;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.sdm.common.entity.req.pbs.SubmitHpcTaskRemoteReq;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@Slf4j
|
||||
public class CommandReplaceUtil {
|
||||
/**
|
||||
* 通用替换方法:将命令字符串中的 %key 占位符替换为指定值
|
||||
* @param command 原始命令字符串(包含%key形式的占位符)
|
||||
* @param key 要替换的占位符键名(不需要带%)
|
||||
* @param value 替换后的值
|
||||
* @return 替换完成后的命令字符串
|
||||
*/
|
||||
public static String replaceCommandPlaceholder(String command, String key, Object value) {
|
||||
// 1. 空值校验,避免空指针异常
|
||||
if (command == null) {
|
||||
throw new IllegalArgumentException("命令字符串(command)不能为null");
|
||||
}
|
||||
if (key == null || key.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("占位符键名(key)不能为null或空字符串");
|
||||
}
|
||||
// value允许为空,空值时直接替换成空字符串
|
||||
String replaceValue = ObjectUtil.isEmpty(value) ? "" : value.toString();
|
||||
|
||||
// 2. 构建要替换的占位符(% + key)
|
||||
String placeholder = "%" + key.trim();
|
||||
|
||||
// 3. 替换所有匹配的占位符
|
||||
return command.replace(placeholder, replaceValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段名动态获取对象的字段值
|
||||
* @param obj 目标对象
|
||||
* @param fieldName 字段名(如 masterFileRegularStr)
|
||||
* @return 字段值
|
||||
* @throws NoSuchFieldException 字段不存在
|
||||
* @throws IllegalAccessException 访问权限不足
|
||||
*/
|
||||
public static Object getFieldValue(Object obj, String fieldName) {
|
||||
if (obj == null || fieldName == null || fieldName.isEmpty()) {
|
||||
log.warn("获取对象为空,或者字段名为空");
|
||||
return null;
|
||||
}
|
||||
// 1. 获取对象的Class对象
|
||||
Class<?> clazz = obj.getClass();
|
||||
// 2. 获取字段(包括public/private/protected)
|
||||
Field field = null;
|
||||
try {
|
||||
field = clazz.getDeclaredField(fieldName);
|
||||
} catch (Exception e) {
|
||||
log.warn("反射获取字段{}异常,{}", fieldName,e.getMessage());
|
||||
return null;
|
||||
}
|
||||
// 3. 设置访问权限(即使字段是private也能访问)
|
||||
field.setAccessible(true);
|
||||
// 4. 获取字段值
|
||||
try {
|
||||
Object o = field.get(obj);
|
||||
return o;
|
||||
} catch (Exception e) {
|
||||
log.warn("反射获取字段值{}异常,{}", fieldName,e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1. 创建测试对象
|
||||
SubmitHpcTaskRemoteReq req = new SubmitHpcTaskRemoteReq();
|
||||
req.setJobName("流体力学计算");
|
||||
req.setCoreNum(8);
|
||||
req.setMasterFileRegularStr("/data/input/file.txt");
|
||||
req.setSoftwareId("softwareId11");
|
||||
// 获取String类型字段
|
||||
String jobName = (String) getFieldValue(req, "jobName");
|
||||
System.out.println("jobName = " + jobName); // 输出:流体力学计算
|
||||
|
||||
// 获取int类型字段
|
||||
Integer coreNum = (Integer) getFieldValue(req, "coreNum");
|
||||
System.out.println("coreNum = " + coreNum); // 输出:8
|
||||
|
||||
// 获取private字段(appRepositoryId)
|
||||
String appId = (String) getFieldValue(req, "softwareId");
|
||||
System.out.println("softwareId = " + appId); // 输出:1001
|
||||
|
||||
}
|
||||
|
||||
|
||||
// // 测试示例
|
||||
// public static void main(String[] args) {
|
||||
// // 测试用的原始命令
|
||||
// String command = "\\\\CARSAFE\\share\\solver\\RLithium\\reta.exe -i %masterFilePath -t %coreNum -m %memory";
|
||||
// // 替换coreNum为4
|
||||
// String newCommand = replaceCommandPlaceholder(command, "coreNum", "4");
|
||||
// System.out.println("替换后命令:");
|
||||
// System.out.println(newCommand);
|
||||
//
|
||||
// // 继续替换其他占位符示例
|
||||
// newCommand = replaceCommandPlaceholder(newCommand, "memory", "8G");
|
||||
// System.out.println("\n继续替换memory后:");
|
||||
// System.out.println(newCommand);
|
||||
//
|
||||
//
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user