修改:hpc底层命令拼接增加string 空格字段的处理

This commit is contained in:
2026-01-30 17:32:32 +08:00
parent 387a2fc210
commit bb3fe3c8dd

View File

@@ -36,6 +36,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
@@ -60,6 +61,11 @@ public class HpcInstructionServiceImpl implements HpcInstructionService {
@Qualifier("pbsWebClient")
private WebClient pbsWebClient;
@Value("#{'${hpc.newJob.stringFields:jobname,license,projectname}'.split(',')}")
private List<String> newJobStringFields;
@Value("#{'${hpc.addJob.stringFields:name,license,projectname}'.split(',')}")
private List<String> addJobStringFields;
// 8MB 每次映射
private static final long MAP_SIZE = 10 * 1024 * 1024;
@@ -108,6 +114,8 @@ public class HpcInstructionServiceImpl implements HpcInstructionService {
String prefixStr = HpcCommandBuilderUtil.initNewJobPrefixStr();
NewJobParam newJobParam = new NewJobParam();
BeanUtils.copyProperties(req, newJobParam);
// 处理d对象中可能包含空的字符串
wrapStringFieldsWithQuotes(newJobParam,newJobStringFields);
String newJobCommand = HpcCommandBuilderUtil.buildHpcCommandStr(prefixStr, newJobParam, "");
String result = hpcCommandExcuteUtil.excuteCmd(newJobCommand);
NewJobResp newJobResp = HpcCommandResulParseUtil.parseJobNewResult(result);
@@ -124,6 +132,8 @@ public class HpcInstructionServiceImpl implements HpcInstructionService {
String prefixStr = HpcCommandBuilderUtil.initAddJobPrefixStr(req.getJobId());
AddJobParam addJobParam = new AddJobParam();
BeanUtils.copyProperties(req, addJobParam);
// 处理对象中可能包含空的字符串
wrapStringFieldsWithQuotes(addJobParam,addJobStringFields);
// String targetWorkDir = addJobParam.getWorkdir() + "\\" + req.getJobId();
String targetWorkDir = addJobParam.getWorkdir();
Pair<Boolean, String> workDirPair = createDirIfNotExist(targetWorkDir);
@@ -578,5 +588,66 @@ public class HpcInstructionServiceImpl implements HpcInstructionService {
return pair;
}
// public static void main(String[] args) {
// // license
// List<String>list=new ArrayList<>();
// list.add("license");
// list.add("jobname");
// list.add("projectname");
// NewJobParam newJobParam = new NewJobParam();
// newJobParam.setJobname("TS 任务 0927");
// newJobParam.setProjectname("lyric bg 项目");
// wrapStringFieldsWithQuotes(newJobParam,list);
//
// }
/**
* 为任意Java Bean对象的指定字符串字段拼接双引号
* @param bean 待处理的对象(不能为空)
* @param stringFields 需要处理的字段名列表(字段名需与对象属性名完全一致)
*/
public static void wrapStringFieldsWithQuotes(Object bean, List<String> stringFields) {
// 1. 入参合法性校验
if (bean == null || stringFields == null || stringFields.isEmpty()) {
log.warn("wrapStringFieldsWithQuotes 入参为空,无需处理");
return;
}
// 2. 获取对象的Class支持任意Java Bean
Class<?> beanClass = bean.getClass();
// 3. 遍历需要处理的字段列表
for (String fieldName : stringFields) {
// 跳过空的字段名
if (StringUtils.isBlank(fieldName)) {
log.warn("wrapStringFieldsWithQuotes 字段空,无需处理:{}", fieldName);
continue;
}
try {
// 3.1 获取指定字段(设置可访问私有字段)
Field field = beanClass.getDeclaredField(fieldName);
field.setAccessible(true);
// 3.2 获取字段当前值
Object fieldValue = field.get(bean);
// 3.3 仅处理:字符串类型 + 非null + 非空字符串
if (fieldValue instanceof String strValue && StringUtils.isNotBlank(strValue)) {
// 拼接双引号
String wrappedValue = "\"" + strValue + "\"";
// 将新值设置回对象
field.set(bean, wrappedValue);
}
} catch (NoSuchFieldException e) {
// 字段不存在时的友好提示(不同对象字段可能不同,不中断流程)
log.error("wrapStringFieldsWithQuotes 对象:{},不存在字段:{},异常:{}",beanClass.getSimpleName(),fieldName,e.getMessage());
} catch (IllegalAccessException e) {
// 字段访问权限异常
log.error("wrapStringFieldsWithQuotes 无法访问对象:{},字段:{},异常:{}",beanClass.getSimpleName(),fieldName,e.getMessage());
} catch (Exception e) {
// 兜底异常处理,避免单个字段处理失败影响整体
log.error("wrapStringFieldsWithQuotes 处理对象:{},字段:{},异常:{}",beanClass.getSimpleName() ,fieldName,e.getMessage());
}
}
}
}