在 Spring 批处理 SystemCommandTasklet 中使用 shell 命令
Use shell Command in Spring Batch SystemCommandTasklet
我想 运行 在 spring 批处理 systemCommandTasklet 中执行此命令,但它不起作用。
tail -1 file1.txt > input_footer.txt && head -n -1 file1.txt > input_body.txt
代码:
String Commmand = String.format("tail -1 %s > input_footer.txt && head -n -1 %s > input_body.txt", fileName.getFilename(),fileName.getFilename());
systemCommandTasklet.setCommand(Commmand);
systemCommandTasklet.setWorkingDirectory(woringDir);
systemCommandTasklet.setTimeout(10000);
systemCommandTasklet.setInterruptOnCancel(true);
没有生成输出文件。
有什么解决方法吗?
基本上我想将正文和页脚分成两个文件作为第一步。
我使用 ProcessBuilder 和 MethodInvokingTaskletAdapter 解决了这个问题
public ExitStatus processFile(String fileName, String workingDir) throws InterruptedException, IOException {
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
logger.info("OS windows {} :", isWindows);
String command = String.format("tail -1 %s > input_footer.txt && head -n -1 %s > input_body.txt", fileName, fileName);
logger.info("command {} and working dir{} :", command, workingDir);
ProcessBuilder builder = new ProcessBuilder();
builder.command(bashPath, "-c", command);
builder.directory(new File(workingDir));
Process process = builder.start();
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
ExitStatus status = exitCode == 0 ? ExitStatus.COMPLETED : ExitStatus.FAILED;
return status;
}
public MethodInvokingTaskletAdapter preProcessFileAdapter(@Value("#{jobExecutionContext['customerFile']}") String file, PreProcessFile preProcessFile)
{
String workingDir = FilenameUtils.getFullPath(file);
String fileName = FilenameUtils.getName(file);
MethodInvokingTaskletAdapter methodInvokingTaskletAdapter=new MethodInvokingTaskletAdapter();
methodInvokingTaskletAdapter.setTargetObject(preProcessFile);
methodInvokingTaskletAdapter.setTargetMethod("processFile");
methodInvokingTaskletAdapter.setArguments(new String[]{fileName,workingDir});
return methodInvokingTaskletAdapter;
}
我想 运行 在 spring 批处理 systemCommandTasklet 中执行此命令,但它不起作用。
tail -1 file1.txt > input_footer.txt && head -n -1 file1.txt > input_body.txt
代码:
String Commmand = String.format("tail -1 %s > input_footer.txt && head -n -1 %s > input_body.txt", fileName.getFilename(),fileName.getFilename());
systemCommandTasklet.setCommand(Commmand);
systemCommandTasklet.setWorkingDirectory(woringDir);
systemCommandTasklet.setTimeout(10000);
systemCommandTasklet.setInterruptOnCancel(true);
没有生成输出文件。
有什么解决方法吗? 基本上我想将正文和页脚分成两个文件作为第一步。
我使用 ProcessBuilder 和 MethodInvokingTaskletAdapter 解决了这个问题
public ExitStatus processFile(String fileName, String workingDir) throws InterruptedException, IOException {
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
logger.info("OS windows {} :", isWindows);
String command = String.format("tail -1 %s > input_footer.txt && head -n -1 %s > input_body.txt", fileName, fileName);
logger.info("command {} and working dir{} :", command, workingDir);
ProcessBuilder builder = new ProcessBuilder();
builder.command(bashPath, "-c", command);
builder.directory(new File(workingDir));
Process process = builder.start();
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
ExitStatus status = exitCode == 0 ? ExitStatus.COMPLETED : ExitStatus.FAILED;
return status;
}
public MethodInvokingTaskletAdapter preProcessFileAdapter(@Value("#{jobExecutionContext['customerFile']}") String file, PreProcessFile preProcessFile)
{
String workingDir = FilenameUtils.getFullPath(file);
String fileName = FilenameUtils.getName(file);
MethodInvokingTaskletAdapter methodInvokingTaskletAdapter=new MethodInvokingTaskletAdapter();
methodInvokingTaskletAdapter.setTargetObject(preProcessFile);
methodInvokingTaskletAdapter.setTargetMethod("processFile");
methodInvokingTaskletAdapter.setArguments(new String[]{fileName,workingDir});
return methodInvokingTaskletAdapter;
}