Java 在Spring批处理SystemCommandTasklet中使用shell命令
我想在spring batch systemCommandTasklet中运行此命令,但它不起作用Java 在Spring批处理SystemCommandTasklet中使用shell命令,java,spring,spring-boot,spring-batch,Java,Spring,Spring Boot,Spring Batch,我想在spring batch 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 >
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;
}
我通过使用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;
}
我不确定这个问题与java或spring有何关系
head
和tail
默认打印10行,这里您指定了1行。抱歉,更新了问题我不确定这个问题与java或spring的关系<代码>头部和尾部
默认打印10行,这里您指定了1行。抱歉,更新了问题