在 Spring Batch SystemCommandTasklet 中使用 shell Command



我想在 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 > 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;
}

最新更新