[英]Spring Batch how to set filepath in ExecutionContext for next step
I have a spring batch workflow where I read from a flat csv file and write it a csv file.我有一个 spring 批处理工作流,我从一个平面 csv 文件中读取并将其写入 csv 文件。
This is what my ItemWriter looks like:这就是我的 ItemWriter 的样子:
@Configuration
public class MyCSVFileWriter implements ItemWriter<RequestModel> {
private final FlatFileItemWriter<RequestModel> writer;
private ExecutionContext jobContext;
public MyCSVFileWriter() throws Exception {
this.writer = new FlatFileItemWriter<>();
DelimitedLineAggregator<RequestModel> lineAggregator = new DelimitedLineAggregator<>();
BeanWrapperFieldExtractor<RequestModel> extractor = new BeanWrapperFieldExtractor<>();
extractor.setNames(new String[]{"id", "source", "date"});
lineAggregator.setFieldExtractor(extractor);
this.writer.setLineAggregator(lineAggregator);
this.writer.setShouldDeleteIfExists(true);
this.writer.afterPropertiesSet();
}
@Override
public void write(List<? extends RequestModel> items) throws Exception {
this.writer.open(jobContext);
this.writer.write(items);
}
@BeforeStep
public void beforeStepHandler(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
jobContext = jobExecution.getExecutionContext();
this.writer.setResource(new FileSystemResource(getRequestOutputPathResource(jobContext)));
}
private String getRequestOutputPathResource(ExecutionContext jobContext) {
//***
return resourcePath;
}
}
I use the executionContext to extract some data used to calculate my resourcePath
for my writer.我使用 executionContext 来提取一些数据,用于为我的作者计算我的
resourcePath
。
My next step after writing is uploading the file I have written to in previous step to a remote server.编写后的下一步是将我在上一步中写入的文件上传到远程服务器。 For this I need to store the file path which were calculated and store it ExecutionContext and make it available in the next step.
为此,我需要存储计算的文件路径并将其存储为 ExecutionContext 并使其在下一步中可用。
What is the best way to do this?做这个的最好方式是什么? Should I be doing this in a
@AfterStep
handler?我应该在
@AfterStep
处理程序中执行此操作吗?
Should I be doing this in a @AfterStep handler?
我应该在 @AfterStep 处理程序中执行此操作吗?
Yes, this is a good option.是的,这是一个不错的选择。 You can store the path of the file that has been written in the job execution context and read it from there in the next step that uploads the file.
您可以存储已写入作业执行上下文的文件的路径,并在上传文件的下一步中从那里读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.