Reputation: 9939
I am currently passing the file name in spring batch job parameter in command line and running my job, the spring batch job will look for file and read, process and write the file. I am currently the job parameter file name in reader and reading the file name, how can I use the same job parameter file name in processor and writer.
Upvotes: 0
Views: 1543
Reputation: 57
@StepScope
@Bean
public ItemWriter<EmployeeCSVItem> writer(@Value("#{jobParameters['file.path']}") String filepath) {
return new CsvItemWriter().buildWriter(filepath);
}
Upvotes: 0
Reputation: 872
One of the ways to access the Job Parameters is to implement StepExecutionListener to your processor/writer Class to make use of its Overridden methods beforeStep and afterStep,
public class TestWriter implements ItemWriter<TestData>,StepExecutionListener {
private String filePath;
@Override
public void beforeStep(StepExecution stepExecution) {
try {
filePath = (String) stepExecution.getJobExecution().getExecutionContext()
.get("filePath");
} catch (Exception e) {
logger.error("Exception while performing read {}", e);
}
}
@Override
public void write(List<? extends TestData> items) throws Exception {
// filePath value read from the job execution can be used inside read use case impl
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return ExitStatus.COMPLETED;
}
}
To Add a value to the jobExecutionContext, you can use like below,
stepExecution.getJobExecution().getExecutionContext().put("path", filePath);
Upvotes: 1