Passing a dynamic file name after upload to spring batch for processing
I am new to spring batch, what am trying to accomplish is upload a csv file from one application, then send a post request to spring batch with the file name of the uploaded file and have spring batch pick up the file from the location it is in and process it.
I have tried to pass a string value to the reader but i do not know how to access it in the step
// controller where i want to pass the file name to the reader
@RestController
public class ProcessController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job importUserJob;
@PostMapping("/load")
public BatchStatus Load(@RequestParam("filePath") String filePath) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
JobExecution jobExecution = jobLauncher.run(importUserJob, new JobParametersBuilder()
.addString("fullPathFileName", filePath)
.toJobParameters());
return jobExecution.getStatus();
}
}
//reader in my configuration class
@Bean
public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile)
// the problem is here at the .reader chain
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader(reader))
.processor(processor())
.writer(writer())
.build();
}
I expect to pass a filename to the reader amd spring batch can process it
You are correctly passing the full path to the file as a job parameter. However, your reader needs to be step scoped in order to bind the job parameter to the pathToFile
parameter at runtime. This is called late binding because it happens lately at runtime and not early at configuration time (we don't know the parameter value yet at that time).
So in your case, your reader could be like:
@Bean
@StepScope
public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
// return reader;
}
Then you can pass null
to the reader
method in the step definition:
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Person, Person> chunk(10)
.reader(reader(null))
.processor(processor())
.writer(writer())
.build();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With