Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a dynamic file name to process from a post request

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

like image 805
user2293554 Avatar asked May 06 '19 03:05

user2293554


1 Answers

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();
}
like image 159
Mahmoud Ben Hassine Avatar answered Sep 28 '22 01:09

Mahmoud Ben Hassine