Reputation: 191
I have set up a java batch project with spring batch that allows to persist the rows of a CSV in a table of a database. I would have liked to know if it was possible with Spring API REST to trigger the Batch with a POST method that would join the necessary CSV.
thank you in advance
Upvotes: 1
Views: 10873
Reputation: 408
You can do that using a Controller
with a JobLauncher
and Job
. The barebones of the controller would be like this
@RestController
public class MyController{
// Usually given by Spring Batch
private JobLauncher jobLauncher;
// Your Job
private Job job;
// Ctor
public MyController(JobLauncher jobLauncher, Job job, ...){}
@PostMapping("/")
public String launchJob(...){
...
// Create JobParameters and launch
JobParameters jobparameters = new Job Parameters();
jobLauncher.run(job, jobParameters);
...
}
}
SimpleJobLauncher, the implementation of JobLauncher, uses a synchronous executor by default, you'll probably want to change it to an Async one depending of your requirements
Upvotes: 4