I am working on a web application, as part of the requirement the application users clicks on a button which is available on the UI so that behind the scenes spring-batch application should trigger and process the data over 15 minutes.
Since the application is web based users can invoke the batch process concurrently at any given point of time.When user performs click action the UI doesn't need to wait for the batch status or response.
I already have the spring-batch process ready which is written in
SimpleJobLauncher, to solve this I am planning to code a REST endpoint to invoke the spring-batch however am not sure about handling the multiple requests from UI.
I am wondering how can I a port my existing synchronous batch process to the new application.
Please advise.
By default, the SimpleJobLauncher uses a synchronous task executor to launch jobs. In your case, you need to use a asynchronous TaskExecutor implementation with the job launcher. This will ensure that jobs are launched concurrently. Here is an example:
@Bean
public JobLauncher jobLauncher() {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
This is explained in details in the Configuring a JobLauncher and Running Jobs from within a Web Container sections of the reference documentation.
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