I have a Spring -Batch job that I run from Spring MVC. The controller receives the downloaded file from the user, and the task must process the file:
@RequestMapping(value = "/upload") public ModelAndView uploadInventory(UploadFile uploadFile, BindingResult bindingResult) { // code for saving the uploaded file to disk goes here... // now I want to launch the job of reading the file line by line and saving it to the database, // but I want to launch this job in a new thread, not in the HTTP request thread, // since I so not want the user to wait until the job ends. jobLauncher.run( jobRegistry.getJob(JOB_NAME), new JobParametersBuilder().addString("targetDirectory", folderPath).addString("targetFile", fileName).toJobParameters() ); return mav; }
I tried the following XML configuration:
<job id="writeProductsJob" xmlns="http://www.springframework.org/schema/batch"> <step id="readWrite"> <tasklet task-executor="taskExecutor"> <chunk reader="productItemReader" writer="productItemWriter" commit-interval="10" /> </tasklet> </step> </job> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="5" /> </bean>
... but it seems that multithreading only occurs within the boundaries of the work itself. Ie, the controller thread is waiting for the job to finish, and the job is being processed by several threads (which is good, but not the main thing I wanted). The main thing I wanted was that the task will be launched in a separate thread (or threads), while the controller thread will continue to execute it, without waiting for the completion of the task threads.
Is there a way to achieve this using Spring-batch?
source share