我有一個Spring-Batch作業,我從Spring MVC控制器啓動。該控制器從用戶獲取上傳的文件和作業應該處理文件:Spring批處理:從Spring MVC控制器啓動一個作業WITH NEW THREAD
@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;
}
我嘗試下面的XML配置:
<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>
...但它似乎像多線程只發生在工作範圍內。即,控制器線程一直等到作業結束,並且作業執行由多個線程處理(這是好的,但不是我想要的主要事情)。我想要的主要事情是作業將在單獨的線程(或多個線程)上啓動,而控制器線程將繼續執行而不等待作業線程結束。
有沒有辦法用Spring-batch實現這一點?
謝謝!正是我想要的!初學者的運氣:) – rapt 2012-02-02 15:32:48
第二個鏈接是404,請更新正確的鏈接。 – Rembo 2015-01-13 07:37:43