我有一个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