2010-02-24 53 views
12

我已经成功设置了一个教程Spring Batch项目。我真的很想知道是否有可能在“Spring层面”使它成为多线程的。如何在Spring批处理中设置多线程?

我想要的基本思想是制作任务或任务步骤的列表,并让它们由独立线程拾取和处理,理想情况是从限制为'n'个线程的池中完成。

这可能吗?如果是这样,怎么样?有人可以指导我从那里我目前在哪里?

我有这个简单的项目是从这个教程here。它基本上有不同的任务,将消息打印到屏幕上。

这是我目前的simpleJob.xml文件,其中包含工作细节:

<import resource="applicationContext.xml"/> 

    <bean id="hello" class="helloworld.PrintTasklet"> 
     <property name="message" value="Hello"/> 
    </bean> 

    <bean id="space" class="helloworld.PrintTasklet"> 
     <property name="message" value=" "/> 
    </bean> 

    <bean id="world" class="helloworld.PrintTasklet"> 
     <property name="message" value="World!\n"/> 
    </bean> 

    <bean id="taskletStep" class="org.springframework.batch.core.step.tasklet.TaskletStep" > 
     <property name="jobRepository" ref="jobRepository"/> 
     <property name="transactionManager" ref="transactionManager"/> 
    </bean> 

    <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob"> 
     <property name="name" value="simpleJob" /> 
     <property name="steps"> 
      <list> 
       <bean parent="taskletStep"> 
        <property name="tasklet" ref="hello"/> 
       </bean> 
       <bean parent="taskletStep"> 
        <property name="tasklet" ref="space"/> 
       </bean> 
       <bean parent="taskletStep"> 
        <property name="tasklet" ref="world"/> 
       </bean> 
      </list> 
     </property> 
     <property name="jobRepository" ref="jobRepository"/> 
    </bean> 

我appContext包含作业库豆(SimpleJobRepository),事务管理器(ResourceLessTransactionManager)和作业启动(SimpleJobLauncher)。如果需要的话,我也可以提供这些代码,我只是不想用大量的XML来消化这篇文章。

非常感谢您的帮助!

回答

10

创建一个拆分,你将能够在不同的分支之间使用多线程。 使用TaskExecutor来定义您的并行策略。

Multi-threaded Step

要确保,如果你想使用多线程和MapJobRepository(之前的最新版本,这是JobRepository不是线程安全的)使用最新版本Spring Batch的的。

4

看看让答案'。一个简单的方法是创建一个SimpleAsyncTaskExecutor的bean,并将一个tasklet与这个bean相关联,就像那样。

<bean id="simpleTaskExecutor" 
    class="org.springframework.core.task.SimpleAsyncTaskExecutor"> 
    <property name="concurrencyLimit" value="10"/> 
</bean> 

<batch:job id="jobTest"> 
    <batch:step id="step1"> 
    <!-- throttle-limit default is 4. Increase this to ensure that a thread pool is fully utilized --> 
     <batch:tasklet task-executor="simpleTaskExecutor" throttle-limit="20"> 
      <batch:chunk /> 
     </batch:tasklet> 
    </batch:step> 
</batch:job> 
相关问题