2010-02-24 127 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> 
相關問題