具有任务执行程序的Spring批处理不会并行运行

时间:2014-03-06 07:19:35

标签: spring task spring-batch executorservice

我有一个弹簧批量配置如下:

<beans>
<bean id="taskExecutor"
          class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="25"/>
</bean>

<batch:job id="springJobBatch1">
        <batch:step id="step1" >
            <batch:tasklet task-executor="taskExecutor">
                <batch:chunk reader="Reader1" writer="Writer1" commit-interval="1000" />
            </batch:tasklet>
        </batch:step>
        <batch:listeners>
            <batch:listener ref="Listener1"/>
        </batch:listeners>
</batch:job>
<bean id="Reader1" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">            
       <property name="dataSource" ref="testdsref1" />
       <property name="queryProvider">
              <bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
               <property name="dataSource" ref="testdsref1" />
                  <property name="selectClause" value="select..."/>
                  <property name="fromClause" value="from..."/>
                  <property name="whereClause" value="where..."/>
                  <property name="sortKey" value="order..." />
             </bean>
        </property>            
        <property name="pageSize" value="1000"/>
        <property name="saveState" value="false"/>
        <property name="rowMapper" ref="testmapper" />
</bean>

<bean id="testRunJob1" class="package1.anyClass.thread1">
        <property name="cache" ref="testdsref2"/>
        property name="jobLauncher" ref="jobLauncher" />
        <property name="reader" ref="springJobBatch1"/>
</bean>

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository">
        <constructor-arg>
            <bean class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
        </constructor-arg>
        <constructor-arg>
        <bean class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
        </constructor-arg>
        <constructor-arg>
        <bean class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
        <bean class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
        </constructor-arg>
</bean>
<bean id="Thread1" class="java.lang.Thread">
        <constructor-arg index="0" type="java.lang.Runnable" ref="testRunJob1" ></constructor-arg>
</bean>    
<bean id="Thread2" class="java.lang.Thread">
        <constructor-arg index="0" type="java.lang.Runnable" ref="testRunJob2" ></constructor-arg>
</bean>   

<bean id="TheChosenOneThread" init-method="initMethod">
        <property name="thread1" ref="Thread1"/>
        <property name="thread2" ref="Thread2"/>
</bean>   
</beans>

public class TheChosenOneThread{
thread1 t1;
thread2 t2;

public void initMethod(){
    ExecutorService t1 = Executors.newFixedThreadPool(NUMBEROFTHREADS);
    executor.execute(t1);
    executor.execute(t2);
    executor.shutdown();
    try{
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    }
    catch(Exception e){
            System.out.println("cache restart timed-out.");
    }

}
预期


要求是确保Thread1和Thread2并行执行 并且服务器应该在完成后才启动。 实际:

当服务器启动时,所有bean都会被初始化。随之而来的是,“TheChosenOneThread”线程的initMethod也会被执行。 但是线程t1和t2与主服务器线程一起停留,服务器永远不会启动。 如果可能的话,请给我一个更简单的方法。

1 个答案:

答案 0 :(得分:3)

您不应该手动创建线程并调用执行程序。设置executorService的{​​{1}},然后用两个作业调用SimpleJobLauncher。它们将并行运行,因为runJob有超过1个线程。 (并且您可以删除Thread1和Thread2 bean。)

要检查完成情况,请定期检查executorService中返回的JobExecution,或使用runJob