2014-03-06 64 views
0

運行我有一個春天批量配置如下:春季批次任務執行人不併行

<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."); 
    } 

} 

預計:
的要求是,以確保該線程1和線程並行 和執行服務器只有在完成後才能啓動。 實際:

當服務器啓動時,所有的bean都被初始化。隨之,「TheChosenOneThread」線程的initMethod也被執行。 但是線程t1和t2與主服務器線程和服務器一起卡住,永遠不會啓動。 如果可能,請給我建議一個更簡單的方法。

回答

3

您不應該手動創建線程並調用執行程序。設置SimpleJobLauncherexecutorService,並撥打runJob與您的兩份工作。它們將並行運行,因爲executorService有多個線程。 (並且您可以刪除您的Thread1和Thread2豆。)

要檢查完成情況,請定期檢查返回的JobExecution,從runJob或使用JobExecutionListener

+0

我無法理解你最後的陳述。你可以看看我提出的第二個答案,在提出了一些改動之後? – krupalpatel86