2013-03-17 70 views
6

如何在JVM崩潰後重新啓動作業?JVM崩潰後的Spring批處理

我在Spring Batch框架中運行了很多Jobs,當我的JVM崩潰或系統失敗時。如何在失敗後重新啓動這些作業?

回答

6

基本上,你可以做如下:

  1. 配置一個JobExplorer工廠bean在你的應用環境:

  2. 配置一個JobOperator豆你applictaion方面

  3. 查詢的jobExplorer針對不同的職位名稱:jobExplorer.getJobNames()

  4. 對於來自步驟(3),查詢jobExplorer對未完成的作業每個作業: jobExplorer.findRunningJobExecutions(String jobName)

  5. 對於每個JobExecution步驟(4)調用:jobOperator.restart(jobExecution.getJobId())

  6. 確保引導過程中調用這個過程中,任何其他工作啓動

之前在技術上可以通過覆蓋JobExecutionDao合併步驟3 + 4這樣的事情findRunningJobExecutions(),但目前API不支持它。

對於上述的Spring bean配置的幫助,請諮詢reference documentation

+0

請[見我的問題](http://stackoverflow.com/questions/40990935/resume-a-spring-batch-job-from-last-processed-point)。我試圖處理相同的情況,工作正在開始,但已經處理的塊不會被自動考慮。 – 2016-12-06 10:18:51

+0

這是在春季啓動工程?我的意思是我們能夠處理Spring啓動應用程序是否有異常並崩潰並重新運行? – Kenji 2017-08-12 10:42:52

2

下面是完整的解決方案,重新啓動JVM崩潰後的工作。

  1. 通過使restarable = 「真」

作業id = 「工作名」 的xmlns = 「http://www.springframework.org/schema/batch」 重新啓動做工作重新啓動= 「true」

2。代碼重新開始工作

import java.util.Date; 
import java.util.List; 
import org.apache.commons.collections.CollectionUtils; 
import org.springframework.batch.core.BatchStatus; 
import org.springframework.batch.core.ExitStatus; 
import org.springframework.batch.core.JobExecution; 
import org.springframework.batch.core.JobInstance; 
import org.springframework.batch.core.explore.JobExplorer; 
import org.springframework.batch.core.launch.JobLauncher; 
import org.springframework.batch.core.launch.JobOperator; 
import org.springframework.batch.core.repository.JobRepository; 
import org.springframework.beans.factory.annotation.Autowired; 

public class ResartJob { 

    @Autowired 
    private JobExplorer jobExplorer; 
    @Autowired 
    JobRepository jobRepository; 
    @Autowired 
    private JobLauncher jobLauncher; 
    @Autowired 
    JobOperator jobOperator; 

    public void restart(){ 
     try { 
      List<JobInstance> jobInstances = jobExplorer.getJobInstances("jobName",0,1);// this will get one latest job from the database 
      if(CollectionUtils.isNotEmpty(jobInstances)){ 
       JobInstance jobInstance = jobInstances.get(0); 
       List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance); 
       if(CollectionUtils.isNotEmpty(jobExecutions)){ 
        for(JobExecution execution: jobExecutions){ 
         // If the job status is STARTED then update the status to FAILED and restart the job using JobOperator.java 
         if(execution.getStatus().equals(BatchStatus.STARTED)){ 
          execution.setEndTime(new Date()); 
          execution.setStatus(BatchStatus.FAILED);        
          execution.setExitStatus(ExitStatus.FAILED);        
          jobRepository.update(execution); 
          jobOperator.restart(execution.getId()); 
         } 
        } 
       } 
      } 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 
    } 
} 

3.

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean" p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:lobHandler-ref="oracleLobHandler"/> 

<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/> 


<bean id="jobExplorer" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean" p:dataSource-ref="dataSource" /> 

<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" /> 

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
     <property name="jobRepository" ref="jobRepository" /> 
     <property name="taskExecutor" ref="jobLauncherTaskExecutor" /> 
</bean> 
<task:executor id="jobLauncherTaskExecutor" pool-size="6" rejection-policy="ABORT" /> 

<bean id="jobOperator" class="org.springframework.batch.core.launch.support.SimpleJobOperator" p:jobLauncher-ref="jobLauncher" p:jobExplorer-re`enter code here`f="jobExplorer" p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry"/> 
4

您需要標註 「跑」 的作業重新啓動之前爲失敗,就像這樣:

List<String> jobs = jobExplorer.getJobNames(); 
for (String job : jobs) { 
    Set<JobExecution> runningJobs = jobExplorer.findRunningJobExecutions(job); 

    for (JobExecution runningJob : runningJobs) { 
     try { 
      runningJob.setStatus(BatchStatus.FAILED); 
      runningJob.setEndTime(new Date()); 
      jobRepository.update(runningJob); 
      jobOperator.restart(runningJob.getId()); 
     } catch (Exception e) { 
      LOG.error(e.getMessage(), e); 
     } 
    } 
}