8

JVM崩溃后如何重新启动作业?

当我的 JVM 崩溃或系统出现故障时,我正在运行许多在 Spring Batch 框架中实现的作业。失败后如何重新启动这些作业?

4

3 回答 3

7

您需要在重新启动它们之前将“正在运行”的作业标记为失败,如下所示:

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);
        }
    }
}
于 2014-07-28T20:30:45.860 回答
6

基本上,您可以执行以下操作:

  1. JobExplorer在应用程序上下文中配置工厂 bean:

  2. JobOperator在您的应用程序上下文中配置bean

  3. 在 jobExplorer 中查询不同的作业名称:jobExplorer.getJobNames()

  4. 对于步骤 (3) 中的每个作业,在 jobExplorer 中查询未完成的作业: jobExplorer.findRunningJobExecutions(String jobName)

  5. 对于JobExecution步骤 (4) 中的每个调用:jobOperator.restart(jobExecution.getJobId())

  6. 确保在启动序列期间调用此过程,然后再启动任何其他作业

从技术上讲,可以findRunningJobExecutions()通过覆盖来合并步骤 3+4 JobExecutionDao,但当前的 API 不支持它。

有关上述 Spring bean 配置的帮助,请参阅参考文档

于 2013-03-17T21:19:20.113 回答
4

这是 JVM 崩溃后重新启动作业的完整解决方案。

  1. 通过设置 restarable="true" 使作业可重新启动

工作 id="jobName" xmlns="http://www.springframework.org/schema/batch" restartable="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"/>
于 2014-07-16T18:29:33.323 回答