Is it a bad idea to change the Spring Batch Meta-Data tables manually?

后端 未结 2 1460
陌清茗
陌清茗 2021-01-02 19:04

Background

I\'m using Spring Batch 2.1.8, and run jobs by CommandLineJobRunner. Such as:

java org.springframework.batch.core.launch.su         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 19:56

    I created a specific spring bean for this which is triggered on a container refresh (which happens on app (re)start too).

    It searches for 'running' jobs, marks them 'FAILED' and restarts them.

    import java.util.Date;
    import java.util.List;
    import java.util.Set;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import org.springframework.batch.core.BatchStatus;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.explore.JobExplorer;
    import org.springframework.batch.core.launch.JobOperator;
    import org.springframework.batch.core.repository.JobRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BatchJobRestarter implements ApplicationListener {
    
        private static final Logger LOGGER  = LoggerFactory.getLogger(BatchJobRestarter.class);
    
        @Autowired
        private JobExplorer         jobExplorer;
    
        @Autowired
        JobRepository               jobRepository;
    
        @Autowired
        JobOperator                 jobOperator;
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            LOGGER.info("Container restart: restarting 'running' batch jobs");
            List jobs = jobExplorer.getJobNames();
            for (String job : jobs) {
                Set runningJobs = jobExplorer.findRunningJobExecutions(job);
    
                for (JobExecution runningJob : runningJobs) {
                    try {
                        LOGGER.info("Restarting job {} with parameters {}", runningJob.getJobInstance().getJobName(), runningJob.getJobParameters().toString());
                        runningJob.setStatus(BatchStatus.FAILED);
                        runningJob.setEndTime(new Date());
                        jobRepository.update(runningJob);
                        jobOperator.restart(runningJob.getId());
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
            }
        }
    }
    

    Steef

提交回复
热议问题