SpringBatch 启动过程

使用SpringBatch启动时发现会自动启动配置的job,由此观察了一下启动的过程

启动时会加载BatchAutoConfiguration,针对启动的配置

@Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
        prefix = "spring.batch.job",
        name = {"enabled"},
        havingValue = "true",
        matchIfMissing = true
    )
    public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(JobLauncher jobLauncher, JobExplorer jobExplorer, JobRepository jobRepository, BatchProperties properties) {
        JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(jobLauncher, jobExplorer, jobRepository);
        String jobNames = properties.getJob().getNames();
        if (StringUtils.hasText(jobNames)) {
            runner.setJobNames(jobNames);
        }

        return runner;
    }

首先如果想关闭自动启动

在 application.properties文件加上如下配置:

项目启动时不调用 Job 
spring.batch.job.enabled = false

项目启动时调用 Job 
spring.batch.job.enabled = true

如果是true,会执行JobLauncherCommandLineRunner中的run方法,之后会调用

protected void launchJobFromProperties(Properties properties) throws JobExecutionException {
        JobParameters jobParameters = this.converter.getJobParameters(properties);
        this.executeLocalJobs(jobParameters);
        this.executeRegisteredJobs(jobParameters);
    }

这些方法中有 

this.execute(job, jobParameters);

调用JobLauncher.run方法,其中SimpleJobLauncher去实现的run方法

//查询上次未执行完的job继续执行,如果执行完成则也不再执行
JobExecution lastExecution = this.jobRepository.getLastJobExecution(job.getName(), jobParameters);

//如果没有执行完成的job会创建一个新的执行
final JobExecution jobExecution = this.jobRepository.createJobExecution(job.getName(), jobParameters);

JobInstance jobInstance = this.jobInstanceDao.getJobInstance(jobName, jobParameters);
        ExecutionContext executionContext;
        if (jobInstance == null) {
            jobInstance = this.jobInstanceDao.createJobInstance(jobName, jobParameters);
            executionContext = new ExecutionContext();
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值