1.SpringBatch-集成到SpringBoot

引入maven

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>

SringBatch配置demo类

@Configuration
 public class BatchConfiguration {

   @Autowired
   private JobBuilderFactory jobBuilderFactory;

   @Autowired
   private StepBuilderFactory stepBuilderFactory;

   @Bean
   public Step step1() {
     return stepBuilderFactory.get("step1")
         .tasklet(new Tasklet() {
           public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
             return null;
           }
         })
         .build();
   }

   @Bean
   public Job job(Step step1) throws Exception {
     return jobBuilderFactory.get("job1")
         .incrementer(new RunIdIncrementer())
         .start(step1)
         .build();
   }
 }

增加自动配置注解

给SpringBoot启动类增加自动执行注解
这个注解加上之后SpringBoot启动时会制动执行你的所有的Job

 @EnableBatchProcessing

在spring配置文件中配置SpringBatch

spring: 
    batch: 
        initialize-schema: ALWAYS
        table-prefix: 
        schema: 
        job:
            names: 
            enabled:
        initializer:
            enabled: 

属性介绍

相关类:org.springframework.boot.autoconfigure.batch.BatchProperties

属性值默认值可选属性说明
initialize-schemaEMBEDDEDALWAYS,MBEDDED,NEVER是否初始化数据库:一直、嵌入式、绝不
table-prefixnull自定义字符串表格前缀
schemajar包里的sql文件路径sql文件路径sql文件内容
job.namesnull以,分割的字符串需要执行的job名称
job.enabledtruetrue/false是否项目启动时执行job默认执行
initializer.enabledtruetrue/false是否初始化,在2.0.x中已经废除用initialize-schema替代

源码分析

job.enabled使用在org.springframework.boot.autoconfigure.batch.BatchAutoConfigurationjobLauncherCommandLineRunner方法中

jobnames的使用在org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner 如果jobNames为空或者无值那么其将执行所有的job,如果不为空那么只执行jobNames中的job

private void executeLocalJobs(JobParameters jobParameters)
			throws JobExecutionException {
		for (Job job : this.jobs) {
			if (StringUtils.hasText(this.jobNames)) {
				String[] jobsToRun = this.jobNames.split(",");
				if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
					logger.debug("Skipped job: " + job.getName());
					continue;
				}
			}
			execute(job, jobParameters);
		}
	}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值