美文网首页
spring boot batch学习

spring boot batch学习

作者: _oeo___ | 来源:发表于2018-06-06 22:37 被阅读42次

1.简介

Spring boot batch 一共有两种任务运行方式。

  1. 使用tasklet 只执行一个Tasklet.execute() 然后你可以使用jobs.start(xx).next(xx2).next(xx3);

  2. 使用 this.step.chunck().reader().processor().writer().build();

2.总结:

使用完之后感觉麻烦、不好用(可能我还没有理解到他的精髓),然后我用CommandLineRunner@Order(1)实现了任务的顺序调用问题。

3.下面是Spring boot batch的例子:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * 1. 使用tasklet 只执行一个Tasklet.execute() 然后你可以使用jobs.start(xx).next(xx2).next(xx3);
 * 2. 使用 this.step.chunck().reader().processor().writer().build();
 */
@SpringBootApplication
@EnableBatchProcessing
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class SpiderBatchApplication {

    private static final Logger logger = LoggerFactory.getLogger(SpiderBatchApplication.class);

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Autowired
    MongoTemplate mongoTemplate;

    @Bean
    protected Tasklet tasklet() {

        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution,
                    ChunkContext context) {
                logger.info("======tasklet");
                return RepeatStatus.FINISHED;
            }
        };

    }

    @Bean
    public Job job() throws Exception {
        logger.info("======job");
        return this.jobs.get("job").start(step1()).build();
    }

    @Bean
    protected Step step1() throws Exception {
        logger.info("======step1");
        //1. 第一种方式
        //this.steps.get("step1").chunk(10).reader(null).processor(null).writer(null).build();
        //2. 第二种方式
        return this.steps.get("step1").tasklet(tasklet()).build();
    }

    /**
     * job->step->tasklet
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // System.exit is common for Batch applications since the exit code can be used to
        // drive a workflow
        logger.info("======================");
        System.exit(SpringApplication
                .exit(SpringApplication.run(SpiderBatchApplication.class, args)));
    }

}

相关文章

网友评论

      本文标题:spring boot batch学习

      本文链接:https://www.haomeiwen.com/subject/tjoisftx.html