印迹

Spring boot batch

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`** 的最基本的操作就是执行一个进程 也就是 **`CommandLineRunner`**。如果要使用`spring batch` 那么就可以研究下`spring boot batch` 参考另一篇博文

CommandLineRunner.java

你需要做的就是实现run() 方法,并不是一个线程
你可以使用Order(num)来实现流程的控制。
原型为下:

#CommandLineRunner.java 

public interface CommandLineRunner {
    void run(String... var1) throws Exception;
}

#ApplicationRunner.java
public interface ApplicationRunner {
    void run(ApplicationArguments var1) throws Exception;
}

下面是CommandLineRunner的例子:

  1. SpiderBatchApplication.java

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    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;
    @EnableAutoConfiguration
    @SpringBootApplication
    public class SpiderBatchApplication {
    private static final Logger logger = LoggerFactory.getLogger(SpiderBatchApplication.class);
    /**
    *
    * @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)));
    }
    }
  2. Task1.java

    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    @Component
    @Order(2)
    public class Task1 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
    System.out.println("task11111111111");
    }
    }
  3. Task2.java

    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    /**
    * Created with IntelliJ IDEA.
    * User: leo
    * Date: 2017/11/10
    * Time: 10:15
    * To change this template use File | Settings | File Templates.
    */
    @Component
    @Order(1)
    public class Task2 implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
    System.out.println("task2222222222");
    }
    }