springbatch自学之路-04(job的创建与使用)

目录

1.使用start。。。next。。。的方式

2.使用from。。。on。。。to。。。的方式


1.使用start。。。next。。。的方式

直接上代码:

配置类:

package com.springbatch._02new_job;

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.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 任务配置器
 *
 * @Package: com.springbatch._01helloworld
 * @ClassName: jobConfig
 * @author: zq
 * @since: 2020/5/22 21:00
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@Configuration
public class JobConfig {

    //创建 创建job的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //创建 创建step的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job newJob() {

        return jobBuilderFactory.get("newJob")
                .start(step1())
                .next(step2())
                .next(step3())
                .build();

    }


    @Bean
    public Step step3() {
        return stepBuilderFactory.get("step3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step3");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step2");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step step1() {

        return stepBuilderFactory.get("step1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step1");
                        return RepeatStatus.FINISHED;
                    }
                }).build();

    }

}

启动类

package com.springbatch._02new_job;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBatchDemo项目启动类
 *
 * @Package: PACKAGE_NAME
 * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
 * @author: zq
 * @since: 2020/5/22 20:31
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {

    public static void main(String[] args) {

        SpringApplication.run(SpringBatchConfig.class, args);

    }

}

2.使用from。。。on。。。to。。。的方式

配置类

package com.springbatch._02new_job;

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.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 任务配置器
 *
 * @Package: com.springbatch._01helloworld
 * @ClassName: jobConfig
 * @author: zq
 * @since: 2020/5/22 21:00
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@Configuration
public class JobConfig {

    //创建 创建job的对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //创建 创建step的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job newJob2() {

        return jobBuilderFactory.get("newJob2")
                .start(step1())
                .on("COMPLETED").to(step2())
                .from(step2()).on("COMPLETED").to(step3())
                .from(step3()).end()
                .build();

    }

    @Bean
    public Step step3() {
        return stepBuilderFactory.get("step3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step3");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step2");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step step1() {

        return stepBuilderFactory.get("step1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("step1");
                        return RepeatStatus.FINISHED;
                    }
                }).build();

    }

}

启动类

package com.springbatch._02new_job;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBatchDemo项目启动类
 *
 * @Package: PACKAGE_NAME
 * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
 * @author: zq
 * @since: 2020/5/22 20:31
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {

    public static void main(String[] args) {

        SpringApplication.run(SpringBatchConfig.class, args);

    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值