📜  Spring Batch-环境(1)

📅  最后修改于: 2023-12-03 15:35:02.886000             🧑  作者: Mango

Spring Batch 环境

简介

Spring Batch 是一个轻量级的开源批处理框架,它可以在企业级应用程序中快速处理固定数量的大量数据。Spring Batch 可以将一个大任务切分成多个小任务,以便更好地管理数据处理。

环境搭建
1. Maven 配置

pom.xml 文件中,添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-infrastructure</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
</dependencies>
2. 数据源配置

application.properties 文件中,添加以下配置:

# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3. Batch 配置

在 Java 配置类中,添加以下配置:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    /**
     * 定义一个 Job
     */
    @Bean
    public Job demoJob() {
        return jobBuilderFactory.get("demoJob")
                .start(step1())
                .next(step2())
                .build();
    }

    /**
     * 定义第一个 Step
     */
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Step 1");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    /**
     * 定义第二个 Step
     */
    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Step 2");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}
使用 Spring Batch
1. 编写任务

编写一个继承 org.springframework.batch.core.Job 接口的任务类,如下:

@Component
public class DemoJob implements Job {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    /**
     * 定义一个 Job
     */
    @Override
    public JobExecution execute(JobExecutionListener listener) throws Exception {
        JobExecution jobExecution = jobBuilderFactory.get("demoJob")
                .start(step1())
                .next(step2())
                .build()
                .execute(listener);
        return jobExecution;
    }

    /**
     * 定义第一个 Step
     */
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Step 1");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    /**
     * 定义第二个 Step
     */
    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet((stepContribution, chunkContext) -> {
                    System.out.println("Step 2");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}
2. 运行任务

在需要运行任务的类中注入任务类,并调用 Job.execute() 方法即可运行任务,如下:

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private DemoJob demoJob;

    @Override
    public void run(String... args) throws Exception {
        JobExecution jobExecution = demoJob.execute(new JobExecutionListenerSupport() {
            @Override
            public void beforeJob(JobExecution jobExecution) {
                System.out.println("Job started");
            }

            @Override
            public void afterJob(JobExecution jobExecution) {
                System.out.println("Job ended");
            }
        });
    }
}
参考链接