Spring Boot下Spring Batch入门实例

一、About

Spring Batch是什么能干什么,网上一搜就有,但是就是没有入门实例,能找到的例子也都是2.0的,看文档都是英文无从下手~~~,使用当前最新的版本整合网络上找到的例子。

关于基础不熟悉的,推荐读一下Spring Batch 批处理框架这本书,虽然讲的是2.0但基本概念没变。

1.1 How Spring Batch works?

Spring Boot下Spring Batch入门实例_第1张图片
image

一个Job有1个或多个Step组成,Step有读、处理、写三部分操作组成;通过JobLauncher启动Job,启动时从JobRepository获取Job Execution;当前运行的Job及Step的结果及状态保存在JobRepository中。

二、Begin

下面举个小例子,就是这篇文章中的例子,下面的这个例子版本是最新的:

  • spring-boot:2.0.1.RELEASE
  • spring-batch-4.0.1.RELEASE(Spring-Boot 2.0.1就是依赖的此版本)

下面这个例子实现的是:从变量中读取3个字符串全转化大写并输出到控制台,加了一个监听,当任务完成时输出一个字符串到控制台,通过web端来调用。

下面是项目的目录结构:

Spring Boot下Spring Batch入门实例_第2张图片
image

2.1 pom.xml



    4.0.0

    com.javainuse
    springboot-batch
    0.0.1
    jar

    SpringBatch
    Spring Batch-Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.h2database
            h2
        

        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-batch
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2.2 Item Reader

读取:从数组中读取3个字符串

package com.javainuse.step;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

public class Reader implements ItemReader {

    private String[] messages = { "javainuse.com", 
            "Welcome to Spring Batch Example",
            "We use H2 Database for this example" };

    private int count = 0;

    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        if (count < messages.length) {
            return messages[count++];
        } else {
            count = 0;
        }
        return null;
    }
    
}

2.3 Item Processor

处理:将字符串转为大写

package com.javainuse.step;

import org.springframework.batch.item.ItemProcessor;

public class Processor implements ItemProcessor {
    
    @Override
    public String process(String data) throws Exception {
        return data.toUpperCase();
    }
    
}

2.4 Item Writer

输出:把转为大写的字符串输出到控制台

package com.javainuse.step;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

public class Writer implements ItemWriter {
    
    @Override
    public void write(List messages) throws Exception {
        for (String msg : messages) {
            System.out.println("Writing the data " + msg);
        }
    }
    
}

2.5 Listener

监听:任务成功完成后往控制台输出一行字符串

package com.javainuse.listener;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;

public class JobCompletionListener extends JobExecutionListenerSupport {
    
    @Override
    public void afterJob(JobExecution jobExecution) {
        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            System.out.println("BATCH JOB COMPLETED SUCCESSFULLY");
        }
    }
    
}

2.6 Config

Spring Boot配置:

package com.javainuse.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javainuse.listener.JobCompletionListener;
import com.javainuse.step.Processor;
import com.javainuse.step.Reader;
import com.javainuse.step.Writer;

@Configuration
public class BatchConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job processJob() {
        return jobBuilderFactory.get("processJob")
                .incrementer(new RunIdIncrementer()).listener(listener())
                .flow(orderStep1()).end().build();
    }

    @Bean
    public Step orderStep1() {
        return stepBuilderFactory.get("orderStep1"). chunk(1)
                .reader(new Reader()).processor(new Processor())
                .writer(new Writer()).build();
    }

    @Bean
    public JobExecutionListener listener() {
        return new JobCompletionListener();
    }
    
}

2.7 Controller

控制器:配置web路由,访问/invokejob来调用任务

package com.javainuse.controller;
 
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class JobInvokerController {
 
    @Autowired
    JobLauncher jobLauncher;
 
    @Autowired
    Job processJob;
 
    @RequestMapping("/invokejob")
    public String handle() throws Exception {
 
            JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                    .toJobParameters();
            jobLauncher.run(processJob, jobParameters);
 
        return "Batch job has been invoked";
    }
}

2.8 application.properties

配置:Spring Batch在加载的时候job默认都会执行,把spring.batch.job.enabled置为false,即把job设置成不可用,应用便会根据jobLauncher.run来执行。下面2行是数据库的配置,不配置也可以,使用的嵌入式数据库h2

spring.batch.job.enabled=false
spring.datasource.url=jdbc:h2:file:./DB
spring.jpa.properties.hibernate.hbm2ddl.auto=update

2.9 Application

Spring Boot入口类:加注解@EnableBatchProcessing

package com.javainuse;

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

@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchApplication {

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

三、Run

启动这个Spring Boot项目,访问下面这个路径,会有如下输出

http://localhost:8080/invokejob

Spring Boot下Spring Batch入门实例_第3张图片
image

然后控制台会有如下输出:

Spring Boot下Spring Batch入门实例_第4张图片
image

我们可以使用H2-console来查看H2数据库,访问如下地址 http://localhost:8080/h2-console , 选择如下数据库,输入JDBC URL:jdbc:h2:file:./DB,这是上文配置的,不用输入密码,直接点击Connect就可以了。

[图片上传失败...(image-573293-1523402615194)]

连上后显示如下:

Spring Boot下Spring Batch入门实例_第5张图片
image

左侧那些表就是Spring Batch自动创建的,至于每个表都是什么意思,可参考下面的那本书,这些建表的脚本存在于哪呢?可在spring-batch-core-xxx.jar包下的org.springframework.batch.core包下找到

Spring Boot下Spring Batch入门实例_第6张图片
image
Spring Boot下Spring Batch入门实例_第7张图片
image

查看batch-h2.properties可以看到相关的默认配置:

Spring Boot下Spring Batch入门实例_第8张图片
image

四、Reference

  • Spring Boot Batch Job Simple Example
  • Implement Spring Batch using Spring Boot-Hello World Example
  • Spring Batch 批处理框架
  • springboot-batch 实例源码

你可能感兴趣的:(Spring Boot下Spring Batch入门实例)