spring batch

spring batch批量

1.简单任务

1.修改配置

修改application.properties文件中的配置

# 程序启动就执行job
#spring.batch.job.enabled=false

2.代码实现

1.创建任务


import cn.hutool.core.date.DateUtil;
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;

import java.util.Date;

/**
 * Description:
 * User: liu
 * Date: 2021-10-17
 * Time: 17:06
 */

@Configuration
public class SimpleJobDemo {

    //引入job工厂和step工厂,job和step都是由2个工厂创造出来的
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * job ,job调用step完成任务
     * @return
     */
    @Bean
    public Job simpleJobDemoJob() {
        //名字相同的job,成功后就不执行了。
        String jobName = "jobLauncherDemoJob_"+ DateUtil.format(new Date(), "yyyy_MM_DD_HH_mm_ss");

        return jobBuilderFactory.get(jobName)
                .start(simpleJobDemoStep1())
                .next(simpleJobDemoStep2())         //多个step,可以继续使用next
                .build();
    }

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

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

2.springboot启动类

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

@SpringBootApplication
@EnableBatchProcessing
public class OpPlatformSpringbootbatchApplication {

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

}

3.运行结果

可以看到step1和step2都运行了,并且打印运行相关信息。
在这里插入图片描述

2.简单传递参数的任务

1.修改配置

修改application.properties文件中的配置

spring.batch.job.enabled=false

2.代码实现

1.创建任务


import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
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;

import java.util.Date;
import java.util.Map;

/**
 * Description:
 * User: liu
 * Date: 2021-10-17
 * Time: 14:39
 */

@Slf4j
@Configuration
public class JobLauncherDemo implements StepExecutionListener {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    private Map<String, JobParameter> parameter;

    @Bean
    public Job jobLauncherDemoJob(){
        String jobName = "jobLauncherDemoJob_"+ DateUtil.format(new Date(), "yyyy_MM_DD_HH_mm_ss");
        return jobBuilderFactory.get(jobName)
                .start(jobLauncherDemoStep())
                .build();
    }

    @Bean
    public Step jobLauncherDemoStep() {
        return stepBuilderFactory.get("jobLauncherDemoStep")
                .listener(this)     //注意这里需要加入侦听
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        log.info(parameter.get("msg").getValue().toString());
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        parameter = stepExecution.getJobParameters().getParameters();
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}

2.创建controller

这个controller用来接收参数,并且把参数传递给step中


import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * User: liu
 * Date: 2021-10-17
 * Time: 15:11
 */

@RestController
public class jobLauncherDemoController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job jobLauncherDemoJob;

    @RequestMapping("/job/{msg}")
    public String JobRunController(@PathVariable String msg) throws JobParametersInvalidException,
            JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        //把接收到的参数值传个任务
        JobParameters jobParametes = new JobParametersBuilder()
                .addString("msg",msg)
                .toJobParameters();

        jobLauncher.run(jobLauncherDemoJob,jobParametes);

        return "success";

    }
}

3.创建springboot启动类

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

@SpringBootApplication
@EnableBatchProcessing
public class OpPlatformSpringbootbatchApplication {

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

}

4.请求地址

输入请求地址;

http://localhost:8080/job/我是测试的

5.运行结果

可以在日志中看到,通过url中传递的参数被成功打印。
在这里插入图片描述

3.读取文件写数据库

1.新建Job和Step类


import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description:
 * User: little
 * Date: 2021-12-13
 * Time: 00:13
 */

@Configuration
@EnableBatchProcessing
public class ItemWriteDbDemo {


    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Autowired
    @Qualifier("itemWriteDbDemoWrite")
    ItemWriter<? super JdqqgUser> itemWriteDbDemoWrite;


    @Autowired
    @Qualifier("itemWriteDbDemoRead")
    ItemReader<JdqqgUser> itemWriteDbDemoRead;

    @Bean
    public Job itemWriteDbDemoJob(){
        String jobName = "itemWriteDbDemoJob"+System.currentTimeMillis();
        return jobBuilderFactory.get(jobName)
                .start(itemWriteDbDemoStep())
                .build();

    }

    @Bean
    public Step itemWriteDbDemoStep() {

        return stepBuilderFactory.get("itemWriteDbDemoStep")
                .<JdqqgUser,JdqqgUser>chunk(10)
                .writer(itemWriteDbDemoWrite)
                .reader(itemWriteDbDemoRead)
                .build();
    }
}

2.reader实现类


import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.validation.BindException;

import java.io.File;

/**
 * Description:
 * User: liuhongtao
 * Date: 2021-12-13
 * Time: 00:18
 */

@Configuration
public class ItemWriteDbDemoReadConfig {

    @Bean
    public FlatFileItemReader<JdqqgUser> itemWriteDbDemoRead(){
        FlatFileItemReader<JdqqgUser> reader = new FlatFileItemReader<>();
//        reader.setResource(new ClassPathResource("resources/jdqqg_user.txt"));
//        File file = new File(filePath);

        reader.setResource(new FileSystemResource("/abcd/jdqqg_user.txt"));

        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();

        tokenizer.setNames(new String[]{"id","vxNickname","phone","lastNum"});

        DefaultLineMapper<JdqqgUser> mapper = new DefaultLineMapper();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<JdqqgUser>() {
            @Override
            public JdqqgUser mapFieldSet(FieldSet fieldSet) throws BindException {
                JdqqgUser JdqqgUser = new JdqqgUser();
//                JdqqgUser.setId(fieldSet.readLong("id"));
                JdqqgUser.setVxNickname(fieldSet.readString("vxNickname"));
                JdqqgUser.setPhone(fieldSet.readString("phone"));
                JdqqgUser.setLastNum(fieldSet.readString("lastNum"));

                return JdqqgUser;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        return reader;
    }
}

3.writer实现类


import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Description:
 * User: liu
 * Date: 2021-12-13
 * Time: 00:17
 */

@Configuration
public class ItemWriteDbDemoWriteConfig {

    @Autowired
    private DataSource datasource;

    @Bean
    public JdbcBatchItemWriter<JdqqgUser> itemWriteDbDemoWrite(){
        JdbcBatchItemWriter<JdqqgUser> writer = new JdbcBatchItemWriter<JdqqgUser>();
        writer.setDataSource(datasource);
        writer.setSql("insert into jdqqg_user(id,vx_nickname,phone,last_num) values (:id,:vxNickname,:phone,:lastNum) ");

        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());

        return writer;
    }
}

4.实体类


import java.util.Date;

public class JdqqgUser {

  private long id;
  private String vxName;
  private String vxNickname;
  private String phone;
  private String lastNum;
  private String des;
  private Date createTime;
  private Date updateTime;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }


  public String getVxName() {
    return vxName;
  }

  public void setVxName(String vxName) {
    this.vxName = vxName;
  }

  public String getVxNickname() {
    return vxNickname;
  }

  public void setVxNickname(String vxNickname) {
    this.vxNickname = vxNickname;
  }


  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getLastNum() {
    return lastNum;
  }

  public void setLastNum(String lastNum) {
    this.lastNum = lastNum;
  }

  public String getDes() {
    return des;
  }

  public void setDes(String des) {
    this.des = des;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }
}

5.数据库sql

create table jdqqg_user
(
    id          int auto_increment,
    vx_name     varchar(200) null comment '微信号',
    vx_nickname varchar(200) null comment '微信昵称',
    phone       varchar(50)  null comment '手机号',
    last_num    varchar(50)  null comment '手机尾号',
    des         varchar(500) null comment '描述',
    create_time datetime     null,
    update_time datetime     null,
    constraint jdqqg_user_id_uindex
        unique (id)
)
    comment '京东区区购用户' collate = utf8mb4_unicode_ci;

alter table jdqqg_user
    add primary key (id);

6.数据文件

1,霸王龙,185-xxxx-xxxx,7845
2,霸王龙爸爸,1x4-xxxx-xxxx,8563
3,卓尼老师,156-xxxx-xxx,4493
4,小芳,13X-xxxx-xxxx,1480
5,芳草地,1xx-xxxx-xxxx,1028
7,* 小雪 *,102-xxxx-xxxx,158x
8,东北大汉,xxxx-xxxx-xxxx,abd
9,写文章,1xx-xxxx-xxxx,cccc
10,老张,1xx-xxxx-xxxx,1199
11,一直赢,xxx-xx21-4xx3,4444

7.修改配置

修改application.properties文件中的配置,让程序启动就运行job

#spring.batch.job.enabled=false

参考

https://www.bilibili.com/video/BV1UJ411v7VA?p=1&vd_source=6d74db55b4cae34a8e57e32c685aa65b

你可能感兴趣的:(程序代码,spring,batch,java)