springbatch-JobOperator使用

springbatch-JobOperator使用

起男 254 2023-06-01

springbatch-JobOperator使用

JobOperator本质上是对JobLauncher的封装

关闭任务自动执行

spring:
  batch:
    job:
      enabled: false

准备任务

@Configuration
public class JobOperatorDemo implements StepExecutionListener {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    private JobParameters jobParameters;

    @Bean
    public Job jobOperatorDemoJob(){
        return jobBuilderFactory
                .get("jobOperatorDemoJob")
                .start(jobLauncherDemoStep())
                .build();
    }
    @Bean
    public Step jobOperatorDemoStep(){
        return stepBuilderFactory
                .get("jobOperatorDemoStep")
                .listener(this)
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        String msg = jobParameters.getString("msg");
                        System.out.println("执行任务,参数:"+msg);
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

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

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

配置bean

默认不会为我们自动创建,这点和JobLauncher不同

	@Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private JobRepository jobRepository;
    @Autowired
    private JobExplorer jobExplorer;
    @Autowired
    private JobRegistry jobRegistry;
    @Autowired
    private ApplicationContext context;
    @Bean
    public JobOperator jobOperator(){
        SimpleJobOperator operator = new SimpleJobOperator();
        operator.setJobLauncher(jobLauncher);
        //转换器
        operator.setJobParametersConverter(new DefaultJobParametersConverter());
        //持久化
        operator.setJobRepository(jobRepository);
        //获取和任务相关的信息
        operator.setJobExplorer(jobExplorer);
        //注册
        operator.setJobRegistry(jobRegistry);
        return operator;
    }
    //把字符串和具体的任务对象关联
    @SneakyThrows
    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(){
        JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
        postProcessor.setJobRegistry(jobRegistry);
        postProcessor.setBeanFactory(context.getAutowireCapableBeanFactory());
        postProcessor.afterPropertiesSet();
        return postProcessor;
    }

执行任务

@RestController
@RequestMapping("job")
public class JobController {
    @Autowired
    private JobOperator jobOperator;

    @SneakyThrows
    @GetMapping("run/{msg}")
    public String run(@PathVariable String msg){
        //启动任务并传入参数(参数需要是 key=value 的格式)
        jobOperator.start("jobOperatorDemoJob","msg="+msg);
        return "ok...";
    }
}