本文主要是介绍springbatch自学之路-06(split的创建与使用-使用多线程执行任务),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.配置类
package com.springbatch._04new_split;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.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
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 org.springframework.core.task.SimpleAsyncTaskExecutor;/*** 任务配置器** @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的对象@Autowiredprivate JobBuilderFactory jobBuilderFactory;//创建 创建step的对象@Autowiredprivate StepBuilderFactory stepBuilderFactory;/*** @param* @throws* @Title: <p>newJob</p>* @Description: <p>可以多线程执行</p>* @return: org.springframework.batch.core.Job* @author: * @Date: 2020/5/28 21:28* @version: 1.0* @Copyright: . All rights reserved.*/@Beanpublic Job newJob() {return jobBuilderFactory.get("newJob").start(flow1()).split(new SimpleAsyncTaskExecutor()).add(flow2()).end().build();}@Beanpublic Step step3() {return stepBuilderFactory.get("step003").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println(Thread.currentThread() + " step003");return RepeatStatus.FINISHED;}}).build();}@Beanpublic Step step4() {return stepBuilderFactory.get("step004").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println(Thread.currentThread() + " step004");return RepeatStatus.FINISHED;}}).build();}@Beanpublic Step step2() {return stepBuilderFactory.get("step002").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println(Thread.currentThread() + " step002");return RepeatStatus.FINISHED;}}).build();}@Beanpublic Step step1() {return stepBuilderFactory.get("step001").tasklet(new Tasklet() {@Overridepublic RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {System.out.println(Thread.currentThread() + " step001");return RepeatStatus.FINISHED;}}).build();}@Beanpublic Flow flow1() {return new FlowBuilder<Flow>("flow001").start(step1()).next(step2()).next(step3()).build();}@Beanpublic Flow flow2() {return new FlowBuilder<Flow>("flow002").start(step4()).build();}}
2.启动类
package com.springbatch._04new_split;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);}}
3.执行结果
这篇关于springbatch自学之路-06(split的创建与使用-使用多线程执行任务)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!