diff --git a/67.spring-batch-start/pom.xml b/67.spring-batch-start/pom.xml new file mode 100644 index 0000000..918aab4 --- /dev/null +++ b/67.spring-batch-start/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.5.RELEASE + + + cc.mrbird + spring-batch-start + 0.0.1-SNAPSHOT + spring-batch-start + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-batch + + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-starter-jdbc + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/SpringBatchStartApplication.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/SpringBatchStartApplication.java new file mode 100644 index 0000000..86b7dea --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/SpringBatchStartApplication.java @@ -0,0 +1,15 @@ +package cc.mrbird.batch; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableBatchProcessing +public class SpringBatchStartApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBatchStartApplication.class, args); + } + +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/decider/MyDecider.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/decider/MyDecider.java new file mode 100644 index 0000000..b90bb55 --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/decider/MyDecider.java @@ -0,0 +1,28 @@ +package cc.mrbird.batch.decider; + +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.job.flow.FlowExecutionStatus; +import org.springframework.batch.core.job.flow.JobExecutionDecider; +import org.springframework.stereotype.Component; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +/** + * @author MrBird + */ +@Component +public class MyDecider implements JobExecutionDecider { + @Override + public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { + LocalDate now = LocalDate.now(); + DayOfWeek dayOfWeek = now.getDayOfWeek(); + + if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) { + return new FlowExecutionStatus("weekend"); + } else { + return new FlowExecutionStatus("workingDay"); + } + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/DeciderJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/DeciderJobDemo.java new file mode 100644 index 0000000..27150c3 --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/DeciderJobDemo.java @@ -0,0 +1,69 @@ +package cc.mrbird.batch.job; + +import cc.mrbird.batch.decider.MyDecider; +import org.springframework.batch.core.Job; +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.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class DeciderJobDemo { + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private MyDecider myDecider; + + @Bean + public Job deciderJob() { + return jobBuilderFactory.get("deciderJob") + .start(step1()) + .next(myDecider) + .from(myDecider).on("weekend").to(step2()) + .from(myDecider).on("workingDay").to(step3()) + .from(step3()).on("*").to(step4()) + .end() + .build(); + } + + private Step step1() { + return stepBuilderFactory.get("step1") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤一操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤二操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step3() { + return stepBuilderFactory.get("step3") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤三操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + + private Step step4() { + return stepBuilderFactory.get("step4") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤四操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FirstJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FirstJobDemo.java new file mode 100644 index 0000000..051984e --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FirstJobDemo.java @@ -0,0 +1,37 @@ +package cc.mrbird.batch.job; + +import org.springframework.batch.core.Job; +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.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class FirstJobDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Job firstJob() { + return jobBuilderFactory.get("firstJob") + .start(step()) + .build(); + } + + private Step step() { + return stepBuilderFactory.get("step") + .tasklet((contribution, chunkContext) -> { + System.out.println("执行步骤...."); + return RepeatStatus.FINISHED; + }).build(); + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FlowJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FlowJobDemo.java new file mode 100644 index 0000000..09d5fc9 --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/FlowJobDemo.java @@ -0,0 +1,66 @@ +package cc.mrbird.batch.job; + +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.Job; +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.job.builder.FlowBuilder; +import org.springframework.batch.core.job.flow.Flow; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class FlowJobDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Job flowJob() { + return jobBuilderFactory.get("flowJob") + .start(flow()) + .next(step3()) + .end() + .build(); + } + + private Step step1() { + return stepBuilderFactory.get("step1") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤一操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤二操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step3() { + return stepBuilderFactory.get("step3") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤三操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + // 创建一个flow对象,包含若干个step + private Flow flow() { + return new FlowBuilder("flow") + .start(step1()) + .next(step2()) + .build(); + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/MultiStepJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/MultiStepJobDemo.java new file mode 100644 index 0000000..7e9b3aa --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/MultiStepJobDemo.java @@ -0,0 +1,63 @@ +package cc.mrbird.batch.job; + +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.Job; +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.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class MultiStepJobDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Job multiStepJob() { + // return jobBuilderFactory.get("multiStepJob") + // .start(step1()) + // .next(step2()) + // .next(step3()) + // .build(); + return jobBuilderFactory.get("multiStepJob2") + .start(step1()) + .on(ExitStatus.COMPLETED.getExitCode()).to(step2()) + .from(step2()) + .on(ExitStatus.COMPLETED.getExitCode()).to(step3()) + .from(step3()).end() + .build(); + } + + private Step step1() { + return stepBuilderFactory.get("step1") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤一操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤二操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step3() { + return stepBuilderFactory.get("step3") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤三操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/NestedJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/NestedJobDemo.java new file mode 100644 index 0000000..e8edfc9 --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/NestedJobDemo.java @@ -0,0 +1,87 @@ +package cc.mrbird.batch.job; + +import org.springframework.batch.core.Job; +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.JobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.JobStepBuilder; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; +import org.springframework.transaction.PlatformTransactionManager; + +/** + * @author MrBird + */ +@Component +public class NestedJobDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private JobLauncher jobLauncher; + @Autowired + private JobRepository jobRepository; + @Autowired + private PlatformTransactionManager platformTransactionManager; + + // 父任务 + @Bean + public Job parentJob() { + return jobBuilderFactory.get("parentJob") + .start(childJobOneStep()) + .next(childJobTwoStep()) + .build(); + } + + + // 将任务转换为特殊的步骤 + private Step childJobOneStep() { + return new JobStepBuilder(new StepBuilder("childJobOneStep")) + .job(childJobOne()) + .launcher(jobLauncher) + .repository(jobRepository) + .transactionManager(platformTransactionManager) + .build(); + } + + // 将任务转换为特殊的步骤 + private Step childJobTwoStep() { + return new JobStepBuilder(new StepBuilder("childJobTwoStep")) + .job(childJobTwo()) + .launcher(jobLauncher) + .repository(jobRepository) + .transactionManager(platformTransactionManager) + .build(); + } + + // 子任务一 + private Job childJobOne() { + return jobBuilderFactory.get("childJobOne") + .start( + stepBuilderFactory.get("childJobOneStep") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("子任务一执行步骤。。。"); + return RepeatStatus.FINISHED; + }).build() + ).build(); + } + + // 子任务二 + private Job childJobTwo() { + return jobBuilderFactory.get("childJobTwo") + .start( + stepBuilderFactory.get("childJobTwoStep") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("子任务二执行步骤。。。"); + return RepeatStatus.FINISHED; + }).build() + ).build(); + } +} diff --git a/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/SplitJobDemo.java b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/SplitJobDemo.java new file mode 100644 index 0000000..604fe9d --- /dev/null +++ b/67.spring-batch-start/src/main/java/cc/mrbird/batch/job/SplitJobDemo.java @@ -0,0 +1,72 @@ +package cc.mrbird.batch.job; + +import org.springframework.batch.core.Job; +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.job.builder.FlowBuilder; +import org.springframework.batch.core.job.flow.Flow; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class SplitJobDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Job splitJob() { + return jobBuilderFactory.get("splitJob") + .start(flow1()) + .split(new SimpleAsyncTaskExecutor()).add(flow2()) + .end() + .build(); + + } + + private Step step1() { + return stepBuilderFactory.get("step1") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤一操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤二操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Step step3() { + return stepBuilderFactory.get("step3") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("执行步骤三操作。。。"); + return RepeatStatus.FINISHED; + }).build(); + } + + private Flow flow1() { + return new FlowBuilder("flow1") + .start(step1()) + .next(step2()) + .build(); + } + + private Flow flow2() { + return new FlowBuilder("flow2") + .start(step3()) + .build(); + } +} diff --git a/67.spring-batch-start/src/main/resources/application.yml b/67.spring-batch-start/src/main/resources/application.yml new file mode 100644 index 0000000..4c962f7 --- /dev/null +++ b/67.spring-batch-start/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/springbatch + username: root + password: 123456 \ No newline at end of file