diff --git a/73.spring-batch-launcher/pom.xml b/73.spring-batch-launcher/pom.xml new file mode 100644 index 0000000..a5b8348 --- /dev/null +++ b/73.spring-batch-launcher/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.5.RELEASE + + + cc.mrbird + spring-batch-launcher + 0.0.1-SNAPSHOT + spring-batch-launcher + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-batch + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter-web + + + + mysql + mysql-connector-java + runtime + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/SpringBatchLauncherApplication.java b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/SpringBatchLauncherApplication.java new file mode 100644 index 0000000..f37f9bb --- /dev/null +++ b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/SpringBatchLauncherApplication.java @@ -0,0 +1,14 @@ +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 SpringBatchLauncherApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBatchLauncherApplication.class, args); + } +} diff --git a/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/configure/JobConfigure.java b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/configure/JobConfigure.java new file mode 100644 index 0000000..f61b1fe --- /dev/null +++ b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/configure/JobConfigure.java @@ -0,0 +1,26 @@ +package cc.mrbird.batch.configure; + +import org.springframework.batch.core.configuration.JobRegistry; +import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author MrBird + */ +@Configuration +public class JobConfigure { + + /** + * 注册JobRegistryBeanPostProcessor bean + * 用于将任务名称和实际的任务关联起来 + */ + @Bean + public JobRegistryBeanPostProcessor processor(JobRegistry jobRegistry, ApplicationContext applicationContext) { + JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor(); + postProcessor.setJobRegistry(jobRegistry); + postProcessor.setBeanFactory(applicationContext.getAutowireCapableBeanFactory()); + return postProcessor; + } +} diff --git a/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/controller/JobController.java b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/controller/JobController.java new file mode 100644 index 0000000..d8c1ecd --- /dev/null +++ b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/controller/JobController.java @@ -0,0 +1,44 @@ +package cc.mrbird.batch.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.batch.core.launch.JobOperator; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author MrBird + */ +@RestController +@RequestMapping("job") +public class JobController { + + @Autowired + private Job job; + @Autowired + private JobLauncher jobLauncher; + @Autowired + private JobOperator jobOperator; + + @GetMapping("launcher/{message}") + public String launcher(@PathVariable String message) throws Exception { + JobParameters parameters = new JobParametersBuilder() + .addString("message", message) + .toJobParameters(); + // 将参数传递给任务 + jobLauncher.run(job, parameters); + return "success"; + } + + @GetMapping("operator/{message}") + public String operator(@PathVariable String message) throws Exception { + // 传递任务名称,参数使用 kv方式 + jobOperator.start("job", "message=" + message); + return "success"; + } +} diff --git a/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/job/MyJob.java b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/job/MyJob.java new file mode 100644 index 0000000..248c435 --- /dev/null +++ b/73.spring-batch-launcher/src/main/java/cc/mrbird/batch/job/MyJob.java @@ -0,0 +1,42 @@ +package cc.mrbird.batch.job; + +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.repeat.RepeatStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author MrBird + */ +@Component +public class MyJob{ + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + + @Bean + public Job job(){ + return jobBuilderFactory.get("job") + .start(step()) + .build(); + } + + private Step step(){ + return stepBuilderFactory.get("step") + .tasklet((stepContribution, chunkContext) -> { + StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); + Map parameters = stepExecution.getJobParameters().getParameters(); + System.out.println(parameters.get("message").getValue()); + return RepeatStatus.FINISHED; + }) + .listener(this) + .build(); + } +} diff --git a/73.spring-batch-launcher/src/main/resources/application.yml b/73.spring-batch-launcher/src/main/resources/application.yml new file mode 100644 index 0000000..c84f4a8 --- /dev/null +++ b/73.spring-batch-launcher/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8 + username: root + password: 123456 + batch: + job: + enabled: false \ No newline at end of file