Spring Batch任务调度
This commit is contained in:
parent
b7aa99b906
commit
43ddb3eb69
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>cc.mrbird</groupId>
|
||||
<artifactId>spring-batch-launcher</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-batch-launcher</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-batch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, JobParameter> parameters = stepExecution.getJobParameters().getParameters();
|
||||
System.out.println(parameters.get("message").getValue());
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.listener(this)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue