forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7aa99b
commit 43ddb3e
Showing
6 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
14 changes: 14 additions & 0 deletions
14
73.spring-batch-launcher/src/main/java/cc/mrbird/batch/SpringBatchLauncherApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
73.spring-batch-launcher/src/main/java/cc/mrbird/batch/configure/JobConfigure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
73.spring-batch-launcher/src/main/java/cc/mrbird/batch/controller/JobController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
73.spring-batch-launcher/src/main/java/cc/mrbird/batch/job/MyJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |