File tree 6 files changed +186
-0
lines changed 6 files changed +186
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+ <modelVersion >4.0.0</modelVersion >
5
+ <parent >
6
+ <groupId >org.springframework.boot</groupId >
7
+ <artifactId >spring-boot-starter-parent</artifactId >
8
+ <version >2.2.5.RELEASE</version >
9
+ <relativePath /> <!-- lookup parent from repository -->
10
+ </parent >
11
+ <groupId >cc.mrbird</groupId >
12
+ <artifactId >spring-batch-launcher</artifactId >
13
+ <version >0.0.1-SNAPSHOT</version >
14
+ <name >spring-batch-launcher</name >
15
+ <description >Demo project for Spring Boot</description >
16
+
17
+ <properties >
18
+ <java .version>1.8</java .version>
19
+ </properties >
20
+
21
+ <dependencies >
22
+ <dependency >
23
+ <groupId >org.springframework.boot</groupId >
24
+ <artifactId >spring-boot-starter-batch</artifactId >
25
+ </dependency >
26
+ <dependency >
27
+ <groupId >org.springframework.boot</groupId >
28
+ <artifactId >spring-boot-starter-jdbc</artifactId >
29
+ </dependency >
30
+ <dependency >
31
+ <groupId >org.springframework.boot</groupId >
32
+ <artifactId >spring-boot-starter-web</artifactId >
33
+ </dependency >
34
+
35
+ <dependency >
36
+ <groupId >mysql</groupId >
37
+ <artifactId >mysql-connector-java</artifactId >
38
+ <scope >runtime</scope >
39
+ </dependency >
40
+ </dependencies >
41
+
42
+ <build >
43
+ <plugins >
44
+ <plugin >
45
+ <groupId >org.springframework.boot</groupId >
46
+ <artifactId >spring-boot-maven-plugin</artifactId >
47
+ </plugin >
48
+ </plugins >
49
+ </build >
50
+
51
+ </project >
Original file line number Diff line number Diff line change
1
+ package cc .mrbird .batch ;
2
+
3
+ import org .springframework .batch .core .configuration .annotation .EnableBatchProcessing ;
4
+ import org .springframework .boot .SpringApplication ;
5
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6
+
7
+ @ SpringBootApplication
8
+ @ EnableBatchProcessing
9
+ public class SpringBatchLauncherApplication {
10
+
11
+ public static void main (String [] args ) {
12
+ SpringApplication .run (SpringBatchLauncherApplication .class , args );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package cc .mrbird .batch .configure ;
2
+
3
+ import org .springframework .batch .core .configuration .JobRegistry ;
4
+ import org .springframework .batch .core .configuration .support .JobRegistryBeanPostProcessor ;
5
+ import org .springframework .context .ApplicationContext ;
6
+ import org .springframework .context .annotation .Bean ;
7
+ import org .springframework .context .annotation .Configuration ;
8
+
9
+ /**
10
+ * @author MrBird
11
+ */
12
+ @ Configuration
13
+ public class JobConfigure {
14
+
15
+ /**
16
+ * 注册JobRegistryBeanPostProcessor bean
17
+ * 用于将任务名称和实际的任务关联起来
18
+ */
19
+ @ Bean
20
+ public JobRegistryBeanPostProcessor processor (JobRegistry jobRegistry , ApplicationContext applicationContext ) {
21
+ JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor ();
22
+ postProcessor .setJobRegistry (jobRegistry );
23
+ postProcessor .setBeanFactory (applicationContext .getAutowireCapableBeanFactory ());
24
+ return postProcessor ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package cc .mrbird .batch .controller ;
2
+
3
+ import org .springframework .batch .core .Job ;
4
+ import org .springframework .batch .core .JobParameters ;
5
+ import org .springframework .batch .core .JobParametersBuilder ;
6
+ import org .springframework .batch .core .launch .JobLauncher ;
7
+ import org .springframework .batch .core .launch .JobOperator ;
8
+ import org .springframework .beans .factory .annotation .Autowired ;
9
+ import org .springframework .web .bind .annotation .GetMapping ;
10
+ import org .springframework .web .bind .annotation .PathVariable ;
11
+ import org .springframework .web .bind .annotation .RequestMapping ;
12
+ import org .springframework .web .bind .annotation .RestController ;
13
+
14
+ /**
15
+ * @author MrBird
16
+ */
17
+ @ RestController
18
+ @ RequestMapping ("job" )
19
+ public class JobController {
20
+
21
+ @ Autowired
22
+ private Job job ;
23
+ @ Autowired
24
+ private JobLauncher jobLauncher ;
25
+ @ Autowired
26
+ private JobOperator jobOperator ;
27
+
28
+ @ GetMapping ("launcher/{message}" )
29
+ public String launcher (@ PathVariable String message ) throws Exception {
30
+ JobParameters parameters = new JobParametersBuilder ()
31
+ .addString ("message" , message )
32
+ .toJobParameters ();
33
+ // 将参数传递给任务
34
+ jobLauncher .run (job , parameters );
35
+ return "success" ;
36
+ }
37
+
38
+ @ GetMapping ("operator/{message}" )
39
+ public String operator (@ PathVariable String message ) throws Exception {
40
+ // 传递任务名称,参数使用 kv方式
41
+ jobOperator .start ("job" , "message=" + message );
42
+ return "success" ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ package cc .mrbird .batch .job ;
2
+
3
+ import org .springframework .batch .core .*;
4
+ import org .springframework .batch .core .configuration .annotation .JobBuilderFactory ;
5
+ import org .springframework .batch .core .configuration .annotation .StepBuilderFactory ;
6
+ import org .springframework .batch .repeat .RepeatStatus ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .context .annotation .Bean ;
9
+ import org .springframework .stereotype .Component ;
10
+
11
+ import java .util .Map ;
12
+
13
+ /**
14
+ * @author MrBird
15
+ */
16
+ @ Component
17
+ public class MyJob {
18
+
19
+ @ Autowired
20
+ private JobBuilderFactory jobBuilderFactory ;
21
+ @ Autowired
22
+ private StepBuilderFactory stepBuilderFactory ;
23
+
24
+ @ Bean
25
+ public Job job (){
26
+ return jobBuilderFactory .get ("job" )
27
+ .start (step ())
28
+ .build ();
29
+ }
30
+
31
+ private Step step (){
32
+ return stepBuilderFactory .get ("step" )
33
+ .tasklet ((stepContribution , chunkContext ) -> {
34
+ StepExecution stepExecution = chunkContext .getStepContext ().getStepExecution ();
35
+ Map <String , JobParameter > parameters = stepExecution .getJobParameters ().getParameters ();
36
+ System .out .println (parameters .get ("message" ).getValue ());
37
+ return RepeatStatus .FINISHED ;
38
+ })
39
+ .listener (this )
40
+ .build ();
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ spring :
2
+ datasource :
3
+ driver-class-name : com.mysql.cj.jdbc.Driver
4
+ url : jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
5
+ username : root
6
+ password : 123456
7
+ batch :
8
+ job :
9
+ enabled : false
You can’t perform that action at this time.
0 commit comments