Skip to content

Commit 768c21b

Browse files
committed
springboot整合springbatch
0 parents  commit 768c21b

18 files changed

Lines changed: 819 additions & 0 deletions

pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.wangqiang</groupId>
7+
<artifactId>springbatch</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springbatch</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-batch</artifactId>
30+
<version>2.0.4.RELEASE</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.batch</groupId>
40+
<artifactId>spring-batch-test</artifactId>
41+
<version>4.0.1.RELEASE</version>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<!--<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
<version>1.4.197</version>
49+
<scope>runtime</scope>
50+
</dependency>-->
51+
<dependency>
52+
<groupId>mysql</groupId>
53+
<artifactId>mysql-connector-java</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter-jdbc</artifactId>
58+
</dependency>
59+
<!--读取xml的依赖-->
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-oxm</artifactId>
63+
<version>5.0.4.RELEASE</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.thoughtworks.xstream</groupId>
67+
<artifactId>xstream</artifactId>
68+
<version>1.4.10-java7</version>
69+
</dependency>
70+
</dependencies>
71+
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-maven-plugin</artifactId>
77+
</plugin>
78+
</plugins>
79+
</build>
80+
81+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.wangqiang.springbatch.config;
2+
3+
import org.springframework.batch.core.Job;
4+
import org.springframework.batch.core.Step;
5+
import org.springframework.batch.core.StepContribution;
6+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
7+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
8+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
9+
import org.springframework.batch.core.job.builder.FlowBuilder;
10+
import org.springframework.batch.core.job.flow.Flow;
11+
import org.springframework.batch.core.scope.context.ChunkContext;
12+
import org.springframework.batch.core.step.tasklet.Tasklet;
13+
import org.springframework.batch.repeat.RepeatStatus;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
17+
18+
@Configuration
19+
@EnableBatchProcessing
20+
public class FlowDemo {
21+
22+
@Autowired
23+
private JobBuilderFactory jobBuilderFactory;
24+
@Autowired
25+
private StepBuilderFactory stepBuilderFactory;
26+
27+
@Bean
28+
public Step flowDemoStep1(){
29+
return stepBuilderFactory.get("flowDemoStep1")
30+
.tasklet(new Tasklet() {
31+
@Override
32+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
33+
System.out.println("flowDemoStep1");
34+
return RepeatStatus.FINISHED;
35+
}
36+
}).build();
37+
}
38+
39+
@Bean
40+
public Step flowDemoStep2(){
41+
return stepBuilderFactory.get("flowDemoStep2")
42+
.tasklet(new Tasklet() {
43+
@Override
44+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
45+
System.out.println("flowDemoStep2");
46+
return RepeatStatus.FINISHED;
47+
}
48+
}).build();
49+
}
50+
51+
@Bean
52+
public Step flowDemoStep3(){
53+
return stepBuilderFactory.get("flowDemoStep3")
54+
.tasklet(new Tasklet() {
55+
@Override
56+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
57+
System.out.println("flowDemoStep3");
58+
return RepeatStatus.FINISHED;
59+
}
60+
}).build();
61+
}
62+
63+
//创建Flow对象,包含哪些Step
64+
@Bean
65+
public Flow flowDemoFlow(){
66+
return new FlowBuilder<Flow>("flowDemoFlow")
67+
.start(flowDemoStep1())
68+
.next(flowDemoStep2())
69+
.build();
70+
}
71+
72+
//创建Job对象,可以包含step或flow
73+
@Bean
74+
public Job flowDemoJob(){
75+
return jobBuilderFactory.get("flowDemoJob")
76+
.start(flowDemoFlow())
77+
.end()
78+
.build();
79+
}
80+
81+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.wangqiang.springbatch.config;
2+
3+
import org.springframework.batch.core.Job;
4+
import org.springframework.batch.core.Step;
5+
import org.springframework.batch.core.StepContribution;
6+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
7+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
8+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
9+
import org.springframework.batch.core.scope.context.ChunkContext;
10+
import org.springframework.batch.core.step.tasklet.Tasklet;
11+
import org.springframework.batch.repeat.RepeatStatus;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
16+
@Configuration
17+
@EnableBatchProcessing
18+
public class JobConfiguration {
19+
20+
//注入创建任务对象的对象
21+
@Autowired
22+
private JobBuilderFactory jobBuilderFactory;
23+
//任务的执行由Step决定
24+
//注入创建Step对象的对象
25+
@Autowired
26+
private StepBuilderFactory stepBuilderFactory;
27+
28+
@Bean
29+
public Job hellowJob(){
30+
return jobBuilderFactory.get("hellowJob")
31+
.start(step1())
32+
.build();
33+
}
34+
35+
@Bean
36+
public Step step1(){
37+
return stepBuilderFactory.get("step1")
38+
.tasklet(new Tasklet() {
39+
@Override
40+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
41+
System.out.println("hello");
42+
return RepeatStatus.FINISHED;
43+
}
44+
}).build();
45+
}
46+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.wangqiang.springbatch.config;
2+
3+
import org.springframework.batch.core.Job;
4+
import org.springframework.batch.core.Step;
5+
import org.springframework.batch.core.StepContribution;
6+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
7+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
8+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
9+
import org.springframework.batch.core.scope.context.ChunkContext;
10+
import org.springframework.batch.core.step.tasklet.Tasklet;
11+
import org.springframework.batch.repeat.RepeatStatus;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
16+
@Configuration
17+
@EnableBatchProcessing
18+
public class JobDemo {
19+
20+
@Autowired
21+
private JobBuilderFactory jobBuilderFactory;
22+
@Autowired
23+
private StepBuilderFactory stepBuilderFactory;
24+
25+
@Bean
26+
public Job jobDemoJob(){
27+
return jobBuilderFactory.get("jobDemoJob")
28+
// .start(step1())
29+
// .next(step2())
30+
// .next(step3())
31+
// .build();
32+
.start(step1())
33+
.on("COMPLETED").to(step2())
34+
.from(step2()).on("COMPLETED").to(step3())
35+
.from(step3()).end()
36+
.build();
37+
}
38+
39+
@Bean
40+
public Step step3() {
41+
return stepBuilderFactory.get("step3")
42+
.tasklet(new Tasklet() {
43+
@Override
44+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
45+
System.out.println("step3 start");
46+
//上一个任务正常结束才能进行下一个
47+
return RepeatStatus.FINISHED;
48+
}
49+
}).build();
50+
}
51+
52+
@Bean
53+
public Step step2() {
54+
return stepBuilderFactory.get("step2")
55+
.tasklet(new Tasklet() {
56+
@Override
57+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
58+
System.out.println("step2 start");
59+
//上一个任务正常结束才能进行下一个
60+
return RepeatStatus.FINISHED;
61+
}
62+
}).build();
63+
}
64+
65+
@Bean
66+
public Step step1() {
67+
return stepBuilderFactory.get("step1")
68+
.tasklet(new Tasklet() {
69+
@Override
70+
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
71+
System.out.println("step1 start");
72+
//上一个任务正常结束才能进行下一个
73+
return RepeatStatus.FINISHED;
74+
}
75+
}).build();
76+
}
77+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.wangqiang.springbatch.config;
2+
3+
import com.wangqiang.springbatch.listener.MyChunkListener;
4+
import com.wangqiang.springbatch.listener.MyJobListener;
5+
import org.springframework.batch.core.Job;
6+
import org.springframework.batch.core.Step;
7+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
8+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
9+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
10+
import org.springframework.batch.item.ItemReader;
11+
import org.springframework.batch.item.ItemWriter;
12+
import org.springframework.batch.item.support.ListItemReader;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
17+
import java.util.Arrays;
18+
import java.util.List;
19+
20+
@Configuration
21+
@EnableBatchProcessing
22+
public class ListenerDemo {
23+
24+
@Autowired
25+
private JobBuilderFactory jobBuilderFactory;
26+
27+
@Autowired
28+
private StepBuilderFactory stepBuilderFactory;
29+
30+
@Bean
31+
public Job listenerJob(){
32+
return jobBuilderFactory.get("listereJob")
33+
.start(listenerStep1())
34+
.listener(new MyJobListener())
35+
.build();
36+
}
37+
38+
@Bean
39+
public Step listenerStep1() {
40+
return stepBuilderFactory.get("listenerStep1")
41+
.<String,String>chunk(2) //read process write,需要写泛型
42+
.faultTolerant()
43+
.listener(new MyChunkListener())
44+
.reader(reader())
45+
.writer(write())
46+
.build();
47+
48+
}
49+
50+
@Bean
51+
public ItemWriter<String> write() {
52+
return new ItemWriter<String>() {
53+
@Override
54+
public void write(List<? extends String> items) throws Exception {
55+
for (String item:items){
56+
System.out.println("item "+item);
57+
}
58+
}
59+
};
60+
}
61+
62+
@Bean
63+
public ItemReader<String> reader() {
64+
return new ListItemReader<>(Arrays.asList("java","spring","mybatis"));
65+
}
66+
}

0 commit comments

Comments
 (0)