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
c54c5ac
commit 93e149d
Showing
12 changed files
with
458 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,49 @@ | ||
<?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-itemprocessor</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-batch-itemprocessor</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>mysql</groupId> | ||
<artifactId>mysql-connector-java</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-validation</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
15 changes: 15 additions & 0 deletions
15
...atch-itemprocessor/src/main/java/cc/mrbird/batch/SpringBatchItemprocessorApplication.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,15 @@ | ||
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 SpringBatchItemprocessorApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBatchItemprocessorApplication.class, args); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/TestData.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,57 @@ | ||
package cc.mrbird.batch.entity; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
public class TestData { | ||
|
||
private int id; | ||
private String field1; | ||
private String field2; | ||
@NotBlank | ||
private String field3; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getField1() { | ||
return field1; | ||
} | ||
|
||
public void setField1(String field1) { | ||
this.field1 = field1; | ||
} | ||
|
||
public String getField2() { | ||
return field2; | ||
} | ||
|
||
public void setField2(String field2) { | ||
this.field2 = field2; | ||
} | ||
|
||
public String getField3() { | ||
return field3; | ||
} | ||
|
||
public void setField3(String field3) { | ||
this.field3 = field3; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TestData{" + | ||
"id=" + id + | ||
", field1='" + field1 + '\'' + | ||
", field2='" + field2 + '\'' + | ||
", field3='" + field3 + '\'' + | ||
'}'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...emprocessor/src/main/java/cc/mrbird/batch/entity/job/BeanValidatingItemProcessorDemo.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,50 @@ | ||
package cc.mrbird.batch.entity.job; | ||
|
||
import cc.mrbird.batch.entity.TestData; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.batch.item.validator.BeanValidatingItemProcessor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class BeanValidatingItemProcessorDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private ListItemReader<TestData> simpleReader; | ||
|
||
@Bean | ||
public Job beanValidatingItemProcessorJob() throws Exception { | ||
return jobBuilderFactory.get("beanValidatingItemProcessorJob") | ||
.start(step()) | ||
.build(); | ||
} | ||
|
||
private Step step() throws Exception { | ||
return stepBuilderFactory.get("step") | ||
.<TestData, TestData>chunk(2) | ||
.reader(simpleReader) | ||
.processor(beanValidatingItemProcessor()) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.build(); | ||
} | ||
|
||
private BeanValidatingItemProcessor<TestData> beanValidatingItemProcessor() throws Exception { | ||
BeanValidatingItemProcessor<TestData> beanValidatingItemProcessor = new BeanValidatingItemProcessor<>(); | ||
// 开启过滤,不符合规则的数据被过滤掉; | ||
beanValidatingItemProcessor.setFilter(true); | ||
beanValidatingItemProcessor.afterPropertiesSet(); | ||
return beanValidatingItemProcessor; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/CompositeItemProcessorDemo.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,61 @@ | ||
package cc.mrbird.batch.entity.job; | ||
|
||
import cc.mrbird.batch.entity.TestData; | ||
import cc.mrbird.batch.processor.TestDataFilterItemProcessor; | ||
import cc.mrbird.batch.processor.TestDataTransformItemPorcessor; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.batch.item.support.CompositeItemProcessor; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class CompositeItemProcessorDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private ListItemReader<TestData> simpleReader; | ||
@Autowired | ||
private TestDataFilterItemProcessor testDataFilterItemProcessor; | ||
@Autowired | ||
private TestDataTransformItemPorcessor testDataTransformItemPorcessor; | ||
|
||
@Bean | ||
public Job compositeItemProcessorJob() { | ||
return jobBuilderFactory.get("compositeItemProcessorJob") | ||
.start(step()) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.<TestData, TestData>chunk(2) | ||
.reader(simpleReader) | ||
.processor(compositeItemProcessor()) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.build(); | ||
} | ||
|
||
// CompositeItemProcessor组合多种中间处理器 | ||
private CompositeItemProcessor<TestData, TestData> compositeItemProcessor() { | ||
CompositeItemProcessor<TestData, TestData> processor = new CompositeItemProcessor<>(); | ||
List<ItemProcessor<TestData, TestData>> processors = Arrays.asList(testDataFilterItemProcessor, testDataTransformItemPorcessor); | ||
// 代理两个processor | ||
processor.setDelegates(processors); | ||
return processor; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...emprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataFilterItemProcessorDemo.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.entity.job; | ||
|
||
import cc.mrbird.batch.entity.TestData; | ||
import cc.mrbird.batch.processor.TestDataFilterItemProcessor; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class TestDataFilterItemProcessorDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private ListItemReader<TestData> simpleReader; | ||
@Autowired | ||
private TestDataFilterItemProcessor testDataFilterItemProcessor; | ||
|
||
@Bean | ||
public Job testDataFilterItemProcessorJob() { | ||
return jobBuilderFactory.get("testDataFilterItemProcessorJob") | ||
.start(step()) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.<TestData, TestData>chunk(2) | ||
.reader(simpleReader) | ||
.processor(testDataFilterItemProcessor) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.build(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...rocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataTransformItemPorcessorDemo.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,45 @@ | ||
package cc.mrbird.batch.entity.job; | ||
|
||
import cc.mrbird.batch.entity.TestData; | ||
import cc.mrbird.batch.processor.TestDataFilterItemProcessor; | ||
import cc.mrbird.batch.processor.TestDataTransformItemPorcessor; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class TestDataTransformItemPorcessorDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private ListItemReader<TestData> simpleReader; | ||
@Autowired | ||
private TestDataTransformItemPorcessor testDataTransformItemPorcessor; | ||
|
||
@Bean | ||
public Job testDataTransformItemPorcessorJob() { | ||
return jobBuilderFactory.get("testDataTransformItemPorcessorJob") | ||
.start(step()) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.<TestData, TestData>chunk(2) | ||
.reader(simpleReader) | ||
.processor(testDataTransformItemPorcessor) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.build(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...h-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/ValidatingItemProcessorDemo.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,55 @@ | ||
package cc.mrbird.batch.entity.job; | ||
|
||
import cc.mrbird.batch.entity.TestData; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.support.ListItemReader; | ||
import org.springframework.batch.item.validator.ValidatingItemProcessor; | ||
import org.springframework.batch.item.validator.ValidationException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class ValidatingItemProcessorDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private ListItemReader<TestData> simpleReader; | ||
|
||
@Bean | ||
public Job validatingItemProcessorJob() { | ||
return jobBuilderFactory.get("validatingItemProcessorJob") | ||
.start(step()) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.<TestData, TestData>chunk(2) | ||
.reader(simpleReader) | ||
.processor(validatingItemProcessor()) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.build(); | ||
} | ||
|
||
private ValidatingItemProcessor<TestData> validatingItemProcessor() { | ||
ValidatingItemProcessor<TestData> processor = new ValidatingItemProcessor<>(); | ||
processor.setValidator(value -> { | ||
// 对每一条数据进行校验 | ||
if ("".equals(value.getField3())) { | ||
// 如果field3的值为空串,则抛异常 | ||
throw new ValidationException("field3的值不合法"); | ||
} | ||
}); | ||
return processor; | ||
} | ||
} |
Oops, something went wrong.