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
93e149d
commit 643b7d8
Showing
11 changed files
with
374 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,45 @@ | ||
<?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-listener</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-batch-listener</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> | ||
</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
71.spring-batch-listener/src/main/java/cc/mrbird/batch/SpringBatchListenerApplication.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 SpringBatchListenerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBatchListenerApplication.class, args); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...atch-listener/src/main/java/cc/mrbird/batch/job/CompositeJobExecutionListenerJobDemo.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,75 @@ | ||
package cc.mrbird.batch.job; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobExecutionListener; | ||
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.core.listener.CompositeJobExecutionListener; | ||
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.Arrays; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class CompositeJobExecutionListenerJobDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
|
||
@Bean | ||
public Job compositeJobExecutionListenerJob() { | ||
return jobBuilderFactory.get("compositeJobExecutionListenerJob") | ||
.start(step()) | ||
.listener(compositeJobExecutionListener()) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.tasklet((contribution, chunkContext) -> { | ||
System.out.println("执行步骤...."); | ||
return RepeatStatus.FINISHED; | ||
}).build(); | ||
} | ||
|
||
private CompositeJobExecutionListener compositeJobExecutionListener() { | ||
CompositeJobExecutionListener listener = new CompositeJobExecutionListener(); | ||
|
||
// 任务监听器1 | ||
JobExecutionListener jobExecutionListenerOne = new JobExecutionListener() { | ||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
System.out.println("任务监听器One,before job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
System.out.println("任务监听器One,after job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
}; | ||
// 任务监听器2 | ||
JobExecutionListener jobExecutionListenerTwo = new JobExecutionListener() { | ||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
System.out.println("任务监听器Two,before job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
System.out.println("任务监听器Two,after job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
}; | ||
// 聚合 | ||
listener.setListeners(Arrays.asList(jobExecutionListenerOne, jobExecutionListenerTwo)); | ||
return listener; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
71.spring-batch-listener/src/main/java/cc/mrbird/batch/job/ListenerTestJobDemo.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,84 @@ | ||
package cc.mrbird.batch.job; | ||
|
||
import cc.mrbird.batch.listener.*; | ||
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.*; | ||
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.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class ListenerTestJobDemo { | ||
|
||
@Autowired | ||
private JobBuilderFactory jobBuilderFactory; | ||
@Autowired | ||
private StepBuilderFactory stepBuilderFactory; | ||
@Autowired | ||
private MyJobExecutionListener myJobExecutionListener; | ||
@Autowired | ||
private MyStepExecutionListener myStepExecutionListener; | ||
@Autowired | ||
private MyChunkListener myChunkListener; | ||
@Autowired | ||
private MyItemReaderListener myItemReaderListener; | ||
@Autowired | ||
private MyItemProcessListener myItemProcessListener; | ||
@Autowired | ||
private MyItemWriterListener myItemWriterListener; | ||
|
||
@Bean | ||
public Job listenerTestJob() { | ||
return jobBuilderFactory.get("listenerTestJob") | ||
.start(step()) | ||
.listener(myJobExecutionListener) | ||
.build(); | ||
} | ||
|
||
private Step step() { | ||
return stepBuilderFactory.get("step") | ||
.listener(myStepExecutionListener) | ||
.<String, String>chunk(2) | ||
.faultTolerant() | ||
.listener(myChunkListener) | ||
.reader(reader()) | ||
.listener(myItemReaderListener) | ||
.processor(processor()) | ||
.listener(myItemProcessListener) | ||
.writer(list -> list.forEach(System.out::println)) | ||
.listener(myItemWriterListener) | ||
.build(); | ||
} | ||
|
||
private ItemReader<String> reader() { | ||
List<String> data = Arrays.asList("java", "c++", "javascript", "python"); | ||
return new simpleReader(data); | ||
} | ||
|
||
private ItemProcessor<String, String> processor() { | ||
return item -> item + " language"; | ||
} | ||
} | ||
|
||
class simpleReader implements ItemReader<String> { | ||
private Iterator<String> iterator; | ||
|
||
public simpleReader(List<String> data) { | ||
this.iterator = data.iterator(); | ||
} | ||
|
||
@Override | ||
public String read() { | ||
return iterator.hasNext() ? iterator.next() : null; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyChunkListener.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.listener; | ||
|
||
import org.springframework.batch.core.ChunkListener; | ||
import org.springframework.batch.core.scope.context.ChunkContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyChunkListener implements ChunkListener { | ||
@Override | ||
public void beforeChunk(ChunkContext context) { | ||
System.out.println("before chunk: " + context.getStepContext().getStepName()); | ||
} | ||
|
||
@Override | ||
public void afterChunk(ChunkContext context) { | ||
System.out.println("after chunk: " + context.getStepContext().getStepName()); | ||
} | ||
|
||
@Override | ||
public void afterChunkError(ChunkContext context) { | ||
System.out.println("before chunk error: " + context.getStepContext().getStepName()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyItemProcessListener.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,25 @@ | ||
package cc.mrbird.batch.listener; | ||
|
||
import org.springframework.batch.core.ItemProcessListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyItemProcessListener implements ItemProcessListener<String, String> { | ||
@Override | ||
public void beforeProcess(String item) { | ||
System.out.println("before process: " + item); | ||
} | ||
|
||
@Override | ||
public void afterProcess(String item, String result) { | ||
System.out.println("after process: " + item + " result: " + result); | ||
} | ||
|
||
@Override | ||
public void onProcessError(String item, Exception e) { | ||
System.out.println("on process error: " + item + " , error message: " + e.getMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyItemReaderListener.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,25 @@ | ||
package cc.mrbird.batch.listener; | ||
|
||
import org.springframework.batch.core.ItemReadListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyItemReaderListener implements ItemReadListener<String> { | ||
@Override | ||
public void beforeRead() { | ||
System.out.println("before read"); | ||
} | ||
|
||
@Override | ||
public void afterRead(String item) { | ||
System.out.println("after read: " + item); | ||
} | ||
|
||
@Override | ||
public void onReadError(Exception ex) { | ||
System.out.println("on read error: " + ex.getMessage()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyItemWriterListener.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,28 @@ | ||
package cc.mrbird.batch.listener; | ||
|
||
import org.springframework.batch.core.ItemWriteListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyItemWriterListener implements ItemWriteListener<String> { | ||
|
||
@Override | ||
public void beforeWrite(List<? extends String> items) { | ||
System.out.println("before write: " + items); | ||
} | ||
|
||
@Override | ||
public void afterWrite(List<? extends String> items) { | ||
System.out.println("after write: " + items); | ||
} | ||
|
||
@Override | ||
public void onWriteError(Exception exception, List<? extends String> items) { | ||
System.out.println("on write error: " + items + " , error message: " + exception.getMessage()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyJobExecutionListener.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,22 @@ | ||
package cc.mrbird.batch.listener; | ||
|
||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobExecutionListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyJobExecutionListener implements JobExecutionListener { | ||
|
||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
System.out.println("before job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
System.out.println("after job execute: " + jobExecution.getJobInstance().getJobName()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
71.spring-batch-listener/src/main/java/cc/mrbird/batch/listener/MyStepExecutionListener.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,23 @@ | ||
package cc.mrbird.batch.listener; | ||
|
||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.annotation.AfterStep; | ||
import org.springframework.batch.core.annotation.BeforeStep; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyStepExecutionListener { | ||
|
||
@BeforeStep | ||
public void breforeStep(StepExecution stepExecution) { | ||
System.out.println("before step execute: " + stepExecution.getStepName()); | ||
} | ||
|
||
@AfterStep | ||
public void afterStep(StepExecution stepExecution) { | ||
System.out.println("after step execute: " + stepExecution.getStepName()); | ||
} | ||
} |
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,6 @@ | ||
spring: | ||
datasource: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
url: jdbc:mysql://127.0.0.1:3306/springbatch | ||
username: root | ||
password: 123456 |