Skip to content

Commit

Permalink
feat(root) 新增 starter-batch模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhengjiaao committed Nov 28, 2022
1 parent 793687e commit 087d771
Show file tree
Hide file tree
Showing 24 changed files with 907 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<modules>
<module>starter-actuator</module>
<module>starter-aop</module>
<module>starter-batch</module>
<!-- <module>starter-batch</module>-->
<module>starter-cache</module>
<module>starter-data</module>
<module>starter-dubbo</module>
Expand Down
13 changes: 13 additions & 0 deletions starter-batch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# starter-batch-未完成

- [spring-batch 官网](https://spring.io/projects/spring-batch#overview)
- [spring-batch-samples 官方示例](https://github.com/spring-projects/spring-batch/tree/main/spring-batch-samples)
- [spring-batch 参考示例](https://blog.csdn.net/qq_37556726/article/details/96028675)

> Spring Batch是一个开源的、全面的、轻量级的批处理框架,通过Spring Batch可以实现强大的批处理应用程序的开发。
> Spring Batch还提供记录/跟踪、事务管理、作业处理统计、作业重启以及资源管理等功能。
> Spring Batch结合定时任务可以发挥更大的作用。
> Spring Batch提供了ItemReader、ItemProcessor和ItemWriter来完成数据的读取、处理以及写出操作,并且可以将批处理的执行状态持久化到数据库中。


57 changes: 57 additions & 0 deletions starter-batch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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>com.zja</groupId>
<artifactId>spring-boot-starter-test-root</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>

<groupId>com.zja</groupId>
<artifactId>starter-batch</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
28 changes: 28 additions & 0 deletions starter-batch/src/main/java/com/zja/BatchApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2022-03-11 9:51
* @Since:
*/
package com.zja;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* http://localhost:8080/swagger-ui/index.html#/
*
* 要实现多Job的情况,需要把EnableBatchProcessing注解的modular设置为true,让每个Job使用自己的ApplicationConext
*/
@SpringBootApplication
@EnableBatchProcessing(modular = false) // 开启Spring Batch支持
public class BatchApplication {

public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}

}
166 changes: 166 additions & 0 deletions starter-batch/src/main/java/com/zja/config/OneBatchConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2022-11-24 16:50
* @Since:
*/
package com.zja.config;

import com.zja.dto.OneDTO;
import com.zja.entity.OneEntity;
import com.zja.listeners.OneJobExecutionListener;
import com.zja.listeners.OneReadListener;
import com.zja.listeners.OneWriteListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableBatchProcessing
public class OneBatchConfig extends DefaultBatchConfigurer {

@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Autowired
DataSource dataSource;

@Resource
private PlatformTransactionManager transactionManager;
@Resource
private EntityManagerFactory entityManagerFactory;

/*
@Bean
@Primary
public JpaTransactionManager jpaTransactionManager() {
final JpaTransactionManager tm = new JpaTransactionManager();
tm.setDataSource(dataSource);
return tm;
}*/

/**
* 任务启动器
*/
/* @Override
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(createJobRepository());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}*/

/**
* 任务存储
*/
/* @Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDatabaseType("onesql");
factory.setTransactionManager(transactionManager);
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
return factory.getObject();
}
*/

/**
* 定义Job
*/
@Bean("oneJob")
public Job job(JobBuilderFactory builder, @Qualifier("oneStep") Step step) {
return builder.get("oneJob")
.incrementer(new RunIdIncrementer())
.flow(step)
.end()
.listener(new OneJobExecutionListener())
.build();
}

@Bean("oneItemReader")
public ItemReader<OneDTO> reader() {
FlatFileItemReader<OneDTO> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("csv/one.csv"));
reader.setLinesToSkip(1);
reader.setLineMapper(new DefaultLineMapper<OneDTO>() {
{
setLineTokenizer(new DelimitedLineTokenizer(",") {
{
setNames("uid", "name");
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<OneDTO>() {
{
setTargetType(OneDTO.class);
}
});
}
});
return reader;
}

@Bean("oneItemProcessor")
public ItemProcessor<OneDTO, OneEntity> processor() {
return new ItemProcessor<OneDTO, OneEntity>() {
@Override
public OneEntity process(OneDTO item) throws Exception {
OneEntity p = new OneEntity();
p.setUid(item.getUid());
p.setName(item.getName());
return p;
}
};
}

@Bean("oneItemWriter")
public ItemWriter<OneEntity> itemWriter() {
JpaItemWriterBuilder<OneEntity> builder = new JpaItemWriterBuilder<>();
builder.entityManagerFactory(entityManagerFactory);
return builder.build();
}

@Bean("oneStep")
public Step step(StepBuilderFactory stepBuilderFactory,
@Qualifier("oneItemReader") ItemReader<OneDTO> reader,
@Qualifier("oneItemWriter") ItemWriter<OneEntity> writer,
@Qualifier("oneItemProcessor") ItemProcessor<OneDTO, OneEntity> processor) {
return stepBuilderFactory
.get("oneStep")
.<OneDTO, OneEntity>chunk(2) // Chunk的机制(即每次读取一条数据,再处理一条数据,累积到一定数量后再一次性交给writer进行写入操作)
// .reader(reader).faultTolerant().retryLimit(3).retry(Exception.class).skip(Exception.class).skipLimit(2)
.reader(reader)
.listener(new OneReadListener())
.processor(processor)
// .writer(writer).faultTolerant().skip(Exception.class).skipLimit(2)
.writer(writer)
.listener(new OneWriteListener())
.transactionManager(transactionManager)
.build();
}

}
40 changes: 40 additions & 0 deletions starter-batch/src/main/java/com/zja/config/Swagger3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @Company: 上海数慧系统技术有限公司
* @Department: 数据中心
* @Author: 郑家骜[ào]
* @Email: [email protected]
* @Date: 2021-10-22 15:17
* @Since:
*/
package com.zja.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
* http://localhost:prot/swagger-ui/index.html#/
*/
@EnableOpenApi
@Configuration
public class Swagger3Config {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.zja")).paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("提供rest服务").description("我是描述").contact(new Contact("联系人", "www.baidu.com", "[email protected]"))
.version("1.0").build();
}
}
Loading

0 comments on commit 087d771

Please sign in to comment.