spring batch源码解析(你知道SpringBatch)

优质文章,及时送达

spring batch源码解析(你知道SpringBatch)(1)

我将向您展示如何使用Spring Boot创建一个的Spring BatchHello World示例。

(循序渐进)

因此,如果您是Spring Batch的初学者,您一定会喜欢本指南

准备好了吗?

如果您想spring batch源码解析(你知道SpringBatch)(2)了解更多关于Spring Batch的信息,请访问Spring Batch教程页面。

1.Spring Batch框架工作原理

在深入研究代码之前,让我们先看看Spring Batch框架。它包含以下主要构建块:

spring batch源码解析(你知道SpringBatch)(3)

Spring Batch框架

一个Batch(批处理)过程由一个Job(作业)组成。这个实体封装了整个批处理过程。

一个Job(作业)可以由一个或多个Step(步骤)组成。在大多数情况下,一个步骤将读取数据(通过ItemReader),处理数据(使用ItemProcessor),然后写入数据(通过ItemWriter)。

JobLauncher处理启动一个Job(作业)。

最后,JobRepository存储关于配置和执行的Job(作业)的元数据。

为了演示Spring Batch是如何工作的,让我们构建一个简单的Hello World批处理作业。

在本例中,我们从person.csv文件中读取一个人的姓和名。从这些数据生成一个问候语。然后将此问候语写入greeting .txt文件。

2.示例概述

我们会使用以下工具/框架:

  • Spring Batch 4.1

  • Spring Boot 2.1

  • Maven 3.6

我们的项目目录结构如下:

spring batch源码解析(你知道SpringBatch)(4)

项目目录结构

3. Maven配置

我们使用Maven构建并运行示例。如果还没有,spring batch源码解析(你知道SpringBatch)(5)下载并安装apache Maven。

让我们使用Spring Initializr来生成Maven项目。确保选择Batch作为依赖项。

spring batch源码解析(你知道SpringBatch)(6)

image

单击Generate Project生成并spring batch源码解析(你知道SpringBatch)(7)下载Spring Boot项目模板。在项目的根目录中,您将发现一个pom.xml文件,它是Maven项目的XML配置文件。

为了避免必须管理不同Spring依赖项的版本兼容性,我们将从spring-boot-starter-parent父POM继承默认配置。

生成的项目包含Spring Boo Starters管理着不同的Spring依赖项。

spring-boot-starter-batch导入Spring BootSpring Batch依赖项。

spring-boot-starter-test包含用于测试Spring引导应用程序的依赖项。它导入了包括JUnit、Hamcrest和Mockito在内的库。

这个也有依赖性spring-batch-test。这个库包含一些帮助类,它们将帮助测试批处理作业。

在plugins部分,您将找到Spring Boot Maven插件:spring-boot-maven- plugin。它帮助我们构建一个单一的、可运行的uber-jar。这是执行和发布代码的一种方便方法。此外,插件允许您通过Maven命令启动示例。

<?xml version="1.0" encoding="UTF-8"?><project xmlns="spring batch源码解析(你知道SpringBatch)(8)http://maven.apache.org/POM/4.0.0"xmlns:xsi="spring batch源码解析(你知道SpringBatch)(9)http://spring batch源码解析(你知道SpringBatch)(10)www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="spring batch源码解析(你知道SpringBatch)(11)http://maven.apache.org/POM/4.0.0 spring batch源码解析(你知道SpringBatch)(12)http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.codenotfound</groupId><artifactId>spring-batch-hello-world</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-batch-hello-world</name><description>Spring Batch Hello World Example</description><url>https://codenotfoundspring batch源码解析(你知道SpringBatch)(13).com/spring-batch-example.html</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

4. Spring Boot 配置

我们使用Spring Boot,目的是让一个Spring Batch应用程序可以“直接运行”。

首先创建一个SpringBatchApplication类。它包含main方法,该方法使用Spring BootSpringApplication.run启动应用程序。

注意

@SpringBootApplication是一个方便的注解,它添加了:@Configuration@EnableAutoConfiguration@ComponentScan

有关Spring Boot的更多信息,请查看Spring Boot入门指南。

默认情况下,Spring Batch使用数据库存储已配置的批处理作业上的元数据。

在本例中,我们不直接使用数据库,而是使用基于内存映射的Map,运行Spring Batch

spring-boot-starter-batch starter依赖于spring-boot-starter-jdbc,并将尝试实例化数据源。添加exclude = {DataSourceAutoConfiguration.class}注解中添加@SpringBootApplication。这可以防止Spring Boot为数据库连接自动配置DataSource

package com.codenotfound;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})publicclassSpringBatchApplication{publicstaticvoidmain(String[] args) {SpringApplication.run(SpringBatchApplication.class, args);}}

5. 创建实体模型

在处理数据之前,通常希望将其映射到实体对象。

在我的示例中,输入数据存储在src/test/resources/csv/persons.csv文件中。

文件中的每一行都包含一个逗号分隔的姓和名。

John, DoeJane, Doe

我们将把这个数据映射到Person对象。这是一个包含姓和名的简单POJO

package com.codenotfound.model;publicclassPerson{private String firstName;private String lastName;publicPerson {// default constructor}public String getFirstName {return firstName;}publicvoidsetFirstName(String firstName) {this.firstName = firstName;}public String getLastName {return lastName;}publicvoidsetLastName(String lastName) {this.lastName = lastName;}@Overridepublic String toString {return firstName " " lastName;}}

6. 配置 Spring Batch Job

我们首先创建一个BatchConfig类,它将配置Spring Batch。类顶部的@Configuration注解表明Spring可以使用该类作为bean定义的源。

我们添加了@EnableBatchProcessing注解,它支持所有所需Spring Batch特性。它还提供了设置批处理作业的基本配置。

通过添加这个注解会需要很多操作。下面是@EnableBatchProcessing创建的概述:

  • JobRepository (bean名称 "jobRepository")

  • JobLauncher (bean名称 "jobLauncher")

  • JobRegistry (bean名称 "jobRegistry")

  • JobExplorer (bean名称 "jobExplorer")

  • PlatformTransactionManager (bean名称 "transactionManager")

  • JobBuilderFactory (bean名称"jobBuilders"),它可以方便地防止您必须将作业存储库注入到每个Job(作业)中

  • StepBuilderFactory (bean名称 "stepBuilders"),以方便您避免将作业存储库和事务管理器注入到每个Step(步骤)中

为了使Spring Batch使用基于Map的JobRepository,我们需要扩展DefaultBatchConfigurer。重写setDataSource方法以不设置DataSource。这将导致自动配置使用基于Map的JobRepository

@Configuration@EnableBatchProcessingpublic class BatchConfig extends DefaultBatchConfigurer {@Overridepublic void setDataSource(DataSource dataSource) {// initialize will use a Map based JobRepository (instead of database)}}

现在让我们继续配置Hello World Spring Batch 作业。

创建一个HelloWorldJobConfig配置类,并用添加@Configuration注解。

HelloWorldJobConfigBean中,我们使用JobBuilderFactory来创建作业。我们传递Job(作业)的名称和需要运行的Step(步骤)。

注意

helloWorlJobBean中,Spring将自动注入jobBuildersstepBuildersBeans。

HelloWorldStepBean中定义了我们的步骤执行的不同项。我们使用StepBuilderFactory创建步骤。

首先,我们传入步骤的名称。使用chunk,我们指定每个事务中处理的项的数量。Chunk还指定步骤的输入(Person)和输出(String)类型。然后,我们将ItemReader (reader)ItemProcessor (processor)ItemWriter (writer)添加到步骤中。

我们使用FlatFileItemReader读取person CSV文件。这个类提供了读取和解析CSV文件的基本功能。

有一个FlatFileItemReaderBuilder实现,它允许我们创建一个FlatFileItemReader。我们首先指定读取文件中每一行的结果是Person对象。然后,我们将使用name方法为FlatFileItemReader添加一个名称,并指定需要读取的资源(在本例中是persons.csv文件)。

为了让FlatFileItemReader处理我们的文件,我们需要指定一些额外的信息。首先,我们定义文件中的数据是带分隔符的(默认为逗号作为分隔符)。

我们还指定了如何将一行中的每个字段映射到Person对象。这是使用names来完成的,通过将名称与对象上的setter匹配,可以使Spring Batch映射字段。

在本文的例子中,一行的第一个字段将使用firstNamesetter进行映射。为了实现这一点,我们还需要指定targetType,即Person对象。

注意

names(new String[] {"firstName", "lastName"})

PersonItemProcessor处理数据。它将一个Person转换成一个问候String。我们将在下面的一个单独的类中定义它。

一旦数据被处理,我们将把它写入一个文本文件。我们使用FlatFileItemWriter来完成这项任务。

我们使用FlatFileItemWriterBuilder实现来创建一个FlatFileItemWriter。我们为writer添加一个名称,并指定需要将数据写入其中的资源(在本例中是greeting.txt文件)。

FlatFileItemWriter需要知道如何将生成的输出转换成可以写入文件的单个字符串。在本例中,我们的输出已经是一个字符串,我们可以使用PassThroughLineAggregator。这是最基本的实现,它假定对象已经是一个字符串。

@ConfigurationpublicclassHelloWorldJobConfig{@Beanpublic Job helloWorlJob(JobBuilderFactory jobBuilders,StepBuilderFactory stepBuilders) {return jobBuilders.get("helloWorldJob").start(helloWorldStep(stepBuilders)).build;}@Beanpublic Step helloWorldStep(StepBuilderFactory stepBuilders) {return stepBuilders.get("helloWorldStep").<Person, String>chunk(10).reader(reader).processor(processor).writer(writer).build;}@Beanpublic FlatFileItemReader<Person> reader {return new FlatFileItemReaderBuilder<Person>.name("personItemReader").resource(new ClassPathResource("csv/persons.csv")).delimited.names(new String[] {"firstName", "lastName"}).targetType(Person.class).build;}@Beanpublic PersonItemProcessor processor {return new PersonItemProcessor;}@Beanpublic FlatFileItemWriter<String> writer {return new FlatFileItemWriterBuilder<String>.name("greetingItemWriter").resource(new FileSystemResource("target/test-outputs/greetings.txt")).lineAggregator(new PassThroughLineAggregator<>).build;}}

7. 处理数据

在大多数情况下,您将希望在批处理作业期间应用一些数据处理。可以使用ItemProcessor来操作。

在我们的示例中,我们将Person对象转换为一个简单的问候语String

为此,我们创建一个实现ItemProcessor接口的PersonItemProcessor。我们实现了process方法,它将人名和姓氏添加到字符串中。

调试的过程中,我们记录日志结果。

public class PersonItemProcessorimplements ItemProcessor<Person, String> {private static final Logger LOGGER =LoggerFactory.getLogger(PersonItemProcessor.class);@Overridepublic String process(Person person) throws Exception {String greeting = "Hello " person.getFirstName " " person.getLastName "!";LOGGER.info("converting '{}' into '{}'", person, greeting);return greeting;}}

8.测试Spring Batch 示例

为了测试本的例子,我们创建了一个基本的单元测试用例。它将运行批处理作业并检查是否成功完成。

我们使用@RunWith@SpringBootTest测试注解告诉JUnit使用Spring的测试支持运行,并使用SpringBoot的支持引导。

Spring Batch附带一个JobLauncherTestUtils实用程序类,用于测试批处理作业。

我们首先创建一个内部BatchTestConfig类,将helloWorld作业添加到JobLauncherTestUtilsbean中。然后使用此bean的launchJob方法运行批处理作业。

如果执行的作业没有任何错误,则Espring batch源码解析(你知道SpringBatch)(14)xitCode的值为COMPLETED

@RunWith(SpringRunner.class)@SpringBootTest(classes = {SpringBatchApplicationTests.BatchTestConfig.class})publicclassSpringBatchApplicationTests{@Autowiredprivate JobLauncherTestUtils jobLauncherTestUtils;@Testpublic void testHelloWorldJob throws Exception {JobExecution jobExecution = jobLauncherTestUtils.launchJob;assertThat(jobExecution.getEspring batch源码解析(你知道SpringBatch)(15)xitStatus.getEspring batch源码解析(你知道SpringBatch)(16)xitCode).isEqualTo("COMPLETED");}@Configuration@Import({BatchConfig.class, HelloWorldJobConfig.class})staticclassBatchTestConfig{@Autowiredprivate Job helloWorlJob;@BeanJobLauncherTestUtils jobLauncherTestUtilsthrows NoSuchJobException {JobLauncherTestUtils jobLauncherTestUtils =new JobLauncherTestUtils;jobLauncherTestUtils.setJob(helloWorlJob);return jobLauncherTestUtils;}}}

要触发上述测试用例,请在项目根文件夹中打开命令提示符,并执行以下Maven命令:

mvn test

结果是构建成功,并在此期间执行批处理作业。

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( \___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.1.5.RELEASE)2019-05-spring batch源码解析(你知道SpringBatch)(17)30 19:11:12.784 INFO spring batch源码解析(你知道SpringBatch)(18)14588 --- [ main] c.c.SpringBatchApplicationTests : Starting SpringBatchApplicationTests on DESKTOP-2RB3C1U with PID spring batch源码解析(你知道SpringBatch)(19)14588 (started by Codenotfound in C:\Users\Codenotfound\repos\spring-batch\spring-batch-hello-world)2019-05-spring batch源码解析(你知道SpringBatch)(20)30 19:11:12.785 INFO spring batch源码解析(你知道SpringBatch)(21)14588 --- [ main] c.c.SpringBatchApplicationTests : No active profile set, falling back to default profiles: default2019-05-spring batch源码解析(你知道SpringBatch)(22)30 19:11:13.spring batch源码解析(你知道SpringBatch)(23)305 WARN spring batch源码解析(你知道SpringBatch)(24)14588 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No datasource was provided...using a Map based JobRepository2019-05-spring batch源码解析(你知道SpringBatch)(25)30 19:11:13.spring batch源码解析(你知道SpringBatch)(26)306 WARN spring batch源码解析(你知道SpringBatch)(27)14588 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a ResourcelessTransactionManager2019-05-spring batch源码解析(你知道SpringBatch)(28)30 19:11:13.328 INFO spring batch源码解析(你知道SpringBatch)(29)14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.2019-05-spring batch源码解析(你知道SpringBatch)(30)30 19:11:13.350 INFO spring batch源码解析(你知道SpringBatch)(31)14588 --- [ main] c.c.SpringBatchApplicationTests : Started SpringBatchApplicationTests in 0.894 seconds (JVM running for 1.777)2019-05-spring batch源码解析(你知道SpringBatch)(32)30 19:11:13.spring batch源码解析(你知道SpringBatch)(33)732 INFO spring batch源码解析(你知道SpringBatch)(34)14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=helloWorldJob]] launched with the following parameters: [{random=459672}]2019-05-spring batch源码解析(你知道SpringBatch)(35)30 19:11:13.759 INFO spring batch源码解析(你知道SpringBatch)(36)14588 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [helloWorldStep]2019-05-spring batch源码解析(你知道SpringBatch)(37)30 19:11:13.812 INFO spring batch源码解析(你知道SpringBatch)(38)14588 --- [ main] c.c.batch.PersonItemProcessor : converting 'John Doe' into 'Hello John Doe!'2019-05-spring batch源码解析(你知道SpringBatch)(39)30 19:11:13.822 INFO spring batch源码解析(你知道SpringBatch)(40)14588 --- [ main] c.c.batch.PersonItemProcessor : converting 'Jane Doe' into 'Hello Jane Doe!'2019-05-spring batch源码解析(你知道SpringBatch)(41)30 19:11:13.842 INFO spring batch源码解析(你知道SpringBatch)(42)14588 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=helloWorldJob]] completed with the following parameters: [{random=459672}] and the following status: [COMPLETED][INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.953 s - in com.codenotfound.SpringBatchApplicationTests[INFO][INFO] Results:[INFO][INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 6.852 s[INFO] Finished at: 2019-05-spring batch源码解析(你知道SpringBatch)(43)30T19:11:14 02:00[INFO] ------------------------------------------------------------------------

您可以在target/test-output /greeting .txt文件中找到结果:

Hello John Doe!Hello Jane Doe!

如果您想运行上面的代码示例,您可以在这里获得完整的源代码。

在本入门教程中,您学习了如何使用Spring Boot和Maven创建一个简单的Spring Batch示例。

原文:https://codenotfoundspring batch源码解析(你知道SpringBatch)(44).com/spring-batch-example.html

作者:codenotfound

译者:[李东

【END】

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页