Spring Batch 简明教程

Spring Batch - Quick Guide

Spring Batch - Overview

Batch processing 是处理模式,此模式涉及在用户不进行交互的情况下执行一系列自动化复杂任务。批处理处理批量数据,且运行很长时间。

Batch processing is a processing mode which involves execution of series of automated complex jobs without user interaction. A batch process handles bulk data and runs for a long time.

需要处理大量数据以执行涉及操作的多个企业应用程序 −

Several Enterprise applications require to process huge data to perform operations involving −

  1. Time-based events such as periodic calculations.

  2. Periodic applications that are processed repetitively over large datasets.

  3. Applications that deals with processing and validation of the data available in a transactional manner.

因此,批处理用于企业应用程序以执行此类事务。

Therefore, batch processing is used in enterprise applications to perform such transactions.

What is Spring Batch

Spring Batch 是一款 lightweight framework ,此类用于开发企业应用程序中使用的 Batch Applications

Spring batch is a lightweight framework which is used to develop Batch Applications that are used in Enterprise Applications.

除了批量处理外,此框架还提供以下功能 −

In addition to bulk processing, this framework provides functions for −

  1. Including logging and tracing

  2. Transaction management

  3. Job processing statistics

  4. Job restart

  5. Skip and Resource management

你还可以使用其分区技术来扩展 Spring Batch 应用程序。

You can also scale spring batch applications using its portioning techniques.

Features of Spring Batch

以下是 Spring Batch 的著名功能 −

Following are the notable features of Spring Batch −

  1. Flexibility − Spring Batch applications are flexible. You simply need to change an XML file to alter the order of processing in an application.

  2. Maintainability − Spring Batch applications are easy to maintain. A Spring Batch job includes steps and each step can be decoupled, tested, and updated, without effecting the other steps.

  3. Scalability − Using the portioning techniques, you can scale the Spring Batch applications. These techniques allow you to − Execute the steps of a job in parallel. Execute a single thread in parallel.

  4. Reliability − In case of any failure, you can restart the job from exactly where it was stopped, by decoupling the steps.

  5. Support for multiple file formats − Spring Batch provides support for a large set of readers and writers such as XML, Flat file, CSV, MYSQL, Hibernate, JDBC, Mongo, Neo4j, etc.

  6. Multiple ways to launch a job − You can launch a Spring Batch job using web applications, Java programs, Command Line, etc.

除了这些之外,Spring Batch应用程序支持−

In addition to these, Spring Batch applications support −

  1. Automatic retry after failure.

  2. Tracking status and statistics during the batch execution and after completing the batch processing.

  3. To run concurrent jobs.

  4. Services such as logging, resource management, skip, and restarting the processing.

Spring Batch - Environment

在本章中,我们将解释如何将 Spring Batch 环境设置到 Eclipse IDE 中。在继续安装前,确保您已在您的系统中安装 Eclipse。如果没有,请在您的系统中下载并安装 Eclipse。

In this chapter, we will explain how to set Spring Batch environment in Eclipse IDE. Before proceeding with the installation, ensure that you have installed Eclipse in your system. If not, download and install Eclipse in your system.

如需了解 Eclipse 的更多信息,请参考我们的 Eclipse Tutorial.

For more information on Eclipse, please refer our Eclipse Tutorial.

Setting Spring Batch on Eclipse

按照以下步骤在 Eclipse 上设置 Spring Batch 环境。

Follow the steps given below to set Spring Batch environment on Eclipse.

Step 1 − 安装 Eclipse,然后打开一个新项目,如以下屏幕截图所示。

Step 1 − Install Eclipse and open a New Project as shown in the following screenshot.

new project

Step 2 − 创建一个样本 Spring Batch 项目,如下所示。

Step 2 − Create a Sample Spring Batch project as shown below.

project name

Step 3 − 右键单击项目,然后按以下所示将其转换为一个 Maven 项目。将其转换为 Maven 项目后,将提供 Pom.xml ,您需要在其中提及必需的依赖项。然后,将 jar 文件自动下载到您的项目中。

Step 3 − Right-click on the project and convert it into a Maven project as shown below. Once you convert it into Maven project, it will give you a Pom.xml where you need to mention the required dependencies. Thereafter, the jar files of those will be automatically downloaded into your project.

configure

Step 4 − 现在,在项目的 pom.xml 中,复制并粘贴以下内容(spring 批处理应用程序的依赖项),然后刷新项目。

Step 4 − Now, in the pom.xml of the project, copy and paste the following content (dependencies for spring batch application) and refresh the project.

<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
   http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>SpringBatchSample</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>SpringBatchExample</name>
   <url>http://maven.apache.org</url>

   <properties>
      <jdk.version>1.8</jdk.version>
      <spring.version>5.3.14</spring.version>
      <spring.batch.version>4.3.4</spring.batch.version>
      <mysql.driver.version>5.1.25</mysql.driver.version>
      <junit.version>4.11</junit.version>
   </properties>

   <dependencies>
      <!-- Spring Core -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
      </dependency>

      <!-- Spring jdbc, for database -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>${spring.version}</version>
      </dependency>

      <!-- Spring XML to/back object -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-oxm</artifactId>
         <version>${spring.version}</version>
      </dependency>

      <!-- MySQL database driver -->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql.driver.version}</version>
      </dependency>

      <!-- Spring Batch dependencies -->
      <dependency>
         <groupId>org.springframework.batch</groupId>
         <artifactId>spring-batch-core</artifactId>
         <version>${spring.batch.version}</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.batch</groupId>
         <artifactId>spring-batch-infrastructure</artifactId>
         <version>${spring.batch.version}</version>
      </dependency>

      <!-- Spring Batch unit test -->
      <dependency>
         <groupId>org.springframework.batch</groupId>
         <artifactId>spring-batch-test</artifactId>
         <version>${spring.batch.version}</version>
      </dependency>

      <!-- Junit -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <finalName>spring-batch</finalName>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
               <downloadSources>true</downloadSources>
               <downloadJavadocs>false</downloadJavadocs>
            </configuration>
         </plugin>

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
               <source>${jdk.version}</source>
               <target>${jdk.version}</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

Spring Batch - Architecture

以下是 Spring Batch 架构的示意图。如此图所示,此架构包含三个主要组件,即 Application, Batch CoreBatch Infrastructure

Following is the diagrammatic representation of the architecture of Spring Batch. As depicted in the figure, the architecture contains three main components namely, Application, Batch Core, and Batch Infrastructure.

architecture

Application − 此组件包含我们使用 Spring Batch 框架编写的全部作业和代码。

Application − This component contains all the jobs and the code we write using the Spring Batch framework.

Batch Core − 此组件包含控制和启动批处理作业所需的所有 API 类。

Batch Core − This component contains all the API classes that are needed to control and launch a Batch Job.

Batch Infrastructure − 此组件包含应用程序和批处理核心组件使用的读取器、编写器和服务。

Batch Infrastructure − This component contains the readers, writers, and services used by both application and Batch core components.

Components of Spring Batch

下图显示了 Spring Batch 的不同组件以及它们如何相互连接。

The following illustration shows the different components of Spring Batch and how they are connected with each other.

components

Job

在 Spring Batch 应用程序中,作业是要执行的批处理。它从开始到结束连续运行。此作业进一步细分为步骤(或作业包含步骤)。

In a Spring Batch application, a job is the batch process that is to be executed. It runs from start to finish without interruption. This job is further divided into steps (or a job contains steps).

我们将在 Spring Batch 中使用 XML 文件或 Java 类配置作业。以下是 Spring Batch 中对作业的 XML 配置。

We will configure a job in Spring Batch using an XML file or a Java class. Following is the XML configuration of a Job in Spring Batch.

<job id = "jobid">
   <step id = "step1" next = "step2"/>
   <step id = "step2" next = "step3"/>
   <step id = "step3"/>
</job>

批处理作业是在 <job></job> 标签中配置的。它有一个名为 id 的属性。在这些标签内,我们定义步骤的定义和顺序。

A Batch job is configured within the tags <job></job>. It has an attribute named id. Within these tags, we define the definition and ordering of the steps.

Restartable − 一般来说,当一个作业正在运行,并且我们尝试再次启动它,这被认为是 restart ,并且它将再次启动。为避免此问题,您需要将 restartable 值设置为 false ,如下所示。

Restartable − In general, when a job is running and we try to start it again that is considered as restart and it will be started again. To avoid this, you need to set the restartable value to false as shown below.

<job id = "jobid" restartable = "false" >

</job>

Step

一个 step 是一个作业的独立部分,其中包含定义和执行作业(其部分)所需的信息。

A step is an independent part of a job which contains the necessary information to define and execute the job (its part).

如该图所示,每一步都由一个 ItemReader、ItemProcessor(可选)和一个 ItemWriter 组成。 A job may contain one or more steps

As specified in the diagram, each step is composed of an ItemReader, ItemProcessor (optional) and an ItemWriter. A job may contain one or more steps.

Readers, Writers, and Processors

一个 item reader 从特定来源读取数据到一个 Spring Batch应用程序中,而 item writer 将数据从 Spring Batch 应用程序写入到特定目的地中。

An item reader reads data into a Spring Batch application from a particular source, whereas an item writer writes data from the Spring Batch application to a particular destination.

一个 Item processor 是一个包含处理从 Spring Batch 读入数据的处理器代码的类。如果应用程序读取 "n" 记录,那么处理器中的代码将被执行在每个记录上。

An Item processor is a class which contains the processing code which processes the data read into the spring batch. If the application reads "n" records, then the code in the processor will be executed on each record.

当未给定阅读器和编写器时,一个 tasklet 充当 SpringBatch 的处理器。它只处理一个单一任务。例如,如果我们正在编写一个作业,其中有一个简单的步骤,我们从 MySQL 数据库中读取数据并对其进行处理,然后将其写入文件(扁平文件),那么我们的步骤使用 −

When no reader and writer are given, a tasklet acts as a processor for SpringBatch. It processes only a single task. For example, if we are writing a job with a simple step in it where we read data from MySQL database and process it and write it to a file (flat), then our step uses −

  1. A reader which reads from MySQL database.

  2. A writer which writes to a flat file.

  3. A custom processor which processes the data as per our wish.

<job id = "helloWorldJob">
   <step id = "step1">
      <tasklet>
         <chunk reader = "mysqlReader" writer = "fileWriter"
            processor = "CustomitemProcessor" ></chunk>
      </tasklet>
   </step>
</ job>

Spring Batch 提供了一个 readerswriters 的长列表。使用这些预定义的类,我们能够为它们定义 Bean。我们将在以后的章节中详细讨论 readerswriters

Spring Batch provides a long list of readers and writers. Using these predefined classes, we can define beans for them. We will discuss readers and writers in greater detail in the coming chapters.

JobRepository

Spring Batch 中的作业存储库为 JobLauncher、Job 和 Step 实现提供创建、检索、更新和删除 (CRUD) 操作。我们将在一个 XML 文件中定义一个作业存储库,如下所示。

A Job repository in Spring Batch provides Create, Retrieve, Update, and Delete (CRUD) operations for the JobLauncher, Job, and Step implementations. We will define a job repository in an XML file as shown below.

<job-repository id = "jobRepository"/>

除了 id ,还有一些额外的选项(可选)。以下是带有所有选项及其默认值的作业存储库配置。

In addition to id, there are some more options (optional) available. Following is the configuration of job repository with all the options and their default values.

<job-repository id = "jobRepository"
   data-source = "dataSource"
   transaction-manager = "transactionManager"
   isolation-level-for-create = "SERIALIZABLE"
   table-prefix = "BATCH_"
   max-varchar-length = "1000"/>

In-Memory Repository − 如果您不想在数据库中保留 Spring Batch 的域对象,您可以按照以下所示配置 jobRepository 的内存版本。

In-Memory Repository − In case you don’t want to persist the domain objects of the Spring Batch in the database, you can configure the in-memory version of the jobRepository as shown below.

<bean id = "jobRepository"
   class = "org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean ">
   <property name = "transactionManager" ref = "transactionManager"/>
</bean>

JobLauncher

JobLauncher 是一个使用 given set of parameters 启动 Spring Batch 作业的接口。 SampleJoblauncher 是实现 JobLauncher 接口的类。以下是 JobLauncher 的配置。

JobLauncher is an interface which launces the Spring Batch job with the given set of parameters. SampleJoblauncher is the class which implements the JobLauncher interface. Following is the configuration of the JobLauncher.

<bean id = "jobLauncher"
   class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
   <property name = "jobRepository" ref = "jobRepository" />
</bean>

JobInstance

一个 JobInstance 表示一个作业的逻辑运行;它是在我们运行作业时创建的。每个作业实例通过作业的名称和在运行时传递给它的参数进行区分。

A JobInstance represents the logical run of a job; it is created when we run a job. Each job instance is differentiated by the name of the job and the parameters passed to it while running.

如果一个 JobInstance 执行失败,相同的 JobInstance 可以再次执行。因此,每个 JobInstance 可以有多个作业执行。

If a JobInstance execution fails, the same JobInstance can be executed again. Hence, each JobInstance can have multiple job executions.

JobExecution and StepExecution

JobExecution 和 StepExecution 是一个作业/步骤执行的表示。它们包含作业/步骤的运行信息,诸如开始时间(作业/步骤)、结束时间(作业/步骤)。

JobExecution and StepExecution are the representation of the execution of a job/step. They contain the run information of the job/step such as start time (of job/step), end time (of job/step).

Spring Batch - Application

本教程中几乎所有示例都包含以下文件 −

Almost all the examples in this tutorial contain the following files −

  1. Configuration file (XML file)

  2. Tasklet/processor (Java class)

  3. Java class with setters and getters (Java class (bean))

  4. Mapper class (Java class)

  5. Launcher class (Java class)

Configuration File

配置文件 (XML) 包含以下 −

The configuration file (XML) contains the following −

  1. The job and step definitions.

  2. Beans defining readers and writers.

  3. Definition of components like JobLauncher, JobRepository, Transaction Manager, and Data Source.

在我们的示例中,为了更好的理解,我们将其分为两个文件,即 job.xml 文件(定义 job、step、reader 和 writer)和 context.xml 文件(job launcher、job repository、transaction manager 和 data source)。

In our examples, for better understanding, we have divided this in to two files the job.xml file (defines job, step, reader and writer) and context.xml file (job launcher, job repository, transaction manager and data source).

Mapper Class

Mapper 类根据 reader 来实现 row mapperfield set mapper 等接口。它包含从 reader 获取数据并将它设置为带 settergetter 方法的 Java 类(Java Bean)的代码。

The Mapper class, depending upon the reader, implements interfaces such as row mapper, field set mapper, etc. It contains the code to get the data from the reader and to set it to a Java class with setter and getter methods (Java Bean).

Java Bean Class

settersgetters 的 Java 类(Java bean)表示具有多个值的数据。它作为一个帮助类。我们通过该类的对象的形式传递数据从一个组件(reader、writer、processor)到另一个组件。

A Java class with setters and getters (Java bean) represents data with multiple values. It acts as a helper class. We will pass the data from one component (reader, writer, processer) to other in the form of object of this class.

Tasklet/processor

Tasklet/processor 类包含 Spring Batch 应用程序的处理代码。processor 是一个接受包含读取数据、处理它并以对象形式返回处理数据的对象(in the form object) 的类。

The Tasklet/processor class contains the processing code of the Spring Batch application. A processor is a class which accepts an object that contains the data read, processes it, and returns the processed data (in the form object).

Launcher class

该类(App.java)包含启动 Spring Batch 应用程序的代码。

This class (App.java) contains the code to launch the Spring Batch application.

application

Spring Batch - Configuration

在编写Spring Batch应用程序时,我们将使用Spring Batch命名空间中提供的XML标记配置作业、步骤、JobLauncher、JobRepository、事务管理器、读取器和写入器。因此,您需要像下面显示的那样在XML文件中包含此命名空间。

While writing a Spring Batch application, we will configure the job, step, JobLauncher, JobRepository, Transaction Manager, readers, and writers using the XML tags provided in the Spring Batch namespace. Therefore, you need to include this namespace in your XML file as shown below.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch

   http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
   http://www.springframework.org/schema/bean
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

在以下部分中,我们将讨论Spring Batch命名空间中可用的各种标记、它们的属性和示例。

In the following sections, we will discuss the various tags, their attributes and examples, available in the Spring Batch namespace.

Job

此标记用于定义/配置SpringBatch的作业。它包含一组步骤,并且可以使用JobLauncher启动它。

This tag is used to define/configure the job of the SpringBatch. It contains a set of steps and it can be launched using the JobLauncher.

此标记具有如下所列的2个属性−

This tag has 2 attributes as listed below −

S.No

Attribute & Description

1

Id It is the Id of the job, it is mandatory to specify value to this attribute.

2

restartable This is the attribute which is used to specify whether the job is restartable or not. This attribute is optional.

以下是SpringBatch作业的XML配置。

Following is the XML configuration of the job of a SpringBatch.

<job id = "jobid" restartable = "false" >
   . . . . . . . .
   . . . . . . . .
   . . . . . . . . // Step definitions
</job>

Step

此标记用于定义/配置SpringBatch作业的步骤。它具有以下三个属性−

This tag is used to define/configure the steps of a SpringBatch job. It has the following three attributes −

S.No

Attribute & Description

1

Id It is the Id of the job, it is mandatory to specify value to this attribute.

2

next It is the shortcut to specify the next step.

3

parent It is used to specify the name of the parent bean from which the configuration should inherit.

以下内容为 SpringBatch 步骤的 XML 配置。

Following is the XML configuration of the step of a SpringBatch.

<job id = "jobid">
   <step id = "step1" next = "step2"/>
   <step id = "step2" next = "step3"/>
   <step id = "step3"/>
</job>

Chunk

此标签用来定义/配置 tasklet 的一部分。它有以下四个属性:

This tag is used to define/configure a chunk of a tasklet. It has the following four attributes −

S.No

Attribute & Description

1

reader It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemReader.

2

writer It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemWriter.

3

processor It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemProcessor.

4

commit-interval It is used to specify the number of items to be processed before committing the transaction.

以下内容为 SpringBatch 的部分内容 XML 配置。

Following is the XML configuration of the chunk of a SpringBatch.

<batch:step id = "step1">
   <batch:tasklet>
      <batch:chunk reader = "xmlItemReader"
         writer = "mysqlItemWriter" processor = "itemProcessor" commit-interval = "10">
      </batch:chunk>
   </batch:tasklet>
</batch:step>

JobRepository

JobRepository Bean 可用于通过关系数据库配置一个 JobRepository。这个 bean 与类型为 org.springframework.batch.core.repository.JobRepository 的类关联。

The JobRepository Bean is used to configure the JobRepository using a relational database. This bean is associated with the class of type org.springframework.batch.core.repository.JobRepository.

S.No

Attribute & Description

1

dataSource It is used to specify the bean name which defines the datasource.

2

transactionManager It is used specify the name of the bean which defines the transactionmanager.

3

databaseType It specifies the type of the relational database used in the job repository.

JobRepository 的配置范例如下。

Following is the example configuration of the JobRepository.

<bean id = "jobRepository"
   class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
   <property name = "dataSource" ref = "dataSource" />
   <property name = "transactionManager" ref="transactionManager" />
   <property name = "databaseType" value = "mysql" />
</bean>

JobLauncher

JobLauncher Bean 可用于配置 JobLauncher。它与类 org.springframework.batch.core.launch.support.SimpleJobLauncher 关联 (在我们的程序中)。这个 bean 有一个名为 jobrepository 的属性,用于指定定义 jobrepository 的 bean 的名称。

The JobLauncher bean is used to configure the JobLauncher. It is associated with the class org.springframework.batch.core.launch.support.SimpleJobLauncher (in our programs). This bean has one property named jobrepository, and it is used to specify the name of the bean which defines the jobrepository.

JobLauncher 的配置范例如下。

Following is the example configuration of the jobLauncher.

<bean id = "jobLauncher"
   class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
   <property name = "jobRepository" ref = "jobRepository" />
</bean>

TransactionManager

TransactionManager Bean 可用于通过关系数据库配置 TransactionManager。这个 bean 与类型为 org.springframework.transaction.platform.TransactionManager 的类关联。

The TransactionManager bean is used to configure the TransactionManager using a relational database. This bean is associated with the class of type org.springframework.transaction.platform.TransactionManager.

<bean id = "transactionManager"
   class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

DataSource

Datasource Bean 用来配置 Datasource 。这个 Bean 与类型为 org.springframework.jdbc.datasource.DriverManagerDataSource 的类关联。

The datasource bean is used to configure the Datasource. This bean is associated with the class of type org.springframework.jdbc.datasource.DriverManagerDataSource.

S.No

Attribute & Description

1

driverClassName This specifies the class name of the driver used to connect with the database.

2

url This specifies the URL of the database.

3

username This specifies the username to connect with the database.

4

password This specifies the password to connect with the database.

以下为 datasource 的示例配置

Following is the example configuration of the datasource.

<bean id = "dataSource"
   class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
   <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
   <property name = "username" value = "myuser" />
   <property name = "password" value = "password" />
</bean>

Spring Batch - Readers, Writers & Processors

Item Reader 从特定源读取数据到 Spring Batch 应用程序,而 Item Writer 将数据从 Spring Batch 应用程序写入到特定目标。

An Item Reader reads data into the spring batch application from a particular source, whereas an Item Writer writes data from Spring Batch application to a particular destination.

Item processor 是一个包含处理代码的类,该代码处理读入到 Spring Batch 中的数据。如果应用程序读取 n 条记录,则处理器中的代码将在每条记录上执行。

An Item processor is a class which contains the processing code which processes the data read in to the spring batch. If the application reads n records the code in the processor will be executed on each record.

chunktasklet 的子元素。它用于执行读取、写入和处理操作。我们可以使用该元素在步骤中配置读取器、写入器和处理器,如下所示。

A chunk is a child element of the tasklet. It is used to perform read, write, and processing operations. We can configure reader, writer, and processors using this element, within a step as shown below.

<batch:job id = "helloWorldJob">
   <batch:step id = "step1">
      <batch:tasklet>
         <batch:chunk reader = "cvsFileItemReader" writer = "xmlItemWriter"
            processor = "itemProcessor" commit-interval = "10">
         </batch:chunk>
      </batch:tasklet>
   </batch:step>
</batch:job>

Spring Batch 提供读取器和写入器以读取和写入各种文件系统/数据库的数据,例如 MongoDB、Neo4j、MySQL、XML、扁平文件、CSV 等。

Spring Batch provides readers and writers to read and write data form various file systems/databases such as MongoDB, Neo4j, MySQL, XML, flatfile, CSV, etc.

要将读取器包含在应用程序中,您需要为该读取器定义一个 bean,为 bean 中的所有必需属性提供值,并将该 bean 的 id 作为 reader 的属性值传递(对 writer 也一样)。

To include a reader in your application, you need to define a bean for that reader, provide values to all the required properties within the bean, and pass the id of such bean as a value to the attribute of the chunk element reader (same for writer).

ItemReader

它是读取数据的步骤(批量处理的步骤)的实体。ItemReader 每次读取一项。Spring Batch 提供了一个接口 ItemReader 。所有 readers 都实现了此接口。

It is the entity of a step (of a batch process) which reads data. An ItemReader reads one item a time. Spring Batch provides an Interface ItemReader. All the readers implement this interface.

以下是 Spring Batch 提供的用于从各种来源读取数据的一些预定义 ItemReader 类。

Following are some of the predefined ItemReader classes provided by Spring Batch to read from various sources.

Reader

Purpose

FlatFIleItemReader

To read data from flat files.

StaxEventItemReader

To read data from XML files.

StoredProcedureItemReader

To read data from the stored procedures of a database.

JDBCPagingItemReader

To read data from relational databases database.

MongoItemReader

To read data from MongoDB.

Neo4jItemReader

To read data from Neo4jItemReader.

我们需要通过创建 bean 来配置 ItemReaders 。下面是 StaxEventItemReader 的一个示例,它从 XML 文件中读取数据。

We need to configure the ItemReaders by creating the beans. Following is an example of StaxEventItemReader which reads data from an XML file.

<bean id = "mysqlItemWriter"
   class = "org.springframework.batch.item.xml.StaxEventItemWriter">
   <property name = "resource" value = "file:xml/outputs/userss.xml" />
   <property name = "marshaller" ref = "reportMarshaller" />
   <property name = "rootTagName" value = "Tutorial" />
</bean>

<bean id = "reportMarshaller"
   class = "org.springframework.oxm.jaxb.Jaxb2Marshaller">
   <property name = "classesToBeBound">
      <list>
         <value>Tutorial</value>
      </list>
   </property>
</bean>

如您所见,在配置时,我们需要指定所需读取器的相应类名,并且我们需要为所有必需属性提供值。

As observed, while configuring, we need to specify the respective class name of the required reader and we need to provide values to all the required properties.

ItemWriter

它是批量处理 step 的元素,用于写入数据。ItemWriter 每次写入一项。Spring Batch 提供了一个接口 ItemWriter 。所有写入器都实现了此接口。

It is the element of the step of a batch process which writes data. An ItemWriter writes one item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this interface.

以下是 Spring Batch 提供的用于从各种来源读取数据的一些预定义 ItemWriter 类。

Following are some of the predefined ItemWriter classes provided by Spring Batch to read from various sources.

Writer

Purpose

FlatFIleItemWriter

To write data into flat files.

StaxEventItemWriter

To write data into XML files.

StoredProcedureItemWriter

To write data into the stored procedures of a database.

JDBCPagingItemWriter

To write data into relational databases database.

MongoItemWriter

To write data into MongoDB.

Neo4jItemWriter

To write data into Neo4j.

同样,我们需要通过创建 Bean 来配置 ItemWriter。下面是一个将数据写入 MySQL 数据库的 JdbcCursorItemReader 的示例。

In same way, we need to configure the ItemWriters by creating the beans. Following is an example of JdbcCursorItemReader which writes data to an MySQL database.

<bean id = "dbItemReader"
   class = "org.springframework.batch.item.database.JdbcCursorItemReader" scope = "step">
   <property name = "dataSource" ref = "dataSource" />
   <property name = "sql" value = "select * from tutorialsdata" />
   <property name = "rowMapper">
      <bean class = "TutorialRowMapper" />
   </property>
</bean>

Item Processor

ItemProcessor − 使用ItemProcessor处理数据。当给定的项无效时,它返回 null ,否则它将处理给定的项并返回处理后的结果。接口 ItemProcessor<I,O> 表示处理器。

ItemProcessor − An ItemProcessor is used to process the data. When the given item is not valid it returns null, else it processes the given item and returns the processed result. The interface ItemProcessor<I,O> represents the processor.

Tasklet class − 当未给定 readerwriter 时,Tasklet 会充当 SpringBatch 的处理器。它只处理单一任务。

Tasklet class − When no reader and writer are given, a Tasklet acts as a processor for SpringBatch. It processes only single task.

我们可以通过实现包 org.springframework.batch.item.ItemProcessor 的接口 ItemProcessor 来定义一个自定义项处理器。此 ItemProcessor 类接受一个对象并处理数据,然后将处理后的数据作为另一个对象返回。

We can define a custom item processor by implementing the interface ItemProcessor of the package org.springframework.batch.item.ItemProcessor. This ItemProcessor class accepts an object and processes the data and returns the processed data as another object.

在批处理中,如果读取 "n" 记录或数据元素,则对于每条记录,它将读取数据、处理数据并将数据写入编写器。要处理数据,它依赖于传递的处理器。

In a batch process, if "n" records or data elements are read, then for each record, it will read the data, process it, and writes the data in the writer. To process the data, it relays on the processor passed.

例如,假设你已编写代码来加载特定 PDF 文档、创建新页面、以表格形式将数据项写入 PDF。如果你执行此应用程序,它会从 XML 文档中读取所有数据项,将它们存储在 MySQL 数据库中,并以单个页面形式将它们打印在给定的 PDF 文档中。

For example, let’s suppose you have written code to load a particular PDF document, create a new page, write the data item on to the PDF in a tabular format. If you execute this application, it reads all the data items from the XML document, stores them in the MySQL database, and prints them in the given PDF document in individual pages.

Example

以下是示例 ItemProcessor 类。

Following is a sample ItemProcessor class.

import org.springframework.batch.item.ItemProcessor;

public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);
      return item;
   }
}

Spring Batch - Basic Application

本章向你展示了基本的 Spring Batch 应用程序。它会简单地执行一个 tasklet 来显示一条消息。

This chapter shows you the basic Spring Batch application. It will simply execute a tasklet to displays a message.

我们的 Spring Batch 应用程序包含以下文件 -

Our Spring Batch application contains the following files −

  1. Configuration file − This is an XML file where we define the Job and the steps of the job. (If the application involves readers and writers too, then the configuration of readers and writers is also included in this file.)

  2. Context.xml − In this file, we will define the beans like job repository, job launcher and transaction manager.

  3. Tasklet class − In this class, we will write the processing code job (In this case, it displays a simple message)

  4. Launcher class − in this class, we will launch the Batch Application by running the Job launcher.

jobConfig.xml

以下为我们示例 Spring Batch 应用程序的配置文件

Following is the configuration file of our sample Spring Batch application.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch
      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
   <import resource="context.xml" />
   <!-- Defining a bean -->
   <bean id = "tasklet" class = "a_sample.MyTasklet" />
   <!-- Defining a job-->
   <batch:job id = "helloWorldJob">
      <!-- Defining a Step -->
      <batch:step id = "step1">
         <tasklet ref = "tasklet"/>
      </batch:step>
   </batch:job>
</beans>

Context.xml

以下为我们 Spring Batch 应用程序的 context.xml

Following is the context.xml of our Spring Batch application.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

   <bean id = "jobRepository"
      class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
      <property name = "transactionManager" ref = "transactionManager" />
   </bean>

   <bean id = "transactionManager"
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository" />
   </bean>
</beans>

Tasklet.java

以下是显示简单消息的 Tasklet 类

Following is the Tasklet class which displays a simple message.

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class MyTasklet implements Tasklet {

   @Override
   public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
      System.out.println("Hello This is a sample example of spring batch");
      return RepeatStatus.FINISHED;
   }
}

App.java

以下为启动批处理的代码

Following is the code which launces the batch process.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
   public static void main(String[] args)throws Exception {

      // System.out.println("hello");
      String[] springConfig  =  {"a_sample/job_hello_world.xml"};

      // Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

      // Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

      // Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

      // Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

在执行时,以上 SpringBatch 程序将生成以下输出 −

On executing, the above SpringBatch program will produce the following output −

Apr 24, 2017 4:40:54 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2ef1e4fa: startup date [Mon Apr 24 16:40:54 IST 2017]; root of context hierarchy
Apr 24, 2017 4:40:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions
Apr 24, 2017 4:40:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions
Apr 24, 2017 4:40:54 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
Apr 24, 2017 4:40:55 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Apr 24, 2017 4:40:55 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
Apr 24, 2017 4:40:55 PM org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1]
Hello This is a sample example of spring batch
Apr 24, 2017 4:40:55 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED

Spring Batch - XML to MySQL

在本章中,我们将创建一个使用 XML 读取器和 MySQL 写入器的 Spring Batch 应用程序

In this chapter, we will create a Spring Batch application which uses an XML Reader and a MySQL Writer.

Reader − 我们在应用程序中使用的读取器是 StaxEventItemReader 以从 XML 文档读取数据

Reader − The reader we are using in the application is StaxEventItemReader to read data from XML documents.

以下为我们在本应用程序中使用的输入 XML 文档。此文档包含详细记录指定教程 ID、教程作者、教程标题、提交日期、教程图标和教程描述等详细信息的数据记录

Following is the input XML document we are using in this application. This document holds data records which specify details like tutorial id, tutorial author, tutorial title, submission date, tutorial icon, and tutorial description.

<?xml version="1.0" encoding="UTF-8"?>
<tutorials>
   <tutorial>
      <tutorial_id>1001</tutorial_id>
      <tutorial_author>Sanjay</tutorial_author>
      <tutorial_title>Learn Java</tutorial_title>
      <submission_date>06-05-2007</submission_date>
      <tutorial_icon>https://www.tutorialspoint.com/java/images/java-minilogo.jpg</tutorial_icon>
      <tutorial_description>Java is a high-level programming language originally
         developed by Sun Microsystems and released in 1995.
         Java runs on a variety of platforms.
         This tutorial gives a complete understanding of Java.');</tutorial_description>
   </tutorial>

   <tutorial>
      <tutorial_id>1002</tutorial_id>
      <tutorial_author>Abdul S</tutorial_author>
      <tutorial_title>Learn MySQL</tutorial_title>
      <submission_date>19-04-2007</submission_date>
      <tutorial_icon>https://www.tutorialspoint.com/mysql/images/mysql-minilogo.jpg</tutorial_icon>
      <tutorial_description>MySQL is the most popular
         Open Source Relational SQL database management system.
         MySQL is one of the best RDBMS being used for developing web-based software applications.
         This tutorial will give you quick start with MySQL
         and make you comfortable with MySQL programming.</tutorial_description>
   </tutorial>

   <tutorial>
      <tutorial_id>1003</tutorial_id>
      <tutorial_author>Krishna Kasyap</tutorial_author>
      <tutorial_title>Learn JavaFX</tutorial_title>
      <submission_date>06-07-2017</submission_date>
      <tutorial_icon>https://www.tutorialspoint.com/javafx/images/javafx-minilogo.jpg</tutorial_icon>
      <tutorial_description>JavaFX is a Java library used to build Rich Internet Applications.
         The applications developed using JavaFX can run on various devices
         such as Desktop Computers, Mobile Phones, TVs, Tablets, etc.
         This tutorial, discusses all the necessary elements of JavaFX that are required
         to develop effective Rich Internet Applications</tutorial_description>
   </tutorial>
</tutorials>

Writer − 我们在应用程序中使用的 writerJdbcBatchItemWriter 以将数据写入 MySQL 数据库。假设我们在名为 "details" 的数据库中创建了 MySQL 中的一个表

Writer − The writer we are using in the application is JdbcBatchItemWriter to write the data to MySQL database. Assume we have created a table in MySQL inside a database called "details".

CREATE TABLE details.TUTORIALS(
   tutorial_id int(10) NOT NULL,
   tutorial_author VARCHAR(20),
   tutorial_title VARCHAR(50),
   submission_date VARCHAR(20),
   tutorial_icon VARCHAR(200),
   tutorial_description VARCHAR(1000)
);

Processor − 我们在应用程序中使用的处理器是一个自定义处理器,它在 PDF 文档上输出每条记录的数据。

Processor − The processor we are using in the application is a custom processor which writes the data of each record on the PDF document.

在批处理中,如果读取 "n" 记录或数据元素,那么对于每条记录,它将读取数据,处理它,然后在写入器中写入数据。要处理数据,它会中继到已传递的处理器。在本例中,在自定义的处理器类中,我们编写的代码能够加载特定的 PDF 文档,创建一个新页面,将数据项以表格格式写入 PDF。

In batch process, if "n" records or data elements were read, then for each record, it will read the data, process it, and write the data in the Writer. To process the data, it relays on the processor passed. In this case, in the custom processor class, we have written code to load a particular PDF document, create a new page, write the data item onto the PDF in a tabular format.

最后,如果你执行此应用程序,它将从 XML 文档读取所有数据项,将它们存储在 MySQL 数据库中,并以单独的页面在给定的 PDF 文档中打印它们。

Finally, if you execute this application, it reads all the data items from the XML document, stores them in the MySQL database, and prints them in the given PDF document in individual pages.

jobConfig.xml

以下为我们示例 Spring Batch 应用程序的配置文件。在此文件中,我们将定义作业和步骤。除了这些之外,我们还将定义 ItemReader、ItemProcessor 和 ItemWriter 的 Bean。(在这里,我们将它们分别与其类关联起来,并传递必需的属性值来配置它们。)

Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and pass the values for the required properties to configure them.)

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util = "http://www.springframework.org/schema/util"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch

      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd ">

   <import resource = "../jobs/context.xml" />

   <bean id = "itemProcessor" class = "CustomItemProcessor" />
   <batch:job id = "helloWorldJob">
      <batch:step id = "step1">
         <batch:tasklet>
            <batch:chunk reader = "xmlItemReader" writer = "mysqlItemWriter" processor = "itemProcessor">
            </batch:chunk>
         </batch:tasklet>
      </batch:step>
   </batch:job>

   <bean id = "xmlItemReader"
      class = "org.springframework.batch.item.xml.StaxEventItemReader">
      <property name = "fragmentRootElementName" value = "tutorial" />
      <property name = "resource" value = "classpath:resources/tutorial.xml" />
      <property name = "unmarshaller" ref = "customUnMarshaller" />
   </bean>

   <bean id = "customUnMarshaller" class = "org.springframework.oxm.xstream.XStreamMarshaller">
      <property name = "aliases">
         <util:map id = "aliases">
            <entry key = "tutorial" value = "Tutorial" />
         </util:map>
      </property>
   </bean>
   <bean id = "mysqlItemWriter" class = "org.springframework.batch.item.database.JdbcBatchItemWriter">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "sql">
         <value>
            <![CDATA[insert into details.tutorials (tutorial_id, tutorial_author, tutorial_title,
               submission_date, tutorial_icon, tutorial_description)
               values (:tutorial_id, :tutorial_author, :tutorial_title, :submission_date,
               :tutorial_icon, :tutorial_description);]]>
         </value>
      </property>

      <property name = "itemSqlParameterSourceProvider">
         <bean class = "org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
      </property>
   </bean>
</beans>

Context.xml

以下是我们 Spring Batch 应用程序的 context.xml 。在该文件中,我们将定义 Bean,如 Job 存储库、Job 启动器和事务管理器。

Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/jdbc
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

   <!-- stored job-meta in database -->
   <bean id = "jobRepository"
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "transactionManager" ref = "transactionManager" />
      <property name = "databaseType" value = "mysql" />
   </bean>

   <bean id = "transactionManager"
   class = "org.springframework.batch.support.transaction.ResourcelessTransactionMana ger" />
   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository" />
   </bean>

   <!-- connect to MySQL database -->
   <bean id = "dataSource"
      class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
      <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
      <property name = "username" value = "myuser" />
      <property name = "password" value = "password" />
   </bean>

   <!-- create job-meta tables automatically -->
   <jdbc:initialize-database data-source = "dataSource">
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql"/>
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql"/>
   </jdbc:initialize-database>
</beans>

CustomItemProcessor.java

以下为 processor 类。在此类中,我们编写应用程序中的处理代码。在这里,我们加载 PDF 文档,创建一个新页面,创建一个表,并为表中的每条记录插入以下的值:教程 ID、教程名称、作者、提交日期。

Following is the processor class. In this class, we write the code of processing in the application. Here, we are loading a PDF document, creating a new page, creating a table, and inserting the following values for each record: tutorial id, tutorial name, author, date of submission in the table.

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.batch.item.ItemProcessor;

public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   public static void drawTable(PDPage page, PDPageContentStream contentStream,
      float y, float margin, String[][] content) throws IOException {
      final int rows = content.length;
      final int cols = content[0].length;
      final float rowHeight = 50;
      final float tableWidth = page.getMediaBox().getWidth()-(2*margin);
      final float tableHeight = rowHeight * rows;
      final float colWidth = tableWidth/(float)cols;
      final float cellMargin=5f;

      // draw the rows
      float nexty = y ;
      for (int i = 0; i <= rows; i++) {
         contentStream.drawLine(margin,nexty,margin+tableWidth,nexty);
         nexty-= rowHeight;
      }

      //draw the columns
      float nextx = margin;
      for (int i = 0; i <= cols; i++) {
         contentStream.drawLine(nextx,y,nextx,y-tableHeight);
         nextx += colWidth;
      }

      // now add the text
      contentStream.setFont(PDType1Font.HELVETICA_BOLD,12);

      float textx = margin+cellMargin;
      float texty = y-15;
      for(int i = 0; i < content.length; i++){
         for(int j = 0 ; j < content[i].length; j++){
            String text = content[i][j];
            contentStream.beginText();
            contentStream.moveTextPositionByAmount(textx,texty);
            contentStream.drawString(text);
            contentStream.endText();
            textx += colWidth;
         }

         texty-=rowHeight;
         textx = margin+cellMargin;
      }
   }

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);

      // Creating PDF document object
      PDDocument doc = PDDocument.load(new File("C:/Examples/test.pdf"));

      // Creating a blank page
      PDPage page = new PDPage();
      doc.addPage( page );
      PDPageContentStream contentStream =  new PDPageContentStream(doc, page);

      String[][] content = {{"Id",""+item.getTutorial_id()},
      {"Title", item.getTutorial_title()},
      {"Authour", item.getTutorial_author()},
      {"Submission Date", item.getSubmission_date()}} ;
      drawTable(page, contentStream, 700, 100, content);

      contentStream.close();
      doc.save("C:/Examples/test.pdf" );
      System.out.println("Hello");
      return item;
   }
}

TutorialFieldSetMapper.java

以下是将数据设置为 Tutorial 类的 ReportFieldSetMapper 类。

Following is the ReportFieldSetMapper class which sets the data to the Tutorial class.

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class TutorialFieldSetMapper implements FieldSetMapper<Tutorial> {

   @Override
   public Tutorial mapFieldSet(FieldSet fieldSet) throws BindException {
      // instantiating the Tutorial class
      Tutorial tutorial = new Tutorial();

      // Setting the fields from XML
      tutorial.setTutorial_id(fieldSet.readInt(0));
      tutorial.setTutorial_title(fieldSet.readString(1));
      tutorial.setTutorial_author(fieldSet.readString(2));
      tutorial.setTutorial_icon(fieldSet.readString(3));
      tutorial.setTutorial_description(fieldSet.readString(4));
      return tutorial;
   }
}

Tutorial.java

以下是 Tutorial 类。它是一个具有 settergetter 方法的简单类。

Following is the Tutorial class. It is a simple class with setter and getter methods.

public class Tutorial {
   private int tutorial_id;
   private String tutorial_author;
   private String tutorial_title;
   private String submission_date;
   private String tutorial_icon;
   private String tutorial_description;

   @Override
   public String toString() {
      return " [id=" + tutorial_id + ", author=" + tutorial_author
         + ", title=" + tutorial_title + ", date=" + submission_date + ", icon ="
         +tutorial_icon +", description = "+tutorial_description+"]";
   }

   public int getTutorial_id() {
      return tutorial_id;
   }

   public void setTutorial_id(int tutorial_id) {
      this.tutorial_id = tutorial_id;
   }

   public String getTutorial_author() {
      return tutorial_author;
   }

   public void setTutorial_author(String tutorial_author) {
      this.tutorial_author = tutorial_author;
   }

   public String getTutorial_title() {
      return tutorial_title;
   }

   public void setTutorial_title(String tutorial_title) {
      this.tutorial_title = tutorial_title;
   }

   public String getSubmission_date() {
      return submission_date;
   }

   public void setSubmission_date(String submission_date) {
      this.submission_date = submission_date;
   }

   public String getTutorial_icon() {
      return tutorial_icon;
   }

   public void setTutorial_icon(String tutorial_icon) {
      this.tutorial_icon = tutorial_icon;
   }

   public String getTutorial_description() {
      return tutorial_description;
   }

   public void setTutorial_description(String tutorial_description) {
      this.tutorial_description = tutorial_description;
   }
}

App.java

以下是启动批处理的代码。在该类中,我们将通过运行 JobLauncher 来启动 Batch 应用程序。

Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher.

public class App {
   public static void main(String[] args) throws Exception {
      String[] springConfig  = {    "jobs/job_hello_world.xml" };

      // Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

      // Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

      // Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

      // Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

执行此应用程序后,它将生成以下输出。

On executing this application, it will produce the following output.

May 05, 2017 4:39:22 PM org.springframework.context.support.ClassPathXmlApplicationContext
prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@306a30c7:
startup date [Fri May 05 16:39:22 IST 2017]; root of context hierarchy
May 05, 2017 4:39:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
May 05, 2017 4:39:32 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing... [id=1001, author=Sanjay, title=Learn Java, date=06-05-2007,
icon =https://www.tutorialspoint.com/java/images/java-mini-logo.jpg,
description = Java is a high-level programming language originally developed by Sun Microsystems
and released in 1995. Java runs on a variety of platforms.
This tutorial gives a complete understanding of Java.');]
Hello
Processing.. [id=1002, author=Abdul S, title=Learn MySQL, date=19-04-2007,
icon =https://www.tutorialspoint.com/mysql/images/mysql-mini-logo.jpg,
description = MySQL is the most popular Open Source Relational SQL database management system.
MySQL is one of the best RDBMS being used for developing web-based software applications.
This tutorial will give you quick start with MySQL and make you comfortable with MySQL programming.]
Hello
Processing... [id=1003, author=Krishna Kasyap, title=Learn JavaFX, date=06-072017,
icon =https://www.tutorialspoint.com/javafx/images/javafx-mini-logo.jpg,
description = JavaFX is a Java library used to build Rich Internet Applications.
The applications developed using JavaFX can run on various devices
such as Desktop Computers, Mobile Phones, TVs, Tablets, etc.
This tutorial, discusses all the necessary elements of JavaFX
that are required to develop effective Rich Internet Applications]
Hello
May 05, 2017 4:39:36 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}]
and the following status: [COMPLETED]
Exit Status : COMPLETED

如果您在数据库中验证 details.tutorial 表,它将显示以下输出 −

If you verify the details.tutorial table in the database, it will show you the following output −

tutorial _id

tutorial _author

tutorial _title

submission _date

tutorial _icon

tutorial _description

1001

Sanjay

Learn Java

06-05-2007

https://www.tutorials point.com /java/images/ java-mini-logo.jpg

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms. This tutorial gives a complete understanding of Java.

1002

Abdul S

Learn MySQL

19-04-2007

https://www. tutorialspoint.com /mysql/images /mysql-minilogo.jpg

MySQL is the most popular Open Source Relational SQL database management system. MySQL is one of the best RDBMS being used for developing web-based software applications. This tutorial will give you quick start with MySQL and make you comfortable with MySQL programming.

1003

Learn JavaFX

Krishna Kasyap

06-07-2017

https://www. tutorialspoint.com /javafx/images/ javafx-minilogo.jpg

MySQL is the most popular Open Source Relational SQL database management system. MySQL is one of the best RDBMS being used for developing web-based software applications. This tutorial will give you quick start with MySQL and make you comfortable with MySQL programming.

这将生成一个 PDF,其中每一页都有记录,如下所示。

This will generate a PDF with the records on each page as shown below.

page thumbnails

Spring Batch - CSV to XML

在本章,我们将创建一个简单的 Spring Batch 应用程序,此应用程序使用 CSV Reader 和 XML Writer。

In this chapter, we will create a simple Spring Batch application which uses a CSV Reader and an XML Writer.

Reader − 我们在应用程序中使用的 readerFlatFileItemReader ,用于从 CSV 文件中读取数据。

Reader − The reader we are using in the application is FlatFileItemReader to read data from the CSV files.

以下是我们在此应用程序中使用的输入 CSV 文件。此文档保存指定详情记录的数据,例如教程 ID、教程作者、教程标题、提交日期、教程图标和教程描述。

Following is the input CSV file we are using in this application. This document holds data records which specify details like tutorial id, tutorial author, tutorial title, submission date, tutorial icon and tutorial description.

1001, "Sanjay", "Learn Java", 06/05/2007
1002, "Abdul S", "Learn MySQL", 19/04/2007
1003, "Krishna Kasyap", "Learn JavaFX", 06/07/2017

Writer − 我们在应用程序中使用的 Writer 是 StaxEventItemWriter ,用于将数据写入 XML 文件。

Writer − The Writer we are using in the application is StaxEventItemWriter to write the data to XML file.

Processor − 我们在应用程序中使用的 Processor 是一个自定义处理器,它只是打印从 CSV 文件中读取的记录。

Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file.

jobConfig.xml

以下是我们样例 Spring Batch 应用程序的配置文件。在此文件中,我们将定义任务和步骤。此外,我们还定义 ItemReader、ItemProcessor 和 ItemWriter 的 bean。(在此,我们将其与各自的类相关联,并传递所需属性的值以对其进行配置。)

Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass the values for the required properties to configure them.)

<beans xmlns = " http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch
      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

   <import resource = "../jobs/context.xml" />

   <bean id = "report" class = "Report" scope = "prototype" />
   <bean id = "itemProcessor" class = "CustomItemProcessor" />

   <batch:job id = "helloWorldJob">

      <batch:step id = "step1">

         <batch:tasklet>
            <batch:chunk reader = "cvsFileItemReader" writer = "xmlItemWriter"
               processor = "itemProcessor" commit-interval = "10">
            </batch:chunk>
         </batch:tasklet>
      </batch:step>
   </batch:job>

   <bean id = "cvsFileItemReader"
      class = "org.springframework.batch.item.file.FlatFileItemReader">
      <property name = "resource" value = "classpath:resources/report.csv" />
      <property name = "lineMapper">
         <bean
            class = "org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name = "lineTokenizer">
               <bean
                  class = "org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                  <property name = "names" value = "tutorial_id,
                     tutorial_author, Tutorial_title, submission_date" />
               </bean>
            </property>

            <property name = "fieldSetMapper">
               <bean class = "ReportFieldSetMapper" />
            </property>
         </bean>
      </property>
   </bean>

   <bean id = "xmlItemWriter"
      class = "org.springframework.batch.item.xml.StaxEventItemWriter">
      <property name = "resource" value = "file:xml/outputs/tutorials.xml" />
      <property name = "marshaller" ref = "reportMarshaller" />
      <property name = "rootTagName" value = "tutorials" />
   </bean>

   <bean id = "reportMarshaller"
      class = "org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name = "classesToBeBound">
         <list>
            <value>Tutorial</value>
         </list>
      </property>
   </bean>
</beans>

Context.xml

以下是我们 Spring Batch 应用程序的 context.xml 。在该文件中,我们将定义 Bean,如 Job 存储库、Job 启动器和事务管理器。

Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/jdbc
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
   <!-- stored job-meta in database -->
   <bean id = "jobRepository"
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "transactionManager" ref = "transactionManager" />
      <property name = "databaseType" value = "mysql" />
   </bean>

   <bean id = "transactionManager"
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository" />
   </bean>

   <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
      <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
      <property name = "username" value = "myuser" />
      <property name = "password" value = "password" />
   </bean>

   <!-- create job-meta tables automatically -->
   <jdbc:initialize-database data-source = "dataSource">
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql" />
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql" />
   </jdbc:initialize-database>
</beans>

CustomItemProcessor.java

以下是 Processor 类。在该类中,我们编写应用程序的处理代码。此处,我们打印每个记录的内容。

Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record.

import org.springframework.batch.item.ItemProcessor;

public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);
      return item;
   }
}

TutorialFieldSetMapper.java

以下是 TutorialFieldSetMapper 类,此类将数据设置为 Tutorial 类。

Following is the TutorialFieldSetMapper class which sets the data to the Tutorial class.

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class TutorialFieldSetMapper implements FieldSetMapper<Tutorial> {

   @Override
   public Tutorial mapFieldSet(FieldSet fieldSet) throws BindException {

      //Instantiating the report object
      Tutorial tutorial = new Tutorial();

      //Setting the fields
      tutorial.setTutorial_id(fieldSet.readInt(0));
      tutorial.setTutorial_author(fieldSet.readString(1));
      tutorial.setTutorial_title(fieldSet.readString(2));
      tutorial.setSubmission_date(fieldSet.readString(3));

      return tutorial;
   }
}

Tutorial.java class

以下是 Tutorial 类。它是一个简单的 Java 类,具有 settergetter 方法。在该类中,我们使用注解来将该类的这些方法与 XML 文件的这些标记相关联。

Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file.

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "tutorial")
public class Tutorial {
   private int tutorial_id;
   private String tutorial_author;
   private String tutorial_title;
   private String submission_date;

   @XmlAttribute(name = "tutorial_id")
   public int getTutorial_id() {
      return tutorial_id;
   }

   public void setTutorial_id(int tutorial_id) {
      this.tutorial_id = tutorial_id;
   }

   @XmlElement(name = "tutorial_author")
   public String getTutorial_author() {
      return tutorial_author;
   }
   public void setTutorial_author(String tutorial_author) {
      this.tutorial_author = tutorial_author;
   }

   @XmlElement(name = "tutorial_title")
   public String getTutorial_title() {
      return tutorial_title;
   }

   public void setTutorial_title(String tutorial_title) {
      this.tutorial_title = tutorial_title;
   }

   @XmlElement(name = "submission_date")
   public String getSubmission_date() {
      return submission_date;
   }

   public void setSubmission_date(String submission_date) {
      this.submission_date = submission_date;
   }

   @Override
   public String toString() {
      return "  [Tutorial id=" + tutorial_id + ",
         Tutorial Author=" + tutorial_author  + ",
         Tutorial Title=" + tutorial_title + ",
         Submission Date=" + submission_date + "]";
   }
}

App.java

以下是启动批处理的代码。在此类中,我们将通过运行 JobLauncher 来启动批处理应用程序。

Following is the code which launches the batch process. In this class, we will launch the batch application by running the JobLauncher.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
   public static void main(String[] args) throws Exception {

      String[] springConfig  =  { "jobs/job_hello_world.xml" };

      // Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

      // Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

      // Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

      // Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

执行此应用程序后,它将生成以下输出。

On executing this application, it will produce the following output.

May 08, 2017 10:10:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@3d646c37: startup date
[Mon May 08 10:10:12 IST 2017]; root of context hierarchy
May 08, 2017 10:10:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
May 08, 2017 10:10:15 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing step: [step1]
Processing...  [Tutorial id=1001, Tutorial Author=Sanjay,
Tutorial Title=Learn Java, Submission Date=06/05/2007]
Processing...  [Tutorial id=1002, Tutorial Author=Abdul S,
Tutorial Title=Learn MySQL, Submission Date=19/04/2007]
Processing...  [Tutorial id=1003, Tutorial Author=Krishna Kasyap,
Tutorial Title=Learn JavaFX, Submission Date=06/07/2017]
May 08, 2017 10:10:21 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters:
[{}] and the following status: [COMPLETED]
Exit Status : COMPLETED

这将生成包含以下内容的 XML 文件。

This will generate an XML file with the following contents.

<?xml version = "1.0" encoding = "UTF-8"?>
<tutorials>
   <tutorial tutorial_id = "1001">
      <submission_date>06/05/2007</submission_date>
      <tutorial_author>Sanjay</tutorial_author>
      <tutorial_title>Learn Java</tutorial_title>
   </tutorial>

   <tutorial tutorial_id = "1002">
      <submission_date>19/04/2007</submission_date>
      <tutorial_author>Abdul S</tutorial_author>
      <tutorial_title>Learn MySQL</tutorial_title>
   </tutorial>

   <tutorial tutorial_id = "1003">
      <submission_date>06/07/2017</submission_date>
      <tutorial_author>Krishna Kasyap</tutorial_author>
      <tutorial_title>Learn JavaFX</tutorial_title>
   </tutorial>
</tutorials>

Spring Batch - MySQL to XML

在本章中,我们将创建一个 Spring Batch 应用程序,它使用 MySQL reader 和 XML Writer。

In this chapter, we will create a Spring Batch application which uses a MySQL reader and an XML Writer.

Reader − 应用程序中使用的 reader 是 JdbcCursorItemReader ,用来从 MySQL 数据库中读取数据。

Reader − The reader we are using in the application is JdbcCursorItemReader to read data from MySQL database.

假设我们在 MySQL 数据库中创建了一个表,如下所示 −

Assume we have created a table in the MySQL database as shown below −

CREATE TABLE details.xml_mysql(
   person_id int(10) NOT NULL,
   sales VARCHAR(20),
   qty int(3),
   staffName VARCHAR(20),
   date VARCHAR(20)
);

假设我们在其中插入了以下记录。

Assume we have inserted the following records in to it.

mysql> select * from tutorialsdata;
+-------------+-----------------+----------------+-----------------+
| tutorial_id | tutorial_author | tutorial_title | submission_date |
+-------------+-----------------+----------------+-----------------+
|         101 | Sanjay          | Learn Java     | 06-05-2007      |
|         102 | Abdul S         | Learn MySQL    | 19-04-2007      |
|         103 | Krishna Kasyap  | Learn JavaFX   | 06-07-2017      |
+-------------+-----------------+----------------+-----------------+
3 rows in set (0.00 sec)

Writer − 应用程序中使用的 Writer 是 StaxEventItemWriter ,用来将数据写入 XML 文件。

Writer − The Writer we are using in the application is StaxEventItemWriter to write the data to the XML file.

Processor − 我们在应用程序中使用的 Processor 是一个自定义处理器,它只是打印从 CSV 文件中读取的记录。

Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file.

jobConfig.xml

以下是样例 Spring Batch 应用程序的配置文件。在该文件中,我们将定义 Job 和 Step。此外,我们还定义了 ItemReader、ItemProcessor 和 ItemWriter 的 bean。(在这里,我们将它们与其各自的类合并,并传递所需的属性值来配置它们。)

Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with their respective classes and pass the values for the required properties to configure them.)

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util = "http://www.springframework.org/schema/util"
   xsi:schemaLocation = " http://www.springframework.org/schema/batch
      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

   <import resource = "../jobs/context.xml" />

   <bean id = "report" class = "Report" scope = "prototype" />
   <bean id = "itemProcessor" class = "CustomItemProcessor" />

   <batch:job id = "helloWorldJob">
      <batch:step id = "step1">
         <batch:tasklet>
            <batch:chunk reader = "dbItemReader"
               writer = "mysqlItemWriter" processor = "itemProcessor" commit-interval = "10">
            </batch:chunk>
         </batch:tasklet>
      </batch:step>
   </batch:job>

   <bean id = "dbItemReader"
      class = "org.springframework.batch.item.database.JdbcCursorItemReader" scope = "step">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "sql" value = "select * from tutorials_data" />
      <property name = "rowMapper">
         <bean class = "TutorialRowMapper" />
      </property>
   </bean>
   <bean id = "mysqlItemWriter"
      class = "org.springframework.batch.item.xml.StaxEventItemWriter">
      <property name = "resource" value = "file:xml/outputs/tutorials.xml" />
      <property name = "marshaller" ref = "reportMarshaller" />
      <property name = "rootTagName" value = "Tutorial" />
   </bean>

   <bean id = "reportMarshaller" class = "org.springframework.oxm.jaxb.Jaxb2Marshaller">
      <property name = "classesToBeBound">
         <list>
            <value>Tutorial</value>
         </list>
      </property>
   </bean>
</beans>

Context.xml

以下是我们 Spring Batch 应用程序的 context.xml 。在该文件中,我们将定义 Bean,如 Job 存储库、Job 启动器和事务管理器。

Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.

<beans xmlns = " http://www.springframework.org/schema/beans"
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/jdbc
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">

   <!-- stored job-meta in database -->
   <bean id = "jobRepository"
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "transactionManager" ref = "transactionManager" />
      <property name = "databaseType" value = "mysql" />
   </bean>

   <bean id = "transactionManager"
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionMana ger" />
   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository" />
   </bean>

   <!-- connect to MySQL database -->
   <bean id = "dataSource"
      class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
      <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
      <property name = "username" value = "myuser" />
      <property name = "password" value = "password" />
   </bean>

   <!-- create job-meta tables automatically -->
   <jdbc:initialize-database data-source = "dataSource">
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql" />
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql" />
   </jdbc:initialize-database>
</beans>

CustomItemProcessor.java

以下是 Processor 类。在该类中,我们编写应用程序的处理代码。此处,我们打印每个记录的内容。

Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record.

import org.springframework.batch.item.ItemProcessor;

public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);
      return item;
   }
}

TutorialRowMapper.java

以下是 TutorialRowMapper 类,它将数据设置为 Tutorial 类。

Following is the TutorialRowMapper class which sets the data to the Tutorial class.

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class TutorialRowMapper implements RowMapper<Tutorial> {

   @Override
   public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {

      Tutorial tutorial = new Tutorial();
      tutorial.setTutorial_id(rs.getInt("tutorial_id"));
      tutorial.setTutorial_author(rs.getString("tutorial_author"));
      tutorial.setTutorial_title(rs.getString("tutorial_title"));
      tutorial.setSubmission_date(rs.getString("submission_date"));
      return tutorial;
   }
}

Tutorial.java

以下是 Tutorial 类。它是一个简单的 Java 类,具有 settergetter 方法。在该类中,我们使用注解来将该类的这些方法与 XML 文件的这些标记相关联。

Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file.

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "details")
public class Tutorial {

   int tutorial_id;
   String tutorial_author;
   String submission_date;

   @XmlAttribute(name = "tutorial_id")
   public int getTutorial_id() {
      return tutorial_id;
   }

   public void setTutorial_id(int tutorial_id) {
      this.tutorial_id = tutorial_id;
   }

   @XmlElement(name = "tutorial_author")
   public String getTutorial_author() {
      return tutorial_author;
   }

   public void setTutorial_author(String tutorial_author) {
      this.tutorial_author = tutorial_author;
   }

   @XmlElement(name = "tutorial_title")
   public String getTutorial_title() {
      return tutorial_title;
   }

   public void setTutorial_title(String tutorial_title) {
      this.tutorial_title = tutorial_title;
   }

   @XmlElement(name = "submission_date")
   public String getSubmission_date() {
      return submission_date;
   }

   public void setSubmission_date(String submission_date) {
      this.submission_date = submission_date;
   }

   public String toString() {
      return " [Tutorial Id=" + tutorial_id + ",
      Tutorial Author =" + tutorial_author  + ",
      Tutorial Title =" + tutorial_title + ",
      Submission Date =" + submission_date + "]";
   }
}

App.java

以下是启动批处理的代码。在该类中,我们将通过运行 JobLauncher 来启动 Batch 应用程序。

Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
   public static void main(String[] args) throws Exception {

      String[] springConfig  =  { "jobs/job_hello_world.xml" };

      // Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

      // Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

      // Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

      // Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

执行此应用程序后,它将生成以下输出。

On executing this application, it will produce the following output.

May 08, 2017 11:32:06 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3d646c37:
startup date [Mon May 08 11:32:06 IST 2017]; root of context hierarchy
May 08, 2017 11:32:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [jobs/job_hello_world.xml]
May 08, 2017 11:32:07 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
May 08, 2017 11:32:14 AM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing... [Tutorial Id=101, Tutorial Author=Sanjay,
Tutorial Title=Learn Java, Submission Date=06-05-2007]
Processing... [Tutorial Id=102, Tutorial Author=Abdul S,
Tutorial Title=Learn MySQL, Submission Date=19-04-2007]
Processing... [Tutorial Id=103, Tutorial Author=Krishna Kasyap,
Tutorial Title=Learn JavaFX, Submission Date=06-07-2017]
May 08, 2017 11:32:14 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters:
[{}] and the following status: [COMPLETED]
Exit Status : COMPLETED

这将生成包含以下内容的 XML 文件。

This will generate an XML file with the following contents.

<?xml version = "1.0" encoding = "UTF-8"?>
<Tutorial>
   <details tutorial_id = "101">
      <submission_date>06-05-2007</submission_date>
      <tutorial_author>Sanjay</tutorial_author>
      <tutorial_title>Learn Java</tutorial_title>
   </details>

   <details tutorial_id = "102">
      <submission_date>19-04-2007</submission_date>
      <tutorial_author>Abdul S</tutorial_author>
      <tutorial_title>Learn MySQL</tutorial_title>
   </details>

   <details tutorial_id = "103">
      <submission_date>06-07-2017</submission_date>
      <tutorial_author>Krishna Kasyap</tutorial_author>
      <tutorial_title>Learn JavaFX</tutorial_title>
   </details>
</Tutorial>

Spring Batch - MySQL to Flat File

在本章中,我们将创建一个 Spring Batch 应用程序,该应用程序使用 MySQL Reader 和 Flatfile Writer(.txt)。

In this chapter, we will create a Spring Batch application which uses an MySQL Reader and a Flatfile Writer (.txt ).

Reader − 我们在应用程序中使用的 Reader 是 JdbcCursorItemReader ,用于从 MySQL 数据库中读取数据。

Reader − The Reader we are using in the application is JdbcCursorItemReader to read data from MySQL database.

假设我们在 MySQL 数据库中创建了一个表格,如下所示。

Assume we have created a table in the MySQL database as shown below.

CREATE TABLE details.xml_mysql(
   person_id int(10) NOT NULL,
   sales VARCHAR(20),
   qty int(3),
   staffName VARCHAR(20),
   date VARCHAR(20)
);

假设我们已向其中插入了以下记录。

Assume we have inserted the following records into it.

mysql> select * from tutorialsdata;
+-------------+-----------------+----------------+-----------------+
| tutorial_id | tutorial_author | tutorial_title | submission_date |
+-------------+-----------------+----------------+-----------------+
|         101 | Sanjay          | Learn Java     | 06-05-2007      |
|         102 | Abdul S         | Learn MySQL    | 19-04-2007      |
|         103 | Krishna Kasyap  | Learn JavaFX   | 06-07-2017      |
+-------------+-----------------+----------------+-----------------+
3 rows in set (0.00 sec)

Writer − 我们在应用程序中使用的 Writer 是 FlatFileItemWriter ,用于将数据写入 flatfile (.txt)。

Writer − The Writer we are using in the application is FlatFileItemWriter to write the data to flatfile (.txt).

Processor − 我们在应用程序中使用的 Processor 是一个自定义处理器,它只是打印从 CSV 文件中读取的记录。

Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file.

jobConfig.xml

以下是我们示例 Spring Batch 应用程序的配置文件。在该文件中,我们将定义 Job 和 Steps。除这些之外,我们还定义 ItemReader、ItemProcessor 和 ItemWriter 的 Bean。(在此处,我们将它们与各自的类相关联,并传递必需属性的值来配置它们。)

Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass the values for the required properties to configure them.)

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util = "http://www.springframework.org/schema/util"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch

      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

   <import resource = "../jobs/context.xml" />
   <bean id = "tutorial" class = "Tutorial" scope = "prototype" />
   <bean id = "itemProcessor" class = "CustomItemProcessor" />

   <batch:job id = "helloWorldJob">
      <batch:step id = "step1">
         <batch:tasklet>
            <batch:chunk reader = "mysqlItemReader"
               writer = "flatFileItemWriter" processor = "itemProcessor"
               commit-interval = "10">
            </batch:chunk>
         </batch:tasklet>
      </batch:step>
   </batch:job>

   <bean id = "mysqlItemReader"
      class = "org.springframework.batch.item.database.JdbcCursorItemReader" >
      <property name = "dataSource" ref = "dataSource" />
      <property name = "sql" value = "select * from details.tutorialsdata" />
      <property name = "rowMapper">
         <bean class = "TutorialRowMapper" />
      </property>
   </bean>

   <bean id = "flatFileItemWriter"
      class = " org.springframework.batch.item.file.FlatFileItemWriter">
      <property name = "resource" value = "file:target/outputfiles/employee_output.txt"/>
      <property name = "lineAggregator">
         <bean class = " org.springframework.batch.item.file.transform.PassThroughLineAggregator"/>
      </property>
   </bean>
</beans>

Context.xml

以下是我们 Spring Batch 应用程序的 context.xml 。在该文件中,我们将定义 Bean,如 Job 存储库、Job 启动器和事务管理器。

Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/jdbc
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">

   <!-- stored job-meta in database -->
   <bean id = "jobRepository"
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
      <property name = "dataSource" ref = "dataSource" />
      <property name = "transactionManager" ref = "transactionManager" />
      <property name = "databaseType" value = "mysql" />
   </bean>

   <bean id = "transactionManager"
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

   <bean id = "dataSource"
      class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
      <property name = "url" value = "jdbc:mysql://localhost:3306/details" />
      <property name = "username" value = "myuser" />
      <property name = "password" value = "password" />
   </bean>

   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository" />
   </bean>

   <!-- create job-meta tables automatically -->
   <jdbc:initialize-database data-source = "dataSource">
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql" />
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql" />
   </jdbc:initialize-database>
</beans>

CustomItemProcessor.java

以下是 Processor 类。在该类中,我们编写应用程序的处理代码。此处,我们打印每个记录的内容。

Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record.

import org.springframework.batch.item.ItemProcessor;

// Implementing the ItemProcessor interface
public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);
      return item;
   }
}

TutorialRowMapper.java

以下是 TutorialRowMapper 类,它将数据设置为 Tutorial 类。

Following is the TutorialRowMapper class which sets the data to the Tutorial class.

public class TutorialRowMapper implements RowMapper<Tutorial> {

   @Override
   public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {

      Tutorial tutorial = new Tutorial();

      tutorial.setTutorial_id(rs.getInt("tutorial_id"));
      tutorial.setTutorial_title(rs.getString("tutorial_title"));
      tutorial.setTutorial_author(rs.getString("tutorial_author"));
      tutorial.setSubmission_date(rs.getString("submission_date"));
      return tutorial;
   }
}

Tutorial.java

以下是 Tutorial 类。它是一个简单的 Java 类,具有 settergetter 方法。在该类中,我们使用注解来将该类的这些方法与 XML 文件的这些标记相关联。

Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file.

public class Tutorial {
   private int tutorial_id;
   private String tutorial_title;
   private String tutorial_author;
   private String submission_date;

   public int getTutorial_id() {
      return tutorial_id;
   }

   public void setTutorial_id(int tutorial_id) {
      this.tutorial_id = tutorial_id;
   }

   public String getTutorial_title() {
      return tutorial_title;
   }

   public void setTutorial_title(String tutorial_title) {
      this.tutorial_title = tutorial_title;
   }

   public String getTutorial_author() {
      return tutorial_author;
   }

   public void setTutorial_author(String tutorial_author) {
      this.tutorial_author = tutorial_author;
   }

   public String getSubmission_date() {
      return submission_date;
   }
   public void setSubmission_date(String submission_date) {
      this.submission_date = submission_date;
   }

   @Override
   public String toString() {
      return " [id=" + tutorial_id + ", title=" +
      tutorial_title                      + ",
      author=" + tutorial_author + ", date=" +
      submission_date + "]";
   }
}

App.java

以下是启动批处理的代码。在该类中,我们将通过运行 JobLauncher 来启动 Batch 应用程序。

Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

   public static void main(String[] args) throws Exception {

      String[] springConfig  =  { "jobs/job_hello_world.xml" };

      // Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

      // Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

      // Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

      // Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

执行此应用程序后,它将生成以下输出。

On executing this application, it will produce the following output.

May 09, 2017 5:44:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXml
ApplicationContext@3d646c37: startup date [Tue May
09 17:44:48 IST 2017]; root of context hierarchy
May 09, 2017 5:44:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
May 09, 2017 5:44:56 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] launched
with the following parameters: [{}]
May 09, 2017 5:44:56 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing...Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007]
Processing...Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007]
Processing...Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=0607-2017]
May 09, 2017 5:44:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters:
[{}] and the following status: [COMPLETED]
Hello
Exit Status : COMPLETED

这将生成一个内容如下所示的 .txt 文件。

This will generate a .txt file with the following contents.

Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007]
Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007]
Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=06-07-2017]