Spring Batch 简明教程

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>