Spring Batch - Configuration


Advertisements


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"> 

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

Job

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.

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.

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

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

Step

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.

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

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.

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

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.

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

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.

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

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

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.

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> 


Advertisements