Spring Batch - Configuration

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.
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 −
Id
It is the Id of the job, it is mandatory to specify value to this attribute.
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>

This tag is used to define/configure the steps of a SpringBatch job. It has the following three attributes −
Id
It is the Id of the job, it is mandatory to specify value to this attribute.
next
It is the shortcut to specify the next step.
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>
This tag is used to define/configure a chunk of a tasklet. It has the following four attributes −
reader
It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemReader.
writer
It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemWriter.
processor
It represents the name of the item reader bean. It accepts the value of the type org.springframework.batch.item.ItemProcessor.
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> 
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.
dataSource
It is used to specify the bean name which defines the datasource.
transactionManager
It is used specify the name of the bean which defines the transactionmanager.
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> 
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>
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" />
The datasource bean is used to configure the Datasource. This bean is associated with the class of type org.springframework.jdbc.datasource.DriverManagerDataSource.
driverClassName
This specifies the class name of the driver used to connect with the database.
url
This specifies the URL of the database.
username
This specifies the username to connect with the database.
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> 

    102 Lectures 
     8 hours 

 Karthikeya T

    39 Lectures 
     5 hours 

 Chaand Sheikh

    73 Lectures 
     5.5 hours 

 Senol Atac

    62 Lectures 
     4.5 hours 

 Senol Atac

    67 Lectures 
     4.5 hours 

 Senol Atac

    69 Lectures 
     5 hours 

 Senol Atac
 Print
 Add Notes
 Bookmark this page