In this post under Spring Batch, I will explain what is the purpose of Job Repository in Spring Batch and how to store it in In-Memory datastore.
Job Repository is used to store the state of a Job (finished or currently executing). In other words Job Repository stores all the metadata of a Job. Informations
such as list of executed steps, how many items read, wrote, or skipped, the duration of each step etc. Job repository can be stored in in-memory or in database.
To store the job repository in in-memory, below is the xml configuration
XML Configuration
1 <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
2
3 <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
4 <property name="transactionManager" ref="transactionManager"/>
5 </bean>
6
7 <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
8 <property name="jobRepository" ref="jobRepository"/>
9 </bean>
As shown in above xml line 3, we need to create an instance of MapJobRepositoryFactoryBean with id “jobRepository”. MapJobRepositoryFactoryBean, a factory bean creates an instance of SimpleJobRepository, which stores metadata in in-memory datastore.
Next we inform the JobLauncher to use the in-memory datastore with id “jobRepository”. Refer to line 8.
The jobRepository instance need a transaction manager. So we create a instance of ResourcelessTransactionManager at line 1 and set it to jobRepository instance at line 4.
In this way we create job repository that will be stored in in-memory datastore. This is useful for testing or when monitoring or restart capabilities is not needed.
Below is the complete xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
<bean id="employee" class="package1.Employee" scope="prototype"/>
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="file:FileInput.txt"/>
<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="id,name,status,salary"/>
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="employee"/>
</bean>
</property>
</bean>
</property>
</bean>
<bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:FileOutput.txt"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="id,name,status,salary"/>
</bean>
</property>
</bean>
</property>
</bean>
<batch:job id="importEmployees">
<batch:step id="readWriteEmployees">
<batch:tasklet>
<batch:chunk reader="reader" writer="writer" commit-interval="50"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
</beans>