Storing Job Repository in In-Memory datastore

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="" />
3 <bean id="jobRepository" class="">
4   <property name="transactionManager" ref="transactionManager"/>
5 </bean>
7 <bean id="jobLauncher" class="">
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="" xmlns:xsi=""
    <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"/>
                <property name="fieldSetMapper">
                    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="employee"/>
    <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"/>
    <batch:job id="importEmployees">
        <batch:step id="readWriteEmployees">
                <batch:chunk reader="reader" writer="writer" commit-interval="50"/>
    <bean id="transactionManager" class="" />
    <bean id="jobRepository" class="">
        <property name="transactionManager" ref="transactionManager"/>
    <bean id="jobLauncher" class="">
        <property name="jobRepository" ref="jobRepository"/>

Leave a Reply