Spring Batch 2.0 - Part II - Flat File To Database
The file is a comma separated file with format => receiptDate,memberName,checkNumber,checkDate,paymentType,depositAmount,paymentAmount,comments
The DDL for the database table:
create table ledger (
ID INT NOT NULL AUTO_INCREMENT,)
rcv_dt date,
mbr_nm VARCHAR(100) not null,
chk_nbr VARCHAR(10) not null,
chk_dt date,
pymt_typ VARCHAR(50) not null,
dpst_amt double,
pymt_amt double,
comments VARCHAR(100),
PRIMARY KEY (ID)
Here is the spring application context xml file...
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:batch="http://www.springframework.org/schema/batch" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-2.0.xsd class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"> http://www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-2.0.xsd class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"> http://www.springframework.org/schema/batch size="1">www.springframework.org/schema/batch/spring-batch-2.0.xsd http://www.springframework.org/schema/context href="http://www.springframework.org/schema/context/spring-context-2.5.xsd">www.springframework.org/schema/context/spring-context-2.5.xsd" > <!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS --> <context:component-scan base-package="com.batch"/> <!-- 2)DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"value="com.mysql.jdbc.Driver" /> <property name="url"value="jdbc:mysql://localhost/seamdb" /> <property name="username"value="root" /> <property name="password"value="root" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManage"> <property name="dataSource"ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"ref="dataSource" /> </bean> <!-- 3) JOBREPOSITORY - WE USE IN-MEMORY REPOSITORY FOR OUR EXAMPLE --> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager"ref="transactionManager" /> </bean> <!-- 4)LAUNCH JOBS FROM A REPOSITORY --> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository"ref="jobRepository" /> </bean> <!-- 5) Define the job and its steps. In our case I use onestep. Configure its readers and writers --> <batch:job id="simpleJob"> <batch:listeners> <batch:listener ref="appJobExecutionListener"/> </batch:listeners> <batch:step id="step1"> <batch:tasklet> <batch:listeners> <batch:listener ref="itemFailureLoggerListener"/> </batch:listeners> <batch:chunk reader="itemReader"writer="itemWriter" commit-interval="1000" /> </batch:tasklet> </batch:step> </batch:job> <!--======================================================= --> <!-- 6) READER --> <!--======================================================= --> <bean id="itemReader"class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource"value="classpath:com/batch/todb/ledger.txt"/> <!--property name="linesToSkip" value="1" /--> <property name="lineMapper"> <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="lineTokenizer"> <bean <property name="names" value="receiptDate,memberName,checkNumber,checkDate,paymentType,depositAmount,paymentAmount,comments"/> </bean> </property> <property name="fieldSetMapper"ref="ledgerMapper" /> </bean> </property> </bean> <bean id="inputFile"class="org.springframework.core.io.ClassPathResource"> <constructor-arg value="com/batch/todb/ledger.txt"/> </bean> </beans> |
- 1 through 4 are the same as the previous blog.
- 5 - Defines the job and its steps. Also registers a job listener and a step listener
- 6 - The reader used to read the flat file with comma separated columns. The FlatFileItemReader will read the rows from the flat file and pass it to the writer to persist to the database..
- 8 - Not shown here is the item writer. It is configured using annotations and the class is LedgerWriter.
//======================================================== // Ledger BEAN - Bean representing a single ledger //======================================================== package com.batch.todb; //======================================================== // Ledger DAO - Used to persist ledgers to the ledger table //======================================================== package com.batch.todb; //======================================================== // Ledger WRITER - Performs db operations on a given list of ledger objects //======================================================== package com.batch.todb; //======================================================== // Ledger MAPPER - Maps a set of fields for a single record to the Ledger bean //======================================================== package com.batch.todb; |
The test driver again is a JUnit class.
package com.batch.todb; |
Running the test case will insert approx 200k rows into the ledger table. It took roughly 1:12 seconds for the entire process to execute.
INFO ToDBBatchTestCase:46 - >>> TIME ELAPSED:StopWatch '': running time (millis) = 71678
Next move over to Spring Batch - Part III - From Database to Flat File
Please see Part III to download entire project file with dependencies






You tutorial is excellent.
Can you please out the required jar files and NoteaDAO interface?
I having bigtime trouble running the code
Thanks
Reply to this
Please refer to Part III of the series. I have linked in a jar file with the entire project including libraries. Note that I am using an older version of Spring Batch. I have yet to update this with the latest version. Maybe tonight
Reply to this
I downloaded all code but could not find where ledger.txt ? Can you help?
Reply to this
I found it
Reply to this