In Part-II of this series on Spring Batch, I went through an example of reading from a flat file and persisting into the database. In this article I will go through the reverse. Read 200,000 rows from the database and export it into a comma separated flat file.
The export from the database to the flat file took around 10 seconds. That is excellent for Java-based batch processing. Again I must point out that this is relatively fast since I am using a local MySQL database and there is no processing related logic being performed during the entire process.
The file is a comma separated file with format => receiptDate,memberName,checkNumber,checkDate,paymentType,depositAmount,paymentAmount,comments
DDL for the database table :
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="color: #008000; font-weight: bold;">create</span> <span style="color: #008000; font-weight: bold;">table</span> ledger ( id <span style="color: #007020;">int</span> <span style="color: #008000; font-weight: bold;">GENERATED</span> <span style="color: #008000; font-weight: bold;">BY</span> <span style="color: #008000; font-weight: bold;">DEFAULT</span> <span style="color: #008000; font-weight: bold;">AS</span> <span style="color: #008000; font-weight: bold;">IDENTITY</span> (<span style="color: #008000; font-weight: bold;">START</span> <span style="color: #008000; font-weight: bold;">WITH</span> <span style="color: #0000d0; font-weight: bold;">1</span>, <span style="color: #008000; font-weight: bold;">INCREMENT</span> <span style="color: #008000; font-weight: bold;">BY</span> <span style="color: #0000d0; font-weight: bold;">1</span>) <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">null</span>, rcv_dt <span style="color: #007020;">date</span>, mbr_nm <span style="color: #007020;">VARCHAR</span>(<span style="color: #0000d0; font-weight: bold;">100</span>) <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">null</span>, chk_nbr <span style="color: #007020;">VARCHAR</span>(<span style="color: #0000d0; font-weight: bold;">10</span>) <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">null</span>, chk_dt <span style="color: #007020;">date</span>, pymt_typ <span style="color: #007020;">VARCHAR</span>(<span style="color: #0000d0; font-weight: bold;">50</span>) <span style="color: #008000; font-weight: bold;">not</span> <span style="color: #008000; font-weight: bold;">null</span>, dpst_amt double, pymt_amt double, comments <span style="color: #007020;">VARCHAR</span>(<span style="color: #0000d0; font-weight: bold;">100</span>), <span style="color: #008000; font-weight: bold;">constraint</span> PID <span style="color: #008000; font-weight: bold;">primary</span> <span style="color: #008000; font-weight: bold;">key</span> (id) ); |
Here is the spring application context xml file…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<span style="color: #507090;"><?xml version="1.0" encoding="UTF-8"?></span> <span style="color: #007000;"><beans</span> <span style="color: #0000c0;">xmlns=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/beans"</span> <span style="color: #0000c0;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color: #0000c0;">xmlns:aop=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/aop"</span> <span style="color: #0000c0;">xmlns:tx=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/tx"</span> <span style="color: #0000c0;">xmlns:batch=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/batch"</span> <span style="color: #0000c0;">xmlns:jdbc=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/jdbc"</span> <span style="color: #0000c0;">xmlns:context=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/context"</span> <span style="color: #0000c0;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</span> <span style="background-color: #fff0f0;"> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd</span> <span style="background-color: #fff0f0;"> http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd</span> <span style="background-color: #fff0f0;"> http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd </span> <span style="background-color: #fff0f0;"> http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd</span> <span style="background-color: #fff0f0;"> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"</span><span style="color: #007000;">></span> <span style="color: #808080;"><!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS --></span> <span style="color: #007000;"><context:component-scan</span> <span style="color: #0000c0;">base-package=</span><span style="background-color: #fff0f0;">"com.batch"</span> <span style="color: #007000;">/></span> <span style="color: #808080;"><!-- 2) DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE --></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.jdbc.datasource.DriverManagerDataSource"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"driverClassName"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"org.hsqldb.jdbcDriver"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"url"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"jdbc:hsqldb:file:target/data/ledger"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"username"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"SA"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"password"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">""</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"transactionManager"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #007000;"><tx:annotation-driven</span> <span style="color: #0000c0;">transaction-manager=</span><span style="background-color: #fff0f0;">"transactionManager"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"jdbcTemplate"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.jdbc.core.JdbcTemplate"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #808080;"><!-- 3) JOB REPOSITORY - WE USE IN-MEMORY REPOSITORY FOR OUR EXAMPLE --></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"jobRepository"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"transactionManager"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"transactionManager"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #808080;"><!-- 4) LAUNCH JOBS FROM A REPOSITORY --></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"jobLauncher"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.core.launch.support.SimpleJobLauncher"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"jobRepository"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"jobRepository"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #808080;"><!-- 5) Define the job and its steps. In our case I use one step. Configure </span> <span style="color: #808080;"> its readers and writers --></span> <span style="color: #007000;"><batch:job</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"simpleJob"</span><span style="color: #007000;">></span> <span style="color: #007000;"><batch:step</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"step1"</span><span style="color: #007000;">></span> <span style="color: #007000;"><batch:tasklet></span> <span style="color: #007000;"><batch:chunk</span> <span style="color: #0000c0;">reader=</span><span style="background-color: #fff0f0;">"cursorReader"</span> <span style="color: #0000c0;">writer=</span><span style="background-color: #fff0f0;">"flatFileWriter"</span> <span style="color: #0000c0;">commit-interval=</span><span style="background-color: #fff0f0;">"1000"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></batch:tasklet></span> <span style="color: #007000;"></batch:step></span> <span style="color: #007000;"></batch:job></span> <span style="color: #808080;"><!-- ======================================================= --></span> <span style="color: #808080;"><!-- 6) READER --></span> <span style="color: #808080;"><!-- ======================================================= --></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"cursorReader"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.item.database.JdbcCursorItemReader"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"dataSource"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"sql"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"select * from ledger"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"rowMapper"</span> <span style="color: #0000c0;">ref=</span><span style="background-color: #fff0f0;">"ledgerRowMapper"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #808080;"><!-- ======================================================= --></span> <span style="color: #808080;"><!-- 7) WRITER --></span> <span style="color: #808080;"><!-- ======================================================= --></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">id=</span><span style="background-color: #fff0f0;">"flatFileWriter"</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.item.file.FlatFileItemWriter"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"resource"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"file:c:/temp/ledgers-output.txt"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"lineAggregator"</span><span style="color: #007000;">></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.item.file.transform.DelimitedLineAggregator"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"delimiter"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">","</span> <span style="color: #007000;">/></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"fieldExtractor"</span><span style="color: #007000;">></span> <span style="color: #007000;"><bean</span> <span style="color: #0000c0;">class=</span><span style="background-color: #fff0f0;">"org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"</span><span style="color: #007000;">></span> <span style="color: #007000;"><property</span> <span style="color: #0000c0;">name=</span><span style="background-color: #fff0f0;">"names"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"id,receiptDate,memberName"</span> <span style="color: #007000;">/></span> <span style="color: #007000;"></bean></span> <span style="color: #007000;"></property></span> <span style="color: #007000;"></bean></span> <span style="color: #007000;"></property></span> <span style="color: #007000;"></bean></span> <span style="color: #007000;"></beans></span> |
- 1 through 4 are the same as the previous blog.
- 5 – Defines the step job and its steps.
- 6 – Registers a JdbcCursorItemReader which will read the rows from the database. It will then write them out to the flat file. The latest version also has a new reader JdbcPagingItemReader. This is a better option since it will read a predefined set of rows rather than making round trips for each row.
- 8 – Configure the writer to write to a flat file
Here is the Java code for the mapper LedgerRowMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<span style="color: #008000; font-weight: bold;">package</span> com<span style="color: #303030;">.</span><span style="color: #0000c0;">batch</span><span style="color: #303030;">.</span><span style="color: #0000c0;">fromdb</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.sql.ResultSet</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.sql.SQLException</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.jdbc.core.RowMapper</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.batch.todb.Ledger</span><span style="color: #303030;">;</span> <span style="color: #505050; font-weight: bold;">@Component</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"ledgerRowMapper"</span><span style="color: #303030;">)</span> <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #008000; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">LedgerRowMapper</span> <span style="color: #008000; font-weight: bold;">implements</span> RowMapper <span style="color: #303030;">{</span> <span style="color: #008000; font-weight: bold;">public</span> Object <span style="color: #0060b0; font-weight: bold;">mapRow</span><span style="color: #303030;">(</span>ResultSet rs<span style="color: #303030;">,</span> <span style="color: #303090; font-weight: bold;">int</span> rowNum<span style="color: #303030;">)</span> <span style="color: #008000; font-weight: bold;">throws</span> SQLException <span style="color: #303030;">{</span> Ledger ledger <span style="color: #303030;">=</span> <span style="color: #008000; font-weight: bold;">new</span> Ledger<span style="color: #303030;">();</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setId</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getInt</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"id"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setReceiptDate</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getDate</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"rcv_dt"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setMemberName</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getString</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"mbr_nm"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setCheckNumber</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getString</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"chk_nbr"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setCheckDate</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getDate</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"chk_dt"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setPaymentType</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getString</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"pymt_typ"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setDepositAmount</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getDouble</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"dpst_amt"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setPaymentAmount</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getDouble</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"pymt_amt"</span><span style="color: #303030;">));</span> ledger<span style="color: #303030;">.</span><span style="color: #0000c0;">setComments</span><span style="color: #303030;">(</span>rs<span style="color: #303030;">.</span><span style="color: #0000c0;">getString</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"comments"</span><span style="color: #303030;">));</span> <span style="color: #008000; font-weight: bold;">return</span> ledger<span style="color: #303030;">;</span> <span style="color: #303030;">}</span> <span style="color: #303030;">}</span> |
Here is test driver class (a the junit test case).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<span style="color: #008000; font-weight: bold;">package</span> com<span style="color: #303030;">.</span><span style="color: #0000c0;">batch</span><span style="color: #303030;">.</span><span style="color: #0000c0;">fromdb</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.apache.log4j.Logger</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.junit.Test</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.junit.runner.RunWith</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.batch.core.Job</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.batch.core.JobParameters</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.batch.core.launch.JobLauncher</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.test.context.ContextConfiguration</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.util.StopWatch</span><span style="color: #303030;">;</span> <span style="color: #505050; font-weight: bold;">@ContextConfiguration</span><span style="color: #303030;">(</span>locations <span style="color: #303030;">=</span> <span style="background-color: #fff0f0;">"classpath:com/batch/fromdb/contextFromDB.xml"</span><span style="color: #303030;">)</span> <span style="color: #505050; font-weight: bold;">@RunWith</span><span style="color: #303030;">(</span>SpringJUnit4ClassRunner<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #008000; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">FromDBBatchTestCase</span> <span style="color: #303030;">{</span> <span style="color: #008000; font-weight: bold;">private</span> <span style="color: #008000; font-weight: bold;">final</span> <span style="color: #008000; font-weight: bold;">static</span> Logger logger <span style="color: #303030;">=</span> Logger <span style="color: #303030;">.</span><span style="color: #0000c0;">getLogger</span><span style="color: #303030;">(</span>FromDBBatchTestCase<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">);</span> <span style="color: #505050; font-weight: bold;">@Autowired</span> <span style="color: #008000; font-weight: bold;">private</span> JobLauncher launcher<span style="color: #303030;">;</span> <span style="color: #505050; font-weight: bold;">@Autowired</span> <span style="color: #008000; font-weight: bold;">private</span> Job job<span style="color: #303030;">;</span> <span style="color: #008000; font-weight: bold;">private</span> JobParameters jobParameters <span style="color: #303030;">=</span> <span style="color: #008000; font-weight: bold;">new</span> JobParameters<span style="color: #303030;">();</span> <span style="color: #505050; font-weight: bold;">@Test</span> <span style="color: #008000; font-weight: bold;">public</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">testLaunchJob</span><span style="color: #303030;">()</span> <span style="color: #008000; font-weight: bold;">throws</span> Exception <span style="color: #303030;">{</span> StopWatch sw <span style="color: #303030;">=</span> <span style="color: #008000; font-weight: bold;">new</span> StopWatch<span style="color: #303030;">();</span> sw<span style="color: #303030;">.</span><span style="color: #0000c0;">start</span><span style="color: #303030;">();</span> launcher<span style="color: #303030;">.</span><span style="color: #0000c0;">run</span><span style="color: #303030;">(</span>job<span style="color: #303030;">,</span> jobParameters<span style="color: #303030;">);</span> sw<span style="color: #303030;">.</span><span style="color: #0000c0;">stop</span><span style="color: #303030;">();</span> logger<span style="color: #303030;">.</span><span style="color: #0000c0;">info</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">">>> TIME ELAPSED:"</span> <span style="color: #303030;">+</span> sw<span style="color: #303030;">.</span><span style="color: #0000c0;">prettyPrint</span><span style="color: #303030;">());</span> <span style="color: #303030;">}</span> <span style="color: #303030;">}</span> |
After running the test case, you will see a file c:\temp\ledgers-output.txt with 200,000 rows.
1 |
19:53:56,832 INFO FromDBBatchTestCase:34 - >>> TIME ELAPSED:StopWatch '': running time (millis) = 7104 |
You can download the Maven project from GitHub – https://github.com/thomasma/springbatch3part.