Spring Batch- Xml based configuration performance over Java based

1.1k Views Asked by At

I am trying to convert the spring batch configuration from xml based to annotation based.

Below is my xml based configuration.

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

    <!-- Step will need a transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<bean id="dbMapper" class="org.test.DBValueMapper">
   </bean>

    <bean id="dbMapperFlatfile" class="org.test.FlatFileRowMapper">
   </bean>

   <bean id="paramSetter" class="org.test.DBParamSetter">
   </bean>
   <bean id="dbReader" class="org.test.DBValueReader"
        scope="step">
        <property name="paramSetter" ref="paramSetter"/>
        <property name="verifyCursorPosition" value="false" />
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="#{jobParameters['SQL_QUERY']}" />
        <property name="rowMapper" ref="dbMapper" />
        <property name="fetchSize" value="5000" />
    </bean>

<bean id="dbWriterIO" class="org.test.TemplateWritterIO"
        scope="step">
        <property name="velocityEngine" ref="velocityEngine" />
<!--        <property name="rptConfig" value="#{jobParameters['RPT_CONFIGVAL']}" /> -->
        <property name="headerCallback" ref="dbWriterIO" />
        <property name="footerCallback" ref="dbWriterIO" />
    </bean>

<batch:job id="fileGenJobNio">
        <batch:step id="fileGenJobStempNio">
            <batch:tasklet>
                <batch:chunk reader="dbReader" writer="dbWriterNIO" 
                    commit-interval="5000">
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>

Below is the equivalent Java based configuration:

    @EnableBatchProcessing
    @Import({ServiceConfiguration.class})
    public class SRBatchGenerator extends DefaultBatchConfigurer{

        @Autowired
        private JobBuilderFactory jobBuilders;


        @Autowired
        private StepBuilderFactory stepBuilders;

        @Autowired
        private VelocityEngine velocityEngine;

         @Autowired
         private DBValueMapper mapper;

         @Autowired
         private DbHelper dbhelper;

        @Autowired
        private DataSource datasource;

        @Bean
        public Step step(){
            return stepBuilders.get("step")
                    .chunk(5000)
                    .reader(reader())
                    //.processor(processor())
                    .writer(writer())
                    //.listener(logProcessListener())
                    .faultTolerant()
                    //.skipLimit(10)
                    //.skip(UnknownGenderException.class)
                    //.listener(logSkipListener())
                    .build();
        }


        @Bean
        public Job fileGeneratorJob(){
            return jobBuilders.get("fileGeneratorJob")
                    //.listener(protocolListener())
                    .start(step())
                    .build();
        }

        @Bean
        public DBValueMapper mapper(){
            return new DBValueMapper();
        }

        @Bean
        @StepScope
        public DBValueReader3 reader(){
            String query="Select Test1,Test2,test3,test4 from RPt_TEST";
            DBValueReader3 dbread = new DBValueReader3();
            dbread.setSql(query);
            dbread.setRowMapper(mapper);
            dbread.setDataSource(datasource);
            return dbread;
        }

        @Bean
        @StepScope
        public TemplateWritterIO writer(){
            TemplateWritterIO writer=new TemplateWritterIO();
            writer.setVelocityEngine(velocityEngine);
            return writer;
        }



        @Override
        protected JobRepository createJobRepository() throws Exception {
            MapJobRepositoryFactoryBean factory = 
                new MapJobRepositoryFactoryBean();
            factory.afterPropertiesSet();
            return  (JobRepository) factory.getObject();
        }

    }

When I execute my Job using xml based it took 27sec to write 1 Million record into Flat file.

But to write same 1 million record, Java based job took about 2 hours to write.

I am not sure what I am missing here. Can anyone help me or guide me why it is slow in Java based configuration.

0

There are 0 best solutions below