Spring batch Read Process Write

Read from DataBase Write in XML

    1. database.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" id="userProps">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="searchSystemEnvironment" value="true"/>
        <property name="locations">
            <list>
                <value>classpath*:/props/*.properties</value>
            </list>
        </property>
    </bean>
    <!-- connect to database -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${datasource.url}" />
        <property name="username" value="${datasource.username}" />
        <property name="password" value="${datasource.password}" />
    </bean>    
         
    <!--Used for FTL POJO generation-->       
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>    
	
    <!--create job-meta tables automatically--> 
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
        <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
    </jdbc:initialize-database>    
</beans>
    1. context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/util 
		http://www.springframework.org/schema/util/spring-util-3.2.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- stored job-meta in mysql --> 
    <bean id="jobRepository"
          class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="dataSource" ref="dataSource" />
        <property name="databaseType" value="mysql" />
    </bean>
 	
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>        
    
<!--    <bean id="transactionManager"
          class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />-->
	 
    <bean id="jobLauncher"
          class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
    
    <bean id="userUnmarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <util:map id="aliases">
                <entry key="user" value="com.javahunter.exampit.User" />                
                <entry key="useraddress" value="com.javahunter.exampit.UserAddress" />                
                <entry key="userdetail" value="com.javahunter.exampit.UserDetail" />                
            </util:map>
        </property>
    </bean>           
</beans>
    1. userDBReaderXMLWriter.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:batch="http://www.springframework.org/schema/batch"        
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/batch 
		http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
        <import resource="../context.xml" />
       <import resource="../database.xml" />
      
    <bean id="userDBReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${read.project}"/>                
        <property name="rowMapper">
            <bean class="org.springframework.jdbc.core.BeanPropertyRowMapper" >
                <property name="mappedClass" value="com.javahunter.exampit.User"/>
                <property name="primitivesDefaultedForNullValue" value="true"/>
            </bean>
        </property>
    </bean>
<bean id="userDetailDBReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${read.project}"/>                
        <property name="rowMapper">
            <bean class="org.springframework.jdbc.core.BeanPropertyRowMapper" >
                <property name="mappedClass" value="com.javahunter.exampit.UserDetail"/>
                <property name="primitivesDefaultedForNullValue" value="true"/>
            </bean>
        </property>
    </bean>
   <bean id="userAddressDBReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${read.project}"/>                
        <property name="rowMapper">
            <bean class="org.springframework.jdbc.core.BeanPropertyRowMapper" >
                <property name="mappedClass" value="com.javahunter.exampit.UserAdress"/>
                <property name="primitivesDefaultedForNullValue" value="true"/>
            </bean>
        </property>
    </bean>
 <bean id="userXMLWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.user}" />
        <property name="marshaller" ref="userUnmarshaller" />
        <property name="rootTagName" value="users" />
    </bean> 
 <bean id="userDetailXMLWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.userdetail}" />
        <property name="marshaller" ref="userUnmarshaller" />
        <property name="rootTagName" value="userdetails" />
    </bean>
 <bean id="userAddressXMLWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.useraddress}" />
        <property name="marshaller" ref="userUnmarshaller" />
        <property name="rootTagName" value="useraddresss" />
    </bean>
<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
<job id="UserBackup" xmlns="http://www.springframework.org/schema/batch">
        <step id="stepBakUserAddress" next="parallelTables">
            <tasklet>
                <chunk reader="userAddressDBReader" writer="userAddressXMLWriter"
                       commit-interval="10000" />
            </tasklet>            
        </step>
      <split id="parallelTables" task-executor="taskExecutor">
           <flow>
                <step id="stepBakUser">
                    <tasklet>
                        <chunk reader="userDBReader" writer="userXMLWriter"
                               commit-interval="100" />
                    </tasklet>            
                </step>
            </flow>
           <flow>
                <step id="stepBakUserDetail">
                    <tasklet>
                        <chunk reader="userDetailDBReader" writer="userDetailXMLWriter"
                               commit-interval="100" />
                    </tasklet>            
                </step>
            </flow>
      </split>
    </job>   
    </beans>
    1. readqueryxmlpath.properties
read.user=select * from user where userID = #{jobParameters['userID']}
read.userdetail=select * from userdetail where userID = #{jobParameters['userID']}
read.useraddress=select * from useraddress where userID = #{jobParameters['userID']}

xml.user=xml/#{jobParameters['userID']}/user.xml
xml.userdetail=xml/#{jobParameters['userID']}/userdetail.xml
xml.useraddress=xml/#{jobParameters['userID']}/useraddress.xml

Read from XML Write in DataBase

    1. userXMLReaderDBWriter.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:batch="http://www.springframework.org/schema/batch"        
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/batch 
		http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">            
    
    <import resource="../context.xml" />
    <import resource="../database.xml" />
    <bean id="userXMLReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.user}" />
        <property name="fragmentRootElementName" value="user" />
        <property name="unmarshaller" ref="userUnmarshaller"/>                    
    </bean>
    <bean id="userDetailXMLReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.userdetail}" />
        <property name="fragmentRootElementName" value="userdetail" />
        <property name="unmarshaller" ref="userUnmarshaller"/>                    
    </bean>
<bean id="userAddressXMLReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
        <property name="resource" value="file:${user.drivelater}${xml.useraddress}" />
        <property name="fragmentRootElementName" value="useraddress" />
        <property name="unmarshaller" ref="userUnmarshaller"/>                    
    </bean>
    <bean id="userDBWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${write.user}"/>      
        <property name="itemSqlParameterSourceProvider">
            <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </property>
    </bean>
    <bean id="userDetailDBWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${write.userdetail}"/>      
        <property name="itemSqlParameterSourceProvider">
            <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </property>
    </bean>
  <bean id="userAddressDBWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter" scope="step">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="${write.useraddress}"/>      
        <property name="itemSqlParameterSourceProvider">
            <bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </property>
    </bean>
<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>     
<bean id="itemFailureLoggerListener" class="com.javahunter.exampit.listener.ItemFailureLoggerListener" >
        <property name="exceptionHandler" ref="exceptionHandler"/>
    </bean>
     <job id="userRestore" xmlns="http://www.springframework.org/schema/batch" restartable="true">
        <step id="stepResUser" next="detailAndAddress">
            <tasklet transaction-manager="transactionManager">
                <chunk reader="userXMLReader" writer="userDBWriter"
                       commit-interval="100" skip-limit="99999">
                    <skippable-exception-classes>
                        <include class="org.springframework.dao.DataIntegrityViolationException"/>
                        <include class="org.springframework.jdbc.BadSqlGrammarException"/>
                    </skippable-exception-classes>
                </chunk>
                <listeners>
                    <listener ref="itemFailureLoggerListener"/>                    
                </listeners>
            </tasklet>                                
        </step>
        <split id="detailAndAddress" task-executor="taskExecutor">
            <flow>
                <step id="stepResUserDetail">
                    <tasklet transaction-manager="transactionManager">
                        <chunk reader="userDetailXMLReader" writer="userDetailDBWriter" 
                               commit-interval="100" skip-limit="99999">
                            <skippable-exception-classes>
                                <include class="org.springframework.dao.DataIntegrityViolationException"/>
                                <include class="org.springframework.jdbc.BadSqlGrammarException"/>
                            </skippable-exception-classes>
                        </chunk>
                        <listeners>
                            <listener ref="itemFailureLoggerListener"/>                    
                        </listeners>
                    </tasklet>                                
                </step>
</flow>
     <flow>
                <step id="stepResUserAddress">
                    <tasklet transaction-manager="transactionManager">
                        <chunk reader="userAddressXMLReader" writer="userAddressDBWriter" 
                               commit-interval="100" skip-limit="99999">
                            <skippable-exception-classes>
                                <include class="org.springframework.dao.DataIntegrityViolationException"/>
                                <include class="org.springframework.jdbc.BadSqlGrammarException"/>
                            </skippable-exception-classes>
                        </chunk>
                        <listeners>
                            <listener ref="itemFailureLoggerListener"/>                    
                        </listeners>
                    </tasklet>                                
                </step>
</flow>
</split>
</job>
</beans>

ul>

    1. writequery.properties
  • write.user=insert into user (username,password,createdOn,isActive,oldId) values (:username,:password,:createdOn,:isActive,:oldId)
    write.userdetail=insert into userdetail (userId,contact,gender,createdOn,isActive,oldId) values ((select userId from user where oldId=:userId),:contact,:gender,:createdOn,:isActive,:oldId)
    write.useraddress=insert into useraddress (userId,address,city,pincode,country,createdOn,isActive,oldId) values ((select userId from user where oldId=:userId),:address,:city,:pincode,:country,:createdOn,:isActive,:oldId)
    #Create POJOs for the above tables
    
    Advertisements
    This entry was posted in Spring, Spring batch and tagged , , , , . Bookmark the permalink.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s