Announcement Announcement Module
Collapse
No announcement yet.
spring batch 2.1.6: newbie question regarding jdbc item reader and writer. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring batch 2.1.6: newbie question regarding jdbc item reader and writer.

    So i have this xml file.
    Code:
    <?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:batch="http://www.springframework.org/schema/batch"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
    	<batch:job-repository
    	id="repository" 
    	data-source="dataSource"
    	transaction-manager="transactionManager" 
    	isolation-level-for-create="DEFAULT" />
    	
    	<batch:job id="enableUserJob" 
    		job-repository="repository" 
    		restartable="true">
    		<batch:step id="activateUser">
    			<batch:tasklet allow-start-if-complete="false" transaction-manager="transactionManager">
    				<batch:chunk writer="userItemWriter" reader="userItemReader" commit-interval="${hibernate.jdbc.batch_size}"/>
    				<batch:transaction-attributes propagation="REQUIRED"/>
    			</batch:tasklet>
    		</batch:step>
    	</batch:job>
    	
    	<bean id="userStatusMapper" class="com.advertising.mapper.UserStatusMapper"/>
    	<bean id="userItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    		<property name="sql">
    			<value>
    				<![CDATA[
    				SELECT user_id, user_status  FROM user WHERE user_status=:lockStatus AND create_time < :current_time
    				]]>
    			</value>
    		</property>
    		<property name="queryTimeout" value="30"/>
    		<property name="fetchSize" value="${hibernate.jdbc.batch_size}"/>
    		<property name="rowMapper" ref="userStatusMapper"/>
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	<bean id="userItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="sql">
    			<value>
    				<![CDATA[
    				UPDATE user SET user_status=:enableStatus WHERE user_id=:userId
    				]]>
    			</value>
    		<property name="itemSqlParameterSourceProvider" ref="sourceProvider"/>
    	</bean>
    	
    	<bean id="sourceProvider" class="com.advertising.provider.UserStatusSourceProvider"/>
    </beans>
    So i have several question.
    1.) How do i pass in parameter as run time parameter (i.e. current time from shell script). [Note: i can write the script], but getting spring batch to accept run time parameter.
    2.) Is this correct way to read from db and than write to db.

    -- I want to be able to read from db all the user that are lock and have been created within last 5 minute (I can pass in via shell script), write to another db.

  • #2
    if you control the shell script, you can use job parameters to transmit data to the Spring Batch configuration. Have a look at the reference documentation: http://static.springsource.org/sprin...l#late-binding

    Comment


    • #3
      Thanks, try it now the only problem is that I can't change the pattern of the date. It seems to parse only "yyyy/MM/dd" format.

      I tried using DefaultJobParametersConverter but it has no way to set the format of the date input.

      Comment


      • #4
        never mind. i got it working like this.

        Code:
        	<bean id="defaultJobParameters" class="org.springframework.batch.core.converter.DefaultJobParametersConverter">
        		<property name="dateFormat" ref="dateFormat"/>
        	</bean>
        	
        	<bean id="dateFormat" class="java.text.SimpleDateFormat">
        		<constructor-arg index="0" value="yyyy/MM/dd hh:mm" type="java.lang.String"/>
        	</bean>

        Comment

        Working...
        X