Results 1 to 5 of 5

Thread: Stop a Job during Chunk Processing - Within the program

  1. #1
    Join Date
    Mar 2011
    Posts
    14

    Question Stop a Job during Chunk Processing - Within the program

    Hi All,

    I have a csv file, which I have to read and update a table. Incase, one of the fields in the csv file is improper - I have to terminate the job and make sure no update from the file takes place.

    (Question 1) Apart from having the commit level as a high value, is there any way I can make sure - that no data from file is present, if the file is improper. Will any completion policy help me?

    (Question 2) Apart from throwing a RuntimeException(), can I stop the processing the step in between. Make sure, my ItemWriter is not called - if ItemProcessor finds that the data is improper?
    I have tried using stepExecution.setTerminateOnly() - didn't work. The terminateFlag is checked only at the beginning or end of the Step.

    This is my ApplicationContext file

    Code:
    <job id="simpleTest">
    		<step id="step1">
    			<tasklet>
    				<chunk reader="csvReader"
    				       processor="csvProcessor"
    					   writer="csvWriter"
    					   commit-interval="1000" 
    					   skip-limit="100">
    					<skippable-exception-classes>
    						<include class="java.lang.Exception"/>
    						<exclude class="org.springframework.batch.item.ReaderNotOpenException"/>
    					</skippable-exception-classes>
    				</chunk>
    				<listeners>
    					  <listener ref="chunkListener" />
    					</listeners>
    					
    			</tasklet>
    		</step>
    	</job>
    	
    	<!-- Defining the bean for chunkListener -->
    	<beans:bean id="chunkListener"
    		class="test.simpleTest.process.CustomItemListenerSupport" />
    <beans:bean id="transactionManager" 
    			class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" lazy-init="true">
        	 </beans:bean>
    	
    <beans:bean id="jobRepository"
    		class="org.springframework.batch.core.repository.support.SimpleJobRepository">
    		<beans:constructor-arg>
    			<beans:bean
    				class="org.springframework.batch.core.repository.dao.MapJobInstanceDao" />
    		</beans:constructor-arg>
    		<beans:constructor-arg>
    			<beans:bean
    				class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
    		</beans:constructor-arg>
    		<beans:constructor-arg>
    			<beans:bean
    				class="org.springframework.batch.core.repository.dao.MapStepExecutionDao" />
    		</beans:constructor-arg>
    		<beans:constructor-arg>
    			<beans:bean
    				class="org.springframework.batch.core.repository.dao.MapExecutionContextDao" />
    		</beans:constructor-arg>
    		</beans:bean>
    		
    	<beans:bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<beans:property name="jobRepository" ref="jobRepository" />
    	</beans:bean>
    Here CustomItemListenerSupport class extends ItemListenerSupport class.

  2. #2
    Join Date
    Mar 2011
    Posts
    14

    Default

    Adding to that, in the ItemProcessor class I am setting the TerminateOnly() flag. And in ItemWriter, checking for the flag. If ON, then exiting the class.

  3. #3
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    looks like the way an ItemProcessor works exactly addresses questions 1 and 2. An item processor can implement validation and return null if the item shouldn't be sent to the item writer. This doesn't stop processing. The item processor can also throw an exception and the chunk will be rolled back. This is going to stop the step execution (and the job execution by default.)

  4. #4
    Join Date
    Mar 2011
    Posts
    14

    Default

    So throwing a RuntimeException would be the only option?

    And do you have any idea, if completion-policy would help me in calling my item reader, as long as there is no more data to be read?

  5. #5
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    So throwing a RuntimeException would be the only option?
    using setTerminateOnly should work too, I'm surprised it doesn't with you. How did you get access to the StepExecution?

    And do you have any idea, if completion-policy would help me in calling my item reader, as long as there is no more data to be read?
    why don't you return null from your ItemReader?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •