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.