问题
The reader looks like this,
<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="classpath:cvs/input/report.csv" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="id,impressions" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="report" />
</bean>
</property>
</bean>
</property>
</bean>
What I want to achieve is, if while reading the CSV file, the reader/job fails, then the next scheduled jobinstance should start the reader from the line it failed in last jobexecution. I can keep track of lines processed by a counter in the CustomProcessor process() function.
But any idea how to read the csv file from the last execution's failed state?
I want to manually launch/restart the failed job. Should I implement SkipPolicy as described here http://thisisurl.com/spring-batch-skip-and-retry?
(CSV file can be a big DB as well, but for now I am testing this scenario on CSV file)
回答1:
If your application fails, you can use folowing:
But any idea how to read the csv file from the last execution's failed state?
Add it to jobParameters they saved in batch table batch_job_execution_params.
At application start up find incomplete jobs in batch_job_execution and restart in manualy.
I want to manually launch/restart the failed job. Should I implement SkipPolicy as described here http://thisisurl.com/spring-batch-skip-and-retry?
I think this is not necessary, for restartability.
If you want restart some lines in your csv, just use RetryPolicy:
<batch:step id="processing-step">
<batch:tasklet>
<batch:chunk reader="csv_reader"
processor="csv_processor"
writer="csv_writer"
commit-interval="5">
<batch:retry-policy>
<bean class="org.springframework.retry.policy.AlwaysRetryPolicy" scope="step"/>
</batch:retry-policy>
</batch:chunk>
</batch:tasklet>
</batch:step>
Spring Batch automatically handle failed chunks (chunks, in which throws an exception). Documentation.
来源:https://stackoverflow.com/questions/21451934/spring-batch-job-csv-file-read-recover-from-the-line-where-it-failed