Spring batch ItemReader FlatFileItemReader set the cursor to start reading from a specific line or install linestoskip dynamically

In my springbatch + quartz installation, I am reading a CSV file using FlatFileItemReader. I want to set the read cursor to read the next job with the given read parameters. Is it possible?

<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <!-- Read a csv file -->
    <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>

The idea is to continue reading the file where the last failure occurred on the next run. I put an integer "writecursor" for each line written in my customWriter.

public void write(List<? extends Report> items) throws Exception {

System.out.println("writer..." + items.size() + " > ");     
for(Report item : items){
    System.out.println("writing item id: " + item.getId());     
    System.out.println(item);

}
//getting stepExecution by implementing StepExecutionListener
this.stepExecution.getExecutionContext().putInt("writecursor", ++writecursor);

}

Now, in the customItemReadListener, I want to get the value of writecursor update, and then skip the lines above to start reading from writecursor

public class CustomItemReaderListener implements ItemReadListener<Report>, StepExecutionListener {

    ApplicationContext context = ApplicationContextUtils.getApplicationContext();
    private StepExecution stepExecution;
    @Override
    public void beforeRead() {
        //Skip lines somehow
    }

, , - linestoskip itemreader. http://thisisurl.com/dynamic-value-for-linestoskip-in-itemreader, . , http://forum.spring.io/forum/spring-projects/batch/100950-accessing-job-execution-context-from-itemwriter

+4
2

FlatFileItemReader.linesToSkip .

<bean id="myReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
    <property name="linesToSkip" value="file:#{jobParameters['cursor']}" />
</bean>
+4

:

xml, , .

public class CustomStepListener implements StepExecutionListener {

    @Autowired
    @Qualifier("cvsFileItemReader")
    FlatFileItemReader cvsFileItmRdr;

    @Override
    public void  beforeStep(StepExecution stepExecution) {
        System.out.println("StepExecutionListener -------- beforeStep");            
        cvsFileItmRdr.setLinesToSkip(4);
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println("StepExecutionListener - afterStep");
        return null;
    }

}

.....

+1

Source: https://habr.com/ru/post/1524672/


All Articles