0
votes

i'm triyng to retry spring batch FlatFileItemReader but no success.

    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();

    Resource resource = new FileSystemResource("input/1-101-D-2017-212-volume-per-transaction.csv");

    try {
        resource.contentLength();
    } catch (IOException e) {
        e.printStackTrace();

    }

    reader.setResource(resource);
    reader.setRecordSeparatorPolicy(new BlankLineRecordSeparatorPolicy());

    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
    reader.setLineMapper(lineMapper);
    reader.setStrict(false);
    reader.setLinesToSkip(NUMBER_OF_HEADER_LINES);

    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    reader.setSkippedLinesCallback(line -> tokenizer.setNames(line.split(",")));
    lineMapper.setLineTokenizer(tokenizer);

    lineMapper.setFieldSetMapper(new TransactionFieldSetMapper());
    reader.setLineMapper(lineMapper);

    return reader;

then into my step i have

        .faultTolerant()
        .retryLimit(3)
        .retry(FileNotFoundException.class)

can someone give my some hint how can retry it?

1
The RetryPolicy in a chunk oriented step is not applied to the reader. Please check this answer for more details: stackoverflow.com/a/54903275/5019386Mahmoud Ben Hassine

1 Answers

0
votes

Hopefully it's still useful for you, please refer to this example where I put an example of the retry reader using FlatFileItemReader: https://github.com/jeronimogalicia/batch-retry-flatfile-item-reader

Basically you have to annotate your application class with @EnableRetry and annotate your reader like this:

@Bean
@StepScope
@Retryable(include = { ItemStreamException.class }, maxAttempts = 5)
ItemReader<Player> loadRecordsReader() throws Exception {

    String filePath = "src/main/resources/players.csv";
    System.out.println("Loading records from "  + filePath + " try " + counter);

    FlatFileItemReader<Player> itemReader = new FlatFileItemReader<>();
    itemReader.setResource(new FileSystemResource(filePath));
    itemReader.setLinesToSkip(1);
    //DelimitedLineTokenizer defaults to comma as its delimiter
    DefaultLineMapper<Player> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(new DelimitedLineTokenizer());
    lineMapper.setFieldSetMapper(new PlayerFieldSetMapper());
    itemReader.setLineMapper(lineMapper);
    itemReader.open(new ExecutionContext());
    return itemReader;
}