0

I have the following ItemReader:

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class MyReader extends FlatFileItemReader<Holding> {

    @Autowired
    public MyReader(LineMapper<Holding> lineMapper, File loadFile) {
        setResource(new FileSystemResource(loadFile));
        final int NUMBER_OF_HEADER_LINES = 1;
        setLinesToSkip(NUMBER_OF_HEADER_LINES);
        setLineMapper(lineMapper);
    }

    @Override
    @Retryable(value=ItemStreamException.class, maxAttempts=5,  backoff=@Backoff(delay=1800000))
    public void open(ExecutionContext executionContext) throws ItemStreamException {
                super.open(executionContext);
    }
}

After the 5th attempt, ItemStreamException is thrown if the loadFile does not exist. How can I catch this exception?

I want to handle this exception by sending an email to an admin. I've tried surrounding jobLauncher.run(job, jobParameters); with the try catch clause but it doesn't work.

James
  • 2,876
  • 18
  • 72
  • 116

2 Answers2

1

Configure a retry interceptor as a bean and specify it in the interceptor property; there's a RetryInterceptorBuilder to help with assembling the bean.

You can add a recoverer to the interceptor which is called after retries are exhausted.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
0

You can apply skippable-exception-classes for your reader so Spring Batch will not fail the job.

For sending email in case failed, I would suggest you for with Job Listener instead of try/catch.

Nghia

Nghia Do
  • 2,588
  • 2
  • 17
  • 31