I have a Spring boot application:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class LoadApplication { public static void main(String[] args) { SpringApplication.run(LoadApplication.class, args); } }
In the @Configuration class, also annotated with @EnableBatchProcessing , I have the following bean batch job:
@Bean public Job loadJob(JobBuilderFactory jobs, Step stepLoadFile, Step stepArchiveFile) { return jobs.get("loadJob") .incrementer(new RunIdIncrementer()) .start(stepLoadFile) .next(stepArchiveFile) .build(); }
The stepLoadFile job stepLoadFile reads a flat file (see below) and writes the file data to the database. stepArchiveFile then moves the file to another directory.
Typically, work should be done once a day (Tuesday - Saturday) at the indicated time. However, if the flat file is not found, the task does not work, and it must be restarted every 30 minutes until it is completed or the maximum number of attempts is reached (for example, 5). After a successful restart, the job should not start again until the next normal run time. In addition, the system should ideally prevent parallel runs of the same job. How can all this be done?
Note. Repeat playback is not required to get where the previous job launch ended. This is because the block size is larger than the number of elements in the file.
I tried this in my @Configuration class (Note: I also added @EnableRetry to the configuration and main class):
@Bean public ItemReader<Test> reader(LineMapper<Test> lineMapper, ApplicationProperties properties) { FlatFileItemReader<Test> flatFileItemReader = new FlatFileItemReader<Test>() { @Override @Retryable(value = {ItemStreamException.class}, maxAttempts=5) public void open(ExecutionContext executionContext) throws ItemStreamException { super.open(executionContext); } @Override @Retryable(maxAttempts=5) public Holding read() throws UnexpectedInputException, ParseException, Exception { return super.read(); } }; flatFileItemReader.setResource(new FileSystemResource(properties.getLoadFile())); flatFileItemReader.setLineMapper(lineMapper); return flatFileItemReader; }
ItemStreamException thrown and the application exits without ItemStreamException .