Home > database >  How to skip whole step if file do not exist for that item reader or step
How to skip whole step if file do not exist for that item reader or step

Time:11-18

ISSUE:> I am having item reader writer processor for my spring batch job. And when I am running it, it is giving me error Failed to initialize the reader and also showing :> Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): file

My requirement is: I want my item reader to not run if file does not exists.

Can anyone help me: I want to skip whole step if file do not exist for that item reader. My step consist of (item reader, processor and writer).

Just tell me how to skip step, if file do not exist. Any help will be appreciated.And is there anyway to set reader to non strict mode.

CodePudding user response:

See here: You can set skip condition (and limit) on a "chunk/step".

@Bean
public Step step1() {
  return this.stepBuilderFactory.get("step1")
    .<String, String>chunk(CHUNK_SIZE)
    .reader(flatFileItemReader())
    .writer(itemWriter())
    .faultTolerant()
    .skipLimit(0) // 0: dont re-try
    .skip(FlatFileParseException.class) // when you use FlatFileItemWriter,
      // but other exception (type) possible, e.g. IllegalStateException.class
    .build();
}

To set "non-strict" mode in FlatFileItemReader, just:

@Bean
public FlatFileItemReader<XY> flatFileItemReader() {

  FlatFileItemReader<XY> reader = new FlatFileItemReader<>();
  //!
  reader.setStrict(false);
  // reader.setXYZ ...
  return reader;
}
  • Related