0

I am working on a Spring Batch application that reads some files and persists it to DB. I am handling error conditions using the Skip Policy. How do I get access to the file name that is read in the reader ?

public FileProcessSkipper(int skipCount) {
        this.configuredSkipCount = skipCount;
    }

@Override
public boolean shouldSkip(Throwable exception, int skipCount) throws SkipLimitExceededException {

        if (exception instanceof FileNotFoundException) {
            return false;
        } else if (exception instanceof FlatFileParseException && skipCount < configuredSkipCount) {
            FlatFileParseException ffpe = (FlatFileParseException) exception;
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("An error occured while processing the " + ffpe.getLineNumber()
                + " line of the file. Below was the faulty " + "input.\n");
            errorMessage.append(ffpe.getInput() + "\n");
            log.error("{}", errorMessage.toString());
  
            return true;
        } else if (exception instanceof DateTimeParseException && skipCount < configuredSkipCount) {
            DateTimeParseException ffpe = (DateTimeParseException) exception;
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("An error occured while processing the Record. Below was the faulty input.\n");
            errorMessage.append(ffpe.getParsedString() + "\n");
            log.error("{}", errorMessage.toString());
            return true;
        } else if (exception instanceof DataIntegrityViolationException && skipCount < configuredSkipCount) {
            DataIntegrityViolationException dive = (DataIntegrityViolationException) exception;
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("An error occured while loading Record into the database Below was the faulty " + "input.\n");
            errorMessage.append(dive.getMessage() + "\n");
            log.error("{}", errorMessage.toString());

            return true;
        } else {
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("Error Count exceeded threshold configures skip count "+configuredSkipCount+" and the Job is being failed.\n");
            
            
            
            return false;
        }
    }
Rakesh
  • 658
  • 6
  • 15
Siva
  • 1
  • Can you try this solution? https://stackoverflow.com/questions/66474112/access-job-filename-parameter-in-skippolicy-of-spring-batch – Girish Mar 05 '21 at 06:04
  • Thank you, these comments work. Thanks and regards, – Siva Mar 10 '21 at 04:53

0 Answers0