I am trying to validate some fields of User with Validator. I am not able to catch the error in ItemProcessListener.
My validator is like so.
public class UserValidator implements Validator<User> {
@Override
public void validate(User user) throws ValidationException {
if (user.getName().length() > 3) {
throw new ValidationException("User name cannot have more than 3 alphabets");
}
}
}
I have Skiplistener like so..
public class UserValidationListener implements SkipListener<User, User> {
@Override
public void onSkipInRead(Throwable throwable) {
System.out.println(throwable.getMessage());
}
@Override
public void onSkipInWrite(User user, Throwable throwable) {
System.out.println(user.toString());
System.out.println(throwable.getMessage());
}
@Override
public void onSkipInProcess(User user, Throwable throwable) {
System.out.println(user.toString());
System.out.println(throwable.getMessage());
// No error message comes here
// 1. get the error message thrown by Validation exception
// 2. Save it db logic
}
}
In the batch job config I have created a bean to filter out the faulty user data.
@Bean
public ValidatingItemProcessor<User> validatingItemProcessor() {
ValidatingItemProcessor<User> itemProcessor = new ValidatingItemProcessor<>(new UserValidator());
itemProcessor.setFilter(true);
return itemProcessor;
}
And finally here is my batch job config.
Step step = stepBuilderFactory.get("ETL-file-load")
.<User, User>chunk(100)
.reader(itemReader)
.processor(validatingItemProcessor())
.writer(itemWriter)
.faultTolerant()
.skip(ValidationException.class)
.listener(userValidationListener())
.build();
return jobBuilderFactory.get("ETL-Load")
.incrementer(new RunIdIncrementer())
.start(step)
.build();
I have created a bean for listener like so:
@Bean
public UserValidationListener userValidationListener() {
return new UserValidationListener();
}
I am not able to catch the error message thrown by the UserValidator with ValidationException. How do I do that?