我有這個任務來處理輸入.csv,.txt文件並將數據存儲到數據庫中。我正在使用Spring Batch來達到這個目的。在將數據轉儲到數據庫之前,我必須對數據執行一些驗證檢查。我使用Spring Batch的ValidatingItemProcessor
和Hibernate的JSR-303
參考實現hibernate驗證器爲相同。代碼看起來是這樣的:獲取驗證失敗的對象Spring批量驗證
public class Person{
@Pattern(regexp = "someregex")
String name;
@NotNull
String address;
@NotNull
String age;
//getters and setters
}
然後我寫了一個驗證器看起來是這樣的 -
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.springframework.batch.item.validator.ValidationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.batch.item.validator.Validator;
class MyBeanValidator implements Validator<Person>, InitializingBean{
private javax.validation.Validator validator;
@Override
public void afterPropertiesSet() throws Exception {
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.usingContext().getValidator();
}
@Override
public void validate(Person person) throws ValidationException {
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(person);
if(constraintViolations.size() > 0) {
generateValidationException(constraintViolations);
}
}
private void generateValidationException(Set<ConstraintViolation<Object>> constraintViolations) {
StringBuilder message = new StringBuilder();
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
message.append(constraintViolation.getMessage() + "\n");
}
throw new ValidationException(message.toString());
}
然後我有一個子類的處理器Spring Batch的的ValidatingItemProcessor
。
public class ValidatingPersonItemProcessor extends ValidatingItemProcessor<Person>{
@Override
public Person process(Person person) {
//some code
}
將通過檢驗檢查的記錄將被傳遞到另一個處理器進行進一步的處理,但失敗的人將被清除,然後到下一個處理器通過。
現在我想抓住未通過驗證的記錄。我的目標是報告未通過驗證的所有輸入記錄,並在將這些記錄傳遞給下一個處理器以供進一步處理之前進一步清理這些記錄。我怎樣才能做到這一點?
如果某些輸入的驗證失敗,Spring批處理進程是否會終止?如果是的話,如何避免這種情況?我的處理器配置看起來像:
<batch:chunk reader="personItemReader" writer="personDBWriter" processor="personProcessor" commit-interval="100" skip-limit="100"> <batch:skippable-exception-classes> <batch:include class="org.springframework.batch.item.validator.ValidationException"/> </batch:skippable-exception-classes> <batch:listeners> <batch:listener> <bean class="org.someorg.poc.batch.listener.PersonSkipListener" /> </batch:listener> </batch:listeners> </batch:chunk> <bean id="personProcessor" class="org.springframework.batch.item.support.CompositeItemProcessor"> <property name="delegates"> <list> <ref bean="validatingPersonItemProcessor" /> <ref bean="personVerifierProcessor" /> </list> </property> </bean> <bean id="validatingPersonItemProcessor" class="org.someorg.poc.batch.processor.ValidatingPersonItemProcessor" scope="step"> <property name="validator" ref="myBeanValidator" /> </bean> <bean id="myBeanValidator" class="org.someorg.poc.batch.validator.MyBeanValidator"> </bean> <bean id="personVerifierProcessor" class="org.someorg.poc.batch.processor.PersonVerifierProcessor" scope="step"/> </beans>
可能的重複[如何創建一個報表篩選項目在春季批處理?](http://stackoverflow.com/questions/12999435/how-to-create-a-report-on-filtered-items- in-spring-batch) –
同時檢查http://stackoverflow.com/questions/27318466/spring-batch-how-to-filter-duplicated-items-before-send-it-to-itemwriter –
@LucaBassoRicci感謝您的評論。如何在ItemProcessor中將異常過濾爲SkipException?也正如你所看到的,我從我的驗證器而不是處理器產生異常。我應該改變這種行爲嗎? – raghav