Results 1 to 2 of 2

Thread: Spring Batch Validation Framework - Looking for good Patterns

  1. #1
    Join Date
    Apr 2011
    Posts
    11

    Default Spring Batch Validation Framework - Looking for good Patterns

    Hi,

    I am reading data from CSV flat file using FlatFileItemReader and want to validate the input records and if any errors, I just want to log it and still continue with the other records in the file. Having said that, what would be the good pattern to include the validation layer.

    1) Include validation (custom) layer in InputFeedFieldSetMapper while reading the input from flat file and log the invalid records

    Code:
    	
    	<bean name="feedMapper" class="com.abc.batch.core.reader.mapper.InputFeedFieldSetMapper"/>
    
    <bean name="feedLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
             <property name="fieldSetMapper" ref="feedMapper" />
             <property name="lineTokenizer" ref="feedLineTokenizer" />
    	</bean>
    2) Include validation in Processing layer as shown below and use Errors.rejectValue etc. to log it in log file but I don't want to reject and just want to log it (no idea how to do it and more over if I use CompositeItemProcessor pattern, the subsequent processors won't be called if any Errors out of ValidatingItemProcessor)

    Code:
    	<bean id="feedValidator" class="com.abc.batch.core.reader.validator.impl.FileLoadValidator" />
    
        <bean id="validator" class="org.springframework.batch.item.validator.SpringValidator">
    	  <property name="validator" ref="feedValidator" />
    	</bean>
    
    			<bean id="validateFileLoadProcessor" class="org.springframework.batch.item.validator.ValidatingItemProcessor">
    			  <property name="validator" ref="validator" />	  
    			</bean>
    Please help me with this. Thanks.

  2. #2
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    366

    Default

    When you say you don't want to "reject" it, just log it, what do you mean? Do you still want it to go through the processors and be written? If that's the case, using option 2 would be your best bet. In your Validator implementation, just log the failing item and don't throw the ValidationException.
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •