10

I want to be able to delete an item while processing it if it fits a specific logic. For example, if the item doesn't contain a value I'm looking for, I don't want to that item to be written out to the file.

I'm currently using a class which implements ItemProcessor<T,T>.

Do I just return null?

samwell
  • 2,757
  • 9
  • 33
  • 48
  • 6
    [Returning null should be good](http://docs.spring.io/spring-batch/trunk/reference/html/domain.html#domainItemProcessor) – Paul Samsotha Jun 26 '14 at 15:50
  • So would that cause any null pointers anywhere? – samwell Jun 26 '14 at 15:56
  • 4
    AFAIK, generally, no. The null just indicates to the writer not to write – Paul Samsotha Jun 26 '14 at 16:02
  • A better design would be to implement validator on your model; if validation fails, the record will be skipped and no further processing would be done. – Barun Jun 27 '14 at 09:29
  • @Barun I only know while the item is being processed if I need to skip the item or not. From what I understand of `ValidatingItemProcessor` it will only check the item and if needs to be skipped an exception is thrown and the processor will know it should skip the item. Is there something else that validates while I process an item? – samwell Jun 27 '14 at 13:40
  • @samwell No, there is nothing else that validates other than your Processors(Processors can be chained too). – Barun Jul 01 '14 at 05:54

1 Answers1

12

Return null from the processor like below.

public class ExceptionRecordValidator implements 
        ItemProcessor<yourDTO, yourDTO>{
@Override
public yourDTO process(
        yourDTOitem) throws Exception {
     if(your requested value found){
         return yourDTO ;
     }else{
         return null;
     }
  }
}