SpringBatch - Get Line Number on FieldSetMapper

前端 未结 2 699
青春惊慌失措
青春惊慌失措 2020-12-21 20:21

I need to get the line number into the FieldSet Mapper. How can I do this?

I want to use the lineNumber as a field of my resulting object produced by the Mapper.

相关标签:
2条回答
  • 2020-12-21 20:39

    I think you can use spEL expression #{fileReader.currentItemCount}, but there is the SB interface ItemCountAware for this purpose.

    Marker interface indicating that an item should have the item count set on it. Typically used within an AbstractItemCountingItemStreamItemReader.

    0 讨论(0)
  • 2020-12-21 20:59

    I realized that I can get the lineNumber value into MyObject by overriding the DefaultLineMapper with my own LineMapper in this way:

    import org.springframework.batch.item.file.FlatFileParseException;
    import org.springframework.batch.item.file.LineMapper;
    import org.springframework.batch.item.file.transform.LineTokenizer;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.util.Assert;
    
    import my.model.MyObject;
    
    public class MyLineMapper<T> implements LineMapper<MyObject>, InitializingBean {
    
        private LineTokenizer tokenizer;
    
        private ResourceFieldSetMapper fieldSetMapper;
    
        public MyObject mapLine(String line, int lineNumber) throws Exception {
            try{
                MyObject r = fieldSetMapper.mapFieldSet(tokenizer.tokenize(line));
                // this is the modification
                r.setLineNumber(lineNumber);
                return r;
            }
            catch(Exception ex){
                throw new FlatFileParseException("Parsing error at line: " + lineNumber + 
                        ", input=[" + line + "]", ex, line, lineNumber); 
            }
        }
    
        public void setLineTokenizer(LineTokenizer tokenizer) {
            this.tokenizer = tokenizer;
        }
    
        public void setFieldSetMapper(ResourceFieldSetMapper fieldSetMapper) {
            this.fieldSetMapper = fieldSetMapper;
        }
    
        public void afterPropertiesSet() {
            Assert.notNull(tokenizer, "The LineTokenizer must be set");
            Assert.notNull(fieldSetMapper, "The FieldSetMapper must be set");
        }
    
    }
    

    Thanks for your help! I hope this works for someone!

    Blessings!

    0 讨论(0)
提交回复
热议问题