2013-07-01 14 views
1

我想讀取讀取文本文件以構建映射並將其放入ExecutionContext以供以後參考。如何使用FlatFileReader讀取,但只寫入ExecutionContext

我以爲開始使用chunk-processng來讀取文件,它的過程,但我不需要FlatFileItemWriter來寫入文件。但是,初始化bean需要我在writer上設置一個資源。

我是否在談論這個錯誤?大塊=處理錯誤的方法。創建一個tasklet我更聰明,但我喜歡SpringBatch會爲我讀取我的文件。有了一個tasklet,我不得不編寫代碼來打開和處理文本文件。對?

關於如何進行的建議將不勝感激。

回答

0

我最終做了什麼(我是新人)創建了一個Tasklet,並讓它實現了StepExecutionListener接口。像魅力一樣工作。它正在用線條讀取逗號分隔的文件,拔掉第二列。我爲我的ExecutionContext映射鍵創建了一個'枚舉'。基本上如下:

public class ProcessTabcPermitsTasklet implements Tasklet, StepExecutionListener { 

    private Resource resource; 
    private int linesToSkip; 
    private Set<String> permits = new TreeSet<String>(); 

    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { 

     BufferedReader reader = new BufferedReader((new FileReader(resource.getFile()))); 
     String line = null; 
     int lines = 0; 

     while ((line = reader.readLine()) != null) { 

      if (++lines <= linesToSkip) 
       continue; 

      String[] s = StringUtils.commaDelimitedListToStringArray(line); 
      permits.add(s[TABC_COLUMNS.PERMIT.ordinal()]); 

     } 

     return RepeatStatus.FINISHED; 
    } 

    /** 
    * @param file 
    *   the file to set 
    */ 
    public void setResource(Resource resource) { 
     this.resource = resource; 
    } 

    /** 
    * @param linesToSkip 
    *   the linesToSkip to set 
    */ 
    public void setLinesToSkip(int linesToSkip) { 
     this.linesToSkip = linesToSkip; 
    } 


    public ExitStatus afterStep(StepExecution stepExecution) { 
     stepExecution.getExecutionContext().put(EXECUTION_CONTEXT.TABC_PERMITS.toString(), permits); 
     return ExitStatus.COMPLETED; 
    } 

}