2011-11-28 116 views
1

我正在嘗試使用Spring批處理並實現一個聚合閱讀器(批處理文件,其中多個記錄在寫入時應該被視爲一個記錄)。下面是我的讀者的代碼片斷:Spring批處理:Aggregated reader/writer問題

public class AggregatePeekableReader implements ItemReader<List<T>>, ItemStream { 



    private SingleItemPeekableItemReader<T> reader; 



    private boolean process(T currentRecord , InvoiceLineItemsHolder holder) throws UnexpectedInputException, ParseException, Exception { 

     next = peekNextInvoiceRecord(); 

     // finish processing if we hit the end of file 
     if (currentRecord == null) { 
       LOG.info("Exhausted ItemReader (END OF FILE)"); 
       holder.exhausted = true; 
       return false; 
     } 

     if (currentRecord.hasSameInvoiceNumberAndVendorNumber(next)){ 
       LOG.info("Found new line item to current invocie record"); 
       holder.records.add(currentRecord); 
       currentRecord = null; 
       return true; 
     }else{ 

      holder.records.add(currentRecord); 
       return false;   
     } 

} 

    private T getNextInvoiceRecord() { 

     T record=null; 

     try { 
      record=reader.read(); 
     } catch (UnexpectedInputException e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 
      throw e; 
     } catch (ParseException e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 
      throw e; 
     } catch (Exception e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 


     } 

     return record; 
    } 

    private T peekNextInvoiceRecord() { 

     T next=null; 

     try { 
      next=reader.peek(); 
     } catch (UnexpectedInputException e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 
      throw e; 
     } catch (ParseException e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 
      throw e; 
     } catch (Exception e) { 
      ALERT.error(LogMessageFormatter.format(Severity.HIGH, 
        BATCH_FILE_READ_EXCEPTION, e), e); 
     } 
     return next; 
    } 

    public void close() { 
     reader.close(); 
    } 

    public SingleItemPeekableItemReader<T> getReader() { 
     return reader; 
    } 


    public void setReader(SingleItemPeekableItemReader<T> reader) { 
     this.reader = reader; 
    } 

    private class InvoiceLineItemsHolder { 
     List<T> records = new ArrayList<T>(); 

     boolean exhausted = false; 
} 


    @Override 
    public void open(ExecutionContext executionContext) throws ItemStreamException { 
     // 
     reader.open(executionContext); 

    } 

    @Override 
    public void update(ExecutionContext executionContext) throws ItemStreamException { 
     // TODO 

    } 

    @Override 
    public List<T> read() throws Exception, UnexpectedInputException, ParseException, 
      NonTransientResourceException { 
     CLASS holder = new SOMECLASS() 

     synchronized (this) { 

      while (process(getNextInvoiceRecord(), holder)) { 
       continue; 
      } 
      if (!holder.exhausted) { 

       return holder.records; 
      } else { 
       //When you hit the end of the file,close the reader. 
       close(); 
       return null; 
      } 

     } 

    } 

}

以上是用於實現peekable reader.This一個工作示例偷窺下一行 (犯規讀它),並確定是否一個邏輯端在達到行(有時 多條線路可以彌補單個事務)

+0

一般而言,我認爲我們的代碼太少,無法提供明智的建議。此外,你說你已經嘗試了幾件事情 - 它可能有助於描述你的一些嘗試解決方案。 – david

回答

1

您需要實現ItemStream接口讀者deleteTaskletStep流。這會給一個提示Spring Batch的,你的讀者需要一些動作來打開/關閉流:

public class InvoiceLineItemAggregatePeekableReader extends AbstractItemStreamItemReader<List<SAPInvoicePaymentRecord>> { 

    @Override 
    public void close() { 
    ... 
    } 
} 

流關閉任何一步執行過程中出現錯誤。有關更多示例,請查看Spring Batch本身的類(例如FlatFileItemReader)。

+0

我想通了,並能夠得到它的工作。謝謝。 –

0

因爲讀者 沒有關閉

我不能輸入文件移動到一個文件夾時出錯

您可以複製該文件並在舊文件上使用File.deleteOnExit()以供日後刪除,或在額外步驟中刪除舊文件,例如,用一個簡單的微進程和調用只有當企業一步都得異常

+0

在這裏放下全班。 –

+0

好吧,我得到了這個工作。我的老方法是錯的。有實施ItemStream.I沒有做更早。謝謝。 –