2011-11-28 122 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没有做更早。谢谢。 –