2017-03-06 80 views
0

文檔顯示的附加器的使用或一個通常與一個lambda零售商,像這樣:紀事隊列:具有較少使用或不lambda表達式

appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> 
     m.write("mkey").text(mkey) 
     .write("timestamp").dateTime(now) 
     .write("msg").text(data))); 

對於零售商II使用:

int count = 0; 
    while (read from tailer) { 
     wire.read("log").marshallable(m -> { 
      String mkey = m.read("mkey").text(); 
      LocalDateTime ts = m.read("timestamp").dateTime(); 
      String bmsg = m.read("msg").text(); 
     //... do more stuff, like updating counters 
      count++; 
     } 
    } 

在閱讀過程中,我想做更新計數器等工作,但這在lambda中是不可能的(需要「有效的最終」值/對象)。

  • 在沒有lambda表達式的情況下使用API​​的最佳做法是什麼?
  • 關於如何做到這一點的任何其他想法? (目前我使用AtomicInteger對象)

回答

2
static class Log extends AbstractMarshallable { 
    String mkey; 
    LocalDateTime timestamp; 
    String msg; 
} 

int count; 

public void myMethod() { 
    Log log = new Log(); 
    final SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(new File("q4")).build(); 
    final ExcerptAppender appender = q.acquireAppender(); 
    final ExcerptTailer tailer = q.createTailer(); 

    try (final DocumentContext dc = appender.writingDocument()) { 
     // this will store the contents of log to the queue 
     dc.wire().write("log").marshallable(log); 
    } 

    try (final DocumentContext dc = tailer.readingDocument()) { 
     if (!dc.isData()) 
      return; 
     // this will replace the contents of log 
     dc.wire().read("log").marshallable(log); 
     //... do more stuff, like updating counters 
     count++; 
    } 
}