Page History
...
Code Block | ||||
---|---|---|---|---|
| ||||
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class EventStorageApp { public static void main(String[] args) throws InterruptedException { List<String> events = new ArrayList<>(); final Duration collectDuration = Duration.ofSeconds(3); final Duration writeDuration = Duration.ofMillis(10); Instant end; while (true) { Iterator<String> eventIterator = eventIterator(); end = Instant.now().plus(collectDuration); // 3초간 이벤트를 모읍니다. while (eventIterator.hasNext() && Instant.now().isBefore(end)) { events.add(eventIterator.next()); } if (Instant.now().isAfter(end)) { // 3초가 지나면 이벤트를 저장합니다. try (BufferedWriter writer = new BufferedWriter(new FileWriter("/path/to/file"))) { for (String event : events) { writer.write(event); writer.newLine(); } events.clear(); } catch (IOException e) { e.printStackTrace(); } } Thread.sleep(writeDuration.toMillis()); } } private static Iterator<String> eventIterator() { // 이벤트를 생성하는 코드 return Stream.generate(UUID::randomUUID) .map(UUID::toString) .iterator(); } } |
정리
...
ChatGPT가 만족할만한 코드를 작성을 못해서.. 작성 못해, 이후 사용할코드를 직접 작성하였습니다.
고성능 비동기 배치 스트림처리코드 : java-labs/SimpleActorTest.java at master · psmon/java-labs (github.com)
...