如何在 Akka 延迟 10 秒后从文件中读取每一行?
How to read each new line from file after 10 second delay with Akka?
我正在尝试逐行读取文件,但有一些延迟。
例如:
- 读取并打印文件的第一行
- 等待 10 秒
- 读取并打印文件的第二行
- 等等
到目前为止我已经尝试过这样的事情:
final Path filePath = Paths.get('path/to/file');
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
Sink<ByteString, CompletionStage<Done>> printlnSink =
Sink.<ByteString>foreach(chunk -> System.out.println(chunk.utf8String()));
final CompletionStage<IOResult> result =
FileIO.fromPath(filePath)
.throttle(1, Duration.create(10, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
.to(printlnSink)
.run(materializer);
但是这个一次爆发太多行而不是一次爆发一行。
有什么解决办法吗?
final CompletionStage<IOResult> result =
.via(Framing.delimiter(ByteString.fromString(System.lineSeparator()),10000,FramingTruncation.ALLOW))
FileIO.fromPath(filePath)
.throttle(1, Duration.create(10, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
.to(printlnSink)
.run(materializer);
看看这是否适合你,framing Class 文件应该澄清
我正在尝试逐行读取文件,但有一些延迟。
例如:
- 读取并打印文件的第一行
- 等待 10 秒
- 读取并打印文件的第二行
- 等等
到目前为止我已经尝试过这样的事情:
final Path filePath = Paths.get('path/to/file');
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
Sink<ByteString, CompletionStage<Done>> printlnSink =
Sink.<ByteString>foreach(chunk -> System.out.println(chunk.utf8String()));
final CompletionStage<IOResult> result =
FileIO.fromPath(filePath)
.throttle(1, Duration.create(10, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
.to(printlnSink)
.run(materializer);
但是这个一次爆发太多行而不是一次爆发一行。
有什么解决办法吗?
final CompletionStage<IOResult> result =
.via(Framing.delimiter(ByteString.fromString(System.lineSeparator()),10000,FramingTruncation.ALLOW))
FileIO.fromPath(filePath)
.throttle(1, Duration.create(10, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
.to(printlnSink)
.run(materializer);
看看这是否适合你,framing Class 文件应该澄清