简体   繁体   English

Akka Java文件IO限制

[英]Akka Java File IO Throttling

I want to stream file content line by line to Actor. 我想逐行将文件内容流式传输到Actor。 I have something like that: 我有这样的事情:

final ActorSystem system = ActorSystem.create("stream_system");
final Materializer materializer = ActorMaterializer.create(system);
final ActorRef actor = system.actorOf(Props.create(streamActor.class), "sink");

final Path file = Paths.get("path/file.txt");

Sink<ByteString, CompletionStage<Done>> printlnSink =
        Sink.<ByteString> foreach(chunk -> actor.tell(chunk.utf8String(), null));
        //Sink.<ByteString> actorRef(actor, null);

CompletionStage<IOResult> ioResult =
        FileIO.fromPath(file)
                .throttle(1, Duration.create(1, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
                .to(printlnSink)
                .run(materializer);

Uncommented version works, but it streams whole file content in one go. 未注释版本可以使用,但可以一次性流式传输整个文件内容。 Commented version ends up with "unknown" messages. 带注释的版本以“未知”消息结尾。

I want to send line by line to Actor with few seconds delay. 我想几秒钟延迟地逐行发送给Actor。 Any help how to accomplish this? 任何帮助如何实现这一目标? Receiving actor just take String message and print it on output. 接收actor只需获取String消息并将其打印在输出上。

The Framing class can help you with this: Framing类可以帮助您:

CompletionStage<IOResult> ioResult =
    FileIO.fromPath(file)
          .via(Framing.delimiter(ByteString.fromString(System.lineSeparator()), 1000, FramingTruncation.ALLOW))
          .throttle(1, Duration.create(1, TimeUnit.SECONDS), 1, ThrottleMode.shaping())
          .to(printlnSink)
          .run(materializer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM