简体   繁体   English

如何从 Akka 中提取实时 FileIO state?

[英]How do I extract the real-time FileIO state from Akka?

I am making a file transfer system using Akka.我正在使用 Akka 制作文件传输系统。 I've been looking at the documents for a while.我已经看了一段时间的文件。 The current status of progress is Actor2 received the file sent by Actor1 and wrote it to the local system of Actor2 (Actor1 = sender, Actor2 = receiver).当前的进度状态是Actor2收到Actor1发送的文件并写入Actor2的本地系统(Actor1=sender,Actor2=receiver)。

But I couldn't find a way to know how much byte I received in real time when writing.但是我找不到一种方法来知道我在写作时实时收到了多少字节。

I tested it, and it turns out, with runWith API, files can be written locally.我测试了一下,结果证明,用runWith API,可以在本地写入文件。 With runForeach API, how much byte was delivered in real time through.使用runForeach API,实时传递了多少字节。 However, if these two are created at the same time, the file cannot be written.但是,如果这两个同时创建,则无法写入文件。

Here's my simple source.这是我的简单来源。 Please give me some advice.请给我一些建议。

public static Behavior<Command> create() {
    return Behaviors.setup(context -> {
        context.getLog().info("Registering myself with receptionist");
        context.getSystem().receptionist().tell(Receptionist.register(RECEIVER_SERVICE_KEY, context.getSelf().narrow()));
        Materializer mat = Materializer.createMaterializer(context);

        return Behaviors.receive(Command.class)
                .onMessage(TransferFile.class, command -> {
                    command.sourceRef.getSource().runWith(FileIO.toPath(Paths.get("test.pptx")), mat);
                    //command.replyTo.tell(new FileTransfered("filename", 1024));
                    command.sourceRef.getSource().runForeach(f -> System.out.println(f.size()), mat);
                    return Behaviors.same();
                }).build();
    });
}

Use a BroadcastHub to allow multiple consumers of your Source :使用BroadcastHub允许Source的多个消费者:

Source<ByteString, NotUsed> fileSource = command.sourceRef.getSource();

RunnableGraph<Source<ByteString, NotUsed>> runnableGraph =
  fileSource.toMat(BroadcastHub.of(ByteString.class, 256), Keep.right());
// adjust the buffer size (256) as needed

Source<ByteString, NotUsed> fromFileSource = runnableGraph.run(mat);

fromFileSource.runWith(FileIO.toPath(Paths.get("test.pptx")), mat);
fromFileSource.runForeach(f -> System.out.println(f.size()), mat);

BroadcastHub as suggested by Jeffrey, allows for a single running stream to be connected to multiple other streams that are started and stopped over time.根据 Jeffrey 的建议, BroadcastHub允许单个运行的 stream 连接到随时间启动和停止的多个其他流。

Having a stream that dynamically connects to others requires quite a lot of extra hoops internally, so if you don't need that it is better to avoid that overhead.拥有一个动态连接到其他人的 stream 需要在内部有很多额外的箍,所以如果你不需要,最好避免这种开销。

If you use case is rather that you want to consume a single source with two sinks that is better done with source.alsoTo(sink1).to(sink2) .如果您的用例是您想要使用带有两个接收器的单个源,最好使用source.alsoTo(sink1).to(sink2)来完成。

alsoTo in the flow API is backed by the Broadcast operator, but using that directly requires that you use the Graph DSL.也To in the flow alsoToBroadcast运算符支持,但直接使用它需要您使用 Graph DSL。

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

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