簡體   English   中英

如何從 Akka 中提取實時 FileIO state?

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

我正在使用 Akka 制作文件傳輸系統。 我已經看了一段時間的文件。 當前的進度狀態是Actor2收到Actor1發送的文件並寫入Actor2的本地系統(Actor1=sender,Actor2=receiver)。

但是我找不到一種方法來知道我在寫作時實時收到了多少字節。

我測試了一下,結果證明,用runWith API,可以在本地寫入文件。 使用runForeach API,實時傳遞了多少字節。 但是,如果這兩個同時創建,則無法寫入文件。

這是我的簡單來源。 請給我一些建議。

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();
    });
}

使用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);

根據 Jeffrey 的建議, BroadcastHub允許單個運行的 stream 連接到隨時間啟動和停止的多個其他流。

擁有一個動態連接到其他人的 stream 需要在內部有很多額外的箍,所以如果你不需要,最好避免這種開銷。

如果您的用例是您想要使用帶有兩個接收器的單個源,最好使用source.alsoTo(sink1).to(sink2)來完成。

也To in the flow alsoToBroadcast運算符支持,但直接使用它需要您使用 Graph DSL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM