Java 如何从Akka提取实时FileIO状态?

Java 如何从Akka提取实时FileIO状态?,java,akka,akka-stream,Java,Akka,Akka Stream,我正在使用Akka制作一个文件传输系统。我看这些文件已经有一段时间了。当前进度状态为Actor2收到Actor1发送的文件并将其写入Actor2的本地系统(Actor1=发送方,Actor2=接收方) 但我找不到一种方法来知道我在写作时实时收到了多少字节 我对它进行了测试,结果表明,使用runWithAPI,可以在本地编写文件。使用runForeachAPI,可以实时通过发送多少字节。但是,如果同时创建这两个文件,则无法写入该文件 这是我的简单来源。请给我一些建议 public static B

我正在使用Akka制作一个文件传输系统。我看这些文件已经有一段时间了。当前进度状态为Actor2收到Actor1发送的文件并将其写入Actor2的本地系统(Actor1=发送方,Actor2=接收方)

但我找不到一种方法来知道我在写作时实时收到了多少字节

我对它进行了测试,结果表明,使用
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();
    });
}
公共静态行为创建(){
返回行为。设置(上下文->{
context.getLog().info(“向接待员注册”);
context.getSystem().receignister().tell(receignister.register(RECEIVER_SERVICE_KEY,context.getSelf().slow());
Materializer mat=Materializer.createMaterializer(上下文);
返回行为.receive(Command.class)
.onMessage(TransferFile.class,命令->{
command.sourceRef.getSource().runWith(FileIO.toPath(path.get(“test.pptx”)),mat);
//command.replyTo.tell(新文件传输(“filename”,1024));
command.sourceRef.getSource().runForeach(f->System.out.println(f.size()),mat);
返回行为。相同();
}).build();
});
}
使用允许多个使用者使用您的
源代码

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);
Source fileSource=command.sourceRef.getSource();
RunnableGraph RunnableGraph=
toMat(BroadcastHub.of(ByteString.class,256),Keep.right();
//根据需要调整缓冲区大小(256)
sourcefromfilesource=runnableGraph.run(mat);
fromFileSource.runWith(FileIO.toPath(path.get(“test.pptx”)),mat);
fromFileSource.runForeach(f->System.out.println(f.size()),mat);
使用允许多个使用者使用您的
源代码

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);
Source fileSource=command.sourceRef.getSource();
RunnableGraph RunnableGraph=
toMat(BroadcastHub.of(ByteString.class,256),Keep.right();
//根据需要调整缓冲区大小(256)
sourcefromfilesource=runnableGraph.run(mat);
fromFileSource.runWith(FileIO.toPath(path.get(“test.pptx”)),mat);
fromFileSource.runForeach(f->System.out.println(f.size()),mat);

BroadcastHub
正如Jeffrey所建议的那样,允许单个正在运行的流连接到多个随时间启动和停止的其他流

拥有一个动态连接到其他流的流在内部需要相当多的额外环,因此如果您不需要,最好避免这种开销

如果您想使用一个源代码和两个接收器,那么最好使用
source.alsoTo(sink1.to(sink2)


操作员支持flow API中的
alsoTo
,但直接使用它需要使用图形DSL

正如Jeffrey所建议的那样,BroadcastHub允许单个正在运行的流连接到多个随时间启动和停止的其他流

拥有一个动态连接到其他流的流在内部需要相当多的额外环,因此如果您不需要,最好避免这种开销

如果您想使用一个源代码和两个接收器,那么最好使用
source.alsoTo(sink1.to(sink2)


操作员支持flow API中的
alsoTo
,但直接使用它需要使用图形DSL

谢谢大家!!我用你告诉我的代码应用了它,我想要的函数正好工作!你是自己了解阿克卡的吗?哈哈:)我尊重并感谢你作为一个阿卡的初学者。谢谢你!我用你告诉我的代码应用了它,我想要的函数正好工作!你是自己了解阿克卡的吗?哈哈:)我尊敬并感谢你,因为你是阿卡的初学者。我没有考虑过开销。谢谢你的建议:)但是如果我想按照你的建议去做,我不知道如何改变它。您能给我一些建议吗?比如,
command.sourceRef.getSource().alsoTo(Sink.foreach(…println)).runWith(FileIO…
您可能还应该查看文件操作符的物化值,以便注意是否存在异常。我没有考虑开销。谢谢你的建议:)但是如果我想按照你的建议去做,我不知道如何改变它。您能给我一些建议吗?比如,
command.sourceRef.getSource().alsoTo(Sink.foreach(…println)).runWith(FileIO…
您可能还应该查看文件操作符的物化值,以便注意是否存在异常。