安排Akka的竣工阶段
我正在编写管理CompletionStage的实用程序,需要使用Akka安排CompletionStage。有没有办法给Akka一个CompletionStage并让它稍后运行 我通常使用这样的演员:安排Akka的竣工阶段,akka,Akka,我正在编写管理CompletionStage的实用程序,需要使用Akka安排CompletionStage。有没有办法给Akka一个CompletionStage并让它稍后运行 我通常使用这样的演员: 类MyActor扩展了UntypedActor{ public void onReceive(对象消息)引发异常{ doTheWork(); } } final-ActorRef=system.actorOf(Props.create(MyActor.class,this)); system.sc
类MyActor扩展了UntypedActor{
public void onReceive(对象消息)引发异常{
doTheWork();
}
}
final-ActorRef=system.actorOf(Props.create(MyActor.class,this));
system.scheduler().schedule(Duration.Zero(),ref,“Test”,system.dispatcher(),null);
有没有一种方法可以让Akka运行CompletionStage,而无需在actor中明确完成该阶段,如下所示:
类MyActor扩展了UntypedActor{
public void onReceive(对象消息)引发异常{
toCompletableFuture().get();
}
}
我认为对这里正在进行的CompletionStage
API有一点误解,completion stage不在任何地方运行,可能会有逻辑在某些线程上运行来完成它,可能会在完成时触发回调,这些回调也会在某些线程上运行
以下是一些与使用Akka dispatcher进行实际执行的CompletableFuture
/CompletionStage
交互的示例:
final CompletableFuture<Integer> futureInt = new CompletableFuture<>();
// right away but on the Akka default dispatcher
system.dispatcher().execute(() -> {
futureInt.complete(5);
});
// or later using the Akka scheduler
system.scheduler().scheduleOnce(FiniteDuration.create(5, TimeUnit.SECONDS), () -> {
futureInt.complete(6);
}, system.dispatcher());
// run some logic _when_ the completion stage is completed
futureInt.thenAcceptAsync((n) -> {
System.out.println("futureInt completed with value " + n);
}, system.dispatcher());
final completablefutureint=new CompletableFuture();
//马上,但在Akka默认调度程序上
system.dispatcher()执行(()->{
futureInt.complete(5);
});
//或者以后使用Akka调度程序
system.scheduler().scheduleOnce(FiniteDuration.create(5,TimeUnit.SECONDS))()->{
futureInt.complete(6);
},system.dispatcher());
//完成阶段完成后运行一些逻辑
futureInt.ThenAcceptsSync((n)->{
System.out.println(“futureInt完成,值为“+n”);
},system.dispatcher());