Play 2.0 Pipe向另一位演员承诺

时间:2012-06-18 06:08:40

标签: playframework-2.0 akka

在Akka中,你可以将未来传播给演员。如何在Play 2.0的承诺下完成?

http://doc.akka.io/docs/akka/current/scala/actors.html#Ask__Send-And-Receive-Future

final Future<Result> transformed = aggregate.map(new Mapper<Iterable<Object>, Result>() {
  public Result apply(Iterable<Object> coll) {
    final Iterator<Object> it = coll.iterator();
    final String s = (String) it.next();
    final int x = (Integer) it.next();
    return new Result(x, s);
  }
});

pipe(transformed).to(actorC);

1 个答案:

答案 0 :(得分:1)

您需要将Future<Result>包装在Promise<Result>中,以便在Play中的异步请求处理程序中使用它。这是一个例子:

public static F.Promise<Result> foo() {

    ActorRef fooActor = Akka.system().actorOf(Props.create(FooActor.class));

    Future<Object> response = ask(fooActor, "message", 5000);

    Future<Result> result = response.map(new Mapper<Object, Result>() {
        @Override
        public Result apply(Object message) {
            return ok("whatever");
        }
    }, HttpExecution.defaultContext());

    return F.Promise.wrap(result);
}