代码之家  ›  专栏  ›  技术社区  ›  wireknight

playframework-2.3 Akka Async交互移植?

  •  0
  • wireknight  · 技术社区  · 10 年前

    我有一些与akka交互的旧的playframework 2.2 java web服务,现在我应该将它们移植到playframework 2.3。

    然而,异步已经被弃用,甚至在阅读了有关异步移植的文档之后( http://www.playframework.com/documentation/2.3.x/JavaAsync )我无法理解如何将其应用于我的案例(代码如下):

    • 在开始构建回复(ok())之前,我必须等待超时/akka服务器回复,否则我将阻塞线程。
    • 我也应该使actor选择异步。

    • 我也应该使akka服务器回复解析/回复构造异步

    我环顾四周,甚至在类型安全模板中也找不到这样的交互示例。

    我怎么能这样做?

    /* playframework 2.2 code */
    
    public class Resolve extends Controller {
    
        private final static String RESOLVER_ACTOR = play.Play.application().configuration().getString("actor.resolve");
    
        @CorsRest
        @VerboseRest
        @RequireAuthentication
        @BodyParser.Of(BodyParser.Json.class)
        public static Result getJsonTree() {
            JsonNode json = request().body().asJson();
    
            ProtoBufMessages.ResolveRequest msg;
            ResolveRequestInput input;
    
            try {
                input = new ResolveRequestInput(json);
            } catch (rest.exceptions.MalformedInputException mie) {
                return badRequest(mie.getMessage());
            }
    
            msg = ((ProtoBufMessages.ResolveRequest)input.getMessage());
    
            ActorSelection resolver = Akka.system().actorSelection(RESOLVER_ACTOR);
    
    
            Timeout tim = new Timeout(Duration.create(4, "seconds"));
    
            Future<Object> fut = Patterns.ask(resolver, input.getMessage(), tim);
    
            return async (
                    F.Promise.wrap(fut).map(
                            new F.Function<Object, Result>() {
                                public Result apply(Object response) {
                                    ProtoBufMessages.ResolveReply rsp = ((ProtoBufMessages.ResolveReply)response);
                                    ResolveOutput output = new ResolveOutput(rsp);
                                    return ok(output.getJsonReply());
                                }
                            }
                    )
            );
        }
    }
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   wireknight    10 年前

    我拿出了下面的代码

    public class Resolve extends Controller {
    
        private final static String RESOLVER_ACTOR = play.Play.application().configuration().getString("actor.resolve");
        private final static BrainProtoMessages.ResolveReply request_error = BrainProtoMessages.ResolveReply.newBuilder()
            .setReturnCode(BResults.REQUEST_FAILED)
            .build();
    
        @CorsRest
        @VerboseRest
        @RequireAuthentication
        @BodyParser.Of(BodyParser.Json.class)
    
    public static Result resolve_map() {
    
        final ResolveRequestInput input;
        final F.Promise<ActorSelection> selected_target;
        final F.Promise<Future<Object>> backend_request;
        final F.Promise<BrainProtoMessages.ResolveReply> backend_reply;
        final F.Promise<ObjectNode> decode_json;
        final F.Promise<Result> ok_result;
        final JsonNode json = request().body().asJson();
    
        try {
            input = new ResolveRequestInput(json);
        } catch (rest.exceptions.MalformedInputException mie) {
            return badRequest(mie.getMessage());
        }
    
        selected_target = F.Promise.promise(
          new F.Function0<ActorSelection>() {
              @Override
              public ActorSelection apply() throws Throwable {
                  return Akka.system().actorSelection(RESOLVER_ACTOR);    
              }
          }      
        );
    
        backend_request =
                selected_target.map(
                        new F.Function<ActorSelection, Future<Object>>() {
                            @Override
                            public Future<Object> apply(ActorSelection actorSelection) throws Throwable {
                                return Patterns.ask(actorSelection, input.getMessage(),new Timeout(Duration.create(4, "seconds")));
                            }
                        }
                );
    
        backend_reply = backend_request.map(
    
                new F.Function<Future<Object>, BrainProtoMessages.ResolveReply>() {
                    @Override
                    public BrainProtoMessages.ResolveReply apply(Future<Object> akka_reply) throws Throwable {
                        try {
                            return (BrainProtoMessages.ResolveReply) Await.result(akka_reply, Duration.create(4, "seconds"));
                        }catch(Exception error)
                        {
                            return request_error;
                        }    
                    }
                }
        );
    
        decode_json = backend_reply.map(
    
                new F.Function<BrainProtoMessages.ResolveReply, ObjectNode>() {
                    @Override
                    public ObjectNode apply(BrainProtoMessages.ResolveReply response) throws Throwable {
                        return new ResolveOutput(response).getJsonReply();
                    }
                }
            );
    
        ok_result = decode_json.map(
                new F.Function<ObjectNode, Result>() {
                    @Override
                    public Result apply(ObjectNode reply) {
                        return ok(reply);
                    }
                }
        );
    
        try {
            return ok_result.get(8000);
        }catch(Exception error)
        {
            return internalServerError();
        }
    
    }
    

    }