如何在Play-Framework 2.6中从CompletionStage获取json对象

时间:2019-02-09 11:34:47

标签: java playframework promise playframework-2.6 completion-stage

我需要从以下代码中获取json对象

JsonObject request = HttpRequestProcessor.getJsonFromRequest(request().body());
    ActorRef actorRef = ActorHandler.getActorReferenceOfParameterisedActor(AControllerTasksExecutor.class,
            new AControllersTaskContext(FactoryType.USER_COMMAND_FACTORY, ObjectType.PURCHASE_SHARES));

            CompletionStage<JsonObject> promise = PromiseHandler.getPromise(actorRef, request, 7000);

    JsonObject response = null;

    Result result = null;
    try {
        response =  promise.get(70000);
        log.debug(">> response object >> {}", response);
        result = PlayResultHandler.getResult(response.get("status").getAsInt(), JsonHandler.parseJsonIntoString(response));

    } catch (Exception e) {

}

public class PromiseHandler {


private final static Logger log = LoggerFactory.getLogger(PromiseHandler.class);

public static <B> CompletionStage<B> getPromise(ActorRef actorRef, Object msg, int time) {

    log.debug("Preparing promise. ActorRef: {}, Message-Object: {}, time: {}", actorRef, msg, time);
    CompletionStagev<B> prom = CompletionStage.wrap(ask(actorRef, msg, time)).map(new Function<Object, B>() {

        @SuppressWarnings("unchecked")
        public B apply(Object response) {
            log.debug("In Apply method. Returning Response.  Response-object: {}",response);
            return (B) response;
        }

    });

    return prom;
}

}

public class AControllerTasksExecutor extends UntypedActor {


    private final static Logger log = LoggerFactory
            .getLogger(AControllerTasksExecutor.class);

    AControllersTaskContext context = null;

    public AControllerTasksExecutor(AControllersTaskContext context) {

        this.context = context;
    }


    @Override
    public void onReceive(Object arg0) throws Exception {

        log.info("Message received. Processing on Message. ");

        Object response = context.executesStrategy(arg0);

        log.debug("Sending response back ");

        getSender().tell(response, getSelf());

    }

我怎样才能像

那样获得我正在执行的json对象
response =  promise.get(70000);

但是它给出了

cannot find symbol
[error]   symbol:   method get(int)
[error]   location: variable promise of type java.util.concurrent.CompletionStage<com.google.gson.JsonObject>
[error]             response =  promise.get(70000);

我该如何实现?

0 个答案:

没有答案