playframework – 在不使用AsyncResult的情况下播放2个异步webservice调用

前端之家收集整理的这篇文章主要介绍了playframework – 在不使用AsyncResult的情况下播放2个异步webservice调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Play 2允许你通过AsyncResult执行 async webservice calls,它不会阻塞线程:

public static Result FeedTitle(String FeedUrl) {
    return async(
        WS.url(FeedUrl).get().map(
            new Function<WS.Response,Result>() {
                public Result apply(WS.Response response) {
                    return ok("Feed title:" + response.asJson().findPath("title"));
                }
            }
        )
    );
}

这只适用于您正在执行简单的操作,例如将WS调用的结果直接传递给用户.但是,如果您必须对结果执行其他操作,该怎么办?

看看the documentation,看起来你可以这样做:

Promise<Response> promise = WS.url("http://some.website.com").get();
Response response = promise.get();    // I've got the result,but I've also blocked

这显然不太理想.有没有办法在允许Play将执行传递给其他线程的同时进行异步调用

解决方法

看一下 https://github.com/jroper/play-promise-presentation.这对于我如何设计一个可能有多个承诺调用等的系统,并将各种承诺响应操作到更复杂的响应所需的内容等等,我真的很清楚.

最好的部分是 – 这个例子并不觉得太冗长.它阅读得非常好,理解起来非常简单.

猜你在找的WebService相关文章