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.这对于我如何设计一个可能有多个承诺调用等的系统,并将各种承诺响应操作到更复杂的响应所需的内容等等,我真的很清楚.

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

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...