3

我仍然是使用 Play!Framework 2.1 WS 库实现 Web 服务请求的新手。现在,我在理解 WS 库行为方面遇到了问题。

首先,我有这样的代码:

public static Result espnRss() {
    try {
        // do request
        return async(
            WS.url("http://espnfc.com/rss/news?section=premierleague").get().map(
                new F.Function<WS.Response, Result>() {
                    @Override
                    public Result apply(WS.Response response) throws Throwable {
                        return ok("Success!"); // success request
                    }
                }
            )
        );
    } catch (Exception e) {
        // exception occured
        return internalServerError("Oops, connect exception occured!");
    }
}

当我尝试请求espnRss操作时,我得到了 SUCCESS 响应。

然后,我想在请求上设置 WS 超时。所以,我改变了我以前的代码是这样的:

public static Result espnRss() {
    try {
        // set request timeout for 1000 ms and do request
        return async(
            WS.url("http://espnfc.com/rss/news?section=premierleague").setTimeout(1000).get().map(
                ... // same as previous
            )
        );
    } catch (Exception e) {
        // exception occured
        return internalServerError("Oops, connect exception occured!");
    }
}

我的互联网连接不快(下载速度约为 40 KB/s),我故意这样做(将请求超时设置为 1 秒)以执行异常处理代码。

但是,我从框架得到默认响应,而不是internalServerError响应提供的代码。

Execution Exception 
[TimeoutException: No response received after 1000] 

谁能解释我为什么无法使用我上面的代码捕获 WS 请求的异常?使用 Play!Framework WS 库处理异常的最佳方法是什么?

4

2 回答 2

9

To handle exception that occur on asynchronous request such as WS request with Play!Framework 2.1.0, there is method on Promise named recover(F.Function<java.lang.Throwable,A> function).

The method should be called when we want to handle all exception occured while requesting using WS library. So, I solved the problem using code that looked like following:

public static Result espnRss() {
    // do request
    return async(
        WS.url("http://espnfc.com/rss/news?section=premierleague").setTimeout(100).get().map(
            new F.Function<WS.Response, Result>() {
                @Override
                public Result apply(WS.Response response) throws Throwable {
                    return ok("Success!"); // success request
                }
            }
        ).recover( // to handle error occured on redeemed PROMISE
            new F.Function<Throwable, Result>() {
                @Override
                public Result apply(Throwable throwable) throws Throwable {
                    // option to distinguish exception
                    if (throwable instanceof TimeoutException) {
                        return internalServerError("Oops, time out exception occured!");
                    } else {
                        return internalServerError("Oops, other exception occured!");
                    }
                }
            }
        )
    );
}
于 2013-03-22T03:13:44.947 回答
2

我不熟悉 Play 框架,但async必须返回/使用某种未来。该请求实际上是在一个单独的线程中执行的,异常显然不会被您的try..catch处理程序捕获。

必须有一些类似的功能/方法onComplete可以应用于async允许您测试运行请求的结果。

于 2013-03-21T08:03:05.453 回答