5

我正在尝试使用 JAVA api 在 Play Framework 2.1 上使用 WS 访问 URL。

这是我想要的:

  1. WS.get()在代码中的某些地方,使用(我设置 timeout 1000ms)启动 WS 请求
  2. 如果 WS.get 超时或发生其他异常,我不希望我的 Promise 抛出异常(因为我的流程需要这种方式)所以我用Promise.recover()另一个来包装这个 Promise。在失败的情况下返回一个nullReponse 对象。
  3. 我想在代码中的其他地方“获取”我的响应。我等待5000 毫秒,但我得到的是java.util.concurrent.TimeoutException: Futures timed out after [5000 milliseconds]

如何? 1000 毫秒WS.get()后超时,因为它抛出一个它被我的恢复函数捕获。它返回空响应而不是异常。java.util.concurrent.TimeoutException: No response received after 1000

所以承诺最多在 1000 毫秒后“完成”。为什么它会在 5000 毫秒后超时并引发异常?

代码:

    Logger.info("Fetch started: " + new Date().toString());
Promise<WS.Response> p1 = WS.url("http://athena.ics.forth.gr:9090/RDF/VRP/Examples/tap.rdf").setTimeout(1000).get();
Promise<WS.Response> p2 = p1.recover(new Function<Throwable, WS.Response>() { 
  @Override
  public Response apply(Throwable a) throws Throwable {
    Logger.error("Promise thrown an exception so I will return null. " + new Date().toString() + " " + System.currentTimeMillis(), a);
    return null;
  }
});

try {
  /* This should return null or valid response but shouldn't throw an exception */
  Response r = p2.get(5000L);
  Logger.info(r.toString());
} catch (Exception e) {
  /* Sholdn't happen! */
  Logger.error("Outer exception: " + " " + System.currentTimeMillis(), e);
}
Logger.error("Fetch finished: " + new Date().toString() + " " + System.currentTimeMillis());

我得到的输出:

[info] application - Fetch started: Tue Mar 19 10:04:42 EET 2013
[error] application - Outer exception:  1363680287626
java.util.concurrent.TimeoutException: Futures timed out after [5000 milliseconds]
    at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:96) ~[scala-library.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:58) ~[scala-library.jar:na]
    at scala.concurrent.Await$$anonfun$ready$1.apply(package.scala:86) ~[scala-library.jar:na]
    at scala.concurrent.Await$$anonfun$ready$1.apply(package.scala:86) ~[scala-library.jar:na]
    at akka.dispatch.MonitorableThreadFactory$AkkaForkJoinWorkerThread$$anon$3.block(ThreadPoolBuilder.scala:173) ~[akka-actor_2.10.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.managedBlock(ForkJoinPool.java:2803) [scala-library.jar:na]
[error] application - Fetch finished: Tue Mar 19 10:04:47 EET 2013 1363680287627
[error] application - Promise thrown an exception so I will return null. Tue Mar 19 10:04:47 EET 2013 1363680287627
java.util.concurrent.TimeoutException: No response received after 1000
    at com.ning.http.client.providers.netty.NettyAsyncHttpProvider$ReaperFuture.run(NettyAsyncHttpProvider.java:1809) ~[async-http-client.jar:na]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) ~[na:1.7.0_11]
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351) ~[na:1.7.0_11]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178) ~[na:1.7.0_11]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) ~[na:1.7.0_11]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.7.0_11]

即使我等待 promise2 并且 promise2 是“恢复包装的 promise1”,恢复函数在抛出异常后运行。

4

0 回答 0