Return Twitter 在一定时间内的未来

Return Twitter's Future within a certain duration

根据 Twitter Util 的 Future 文档,Future.within() or .by()“Returns 一个新的 Future,如果它在给定时间之前不满足则失败”。

我试图在 Java 中使用它来 return 如果 getDownload() 花费的时间少于 5 秒,并且 return / 抛出异常如果 getDownload() 花费超过 5 秒。

Future<String> futureDownload = getDownload();
return futureDownload.within(DefaultTimer.twitter(), Duration.fromSeconds(5));

我无法让它工作。例如,如果 getDownload() 需要 15 秒,它会在 15 秒后 returns。

我错过了什么?

在您的情况下,您应该改用 Await.result

示例:

Await.result(future, Duration#fromSeconds);