play.api.libs.concurrent.Futures
See theFutures companion object
trait Futures
This trait is used to provide non-blocking timeouts and delays on an operation that returns a Future.
You can dependency inject the Futures as follows to create a Future that will timeout after a certain period of time:
class MyService @Inject()(futures: Futures, piCalculator: PiCalculator) extends Timeout {
def calculateWithTimeout(timeoutDuration: FiniteDuration): Future[Int] = {
futures.timeout(timeoutDuration)(piCalculator.rawCalculation())
}
}
And you can also use a delay to return data after a given period of time.
class PiCalculator @Inject()(futures: Futures) {
def rawCalculation(): Future[Int] = {
futures.delay(300 millis) { Future.successful(42) }
}
}
You should check for timeout by using scala.concurrent.Future.recover or scala.concurrent.Future.recoverWith and checking for scala.concurrent.TimeoutException:
val future = myService.calculateWithTimeout(100 millis).recover {
case _: TimeoutException =>
-1
}
Attributes
- See also
- Companion
- object
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class DefaultFutures
Members list
In this article