![JAR search and dependency download from the Maven repository](/logo.png)
io.leonard.amqp.concurrent.FutureTimeout.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amqp-scala-client_2.12 Show documentation
Show all versions of amqp-scala-client_2.12 Show documentation
Scala wrapper for rabbitmq-java-client
The newest version!
package io.leonard.amqp.concurrent
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ Future, TimeoutException }
object FutureTimeout {
/**
* Returns a new future which will be completed either by the original future or by a timeout exception after the
* specified delay. Timeout tasks are cancelled when the main future completes
* @param duration to set timeout
* @param future for the main completion of the returned future
* @param executor schedules the timeout and combines the futures
*/
def timeoutAfter[T](duration: FiniteDuration)(future: Future[T])(implicit executor: ScheduledExecutor): Future[T] = {
implicit val executionContext = executor.executionContext
val timeoutFuture = scheduleTimeout(duration, executor)
future.onComplete(cancelCallback(timeoutFuture))
Future.firstCompletedOf(Seq(future, timeoutFuture))
}
private def cancelCallback[T](cancellableFuture: CancellableFuture[T]): (Any) ⇒ Boolean = {
_ ⇒ cancellableFuture.cancel(mayInterruptIfRunning = false)
}
private def scheduleTimeout[T](duration: FiniteDuration, executor: ScheduledExecutor): CancellableFuture[Nothing] = {
executor.delayExecution(throw new TimeoutException("Future timed out"))(duration)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy