本文整理汇总了Scala中io.netty.util.Timeout类的典型用法代码示例。如果您正苦于以下问题:Scala Timeout类的具体用法?Scala Timeout怎么用?Scala Timeout使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Timeout类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TimeoutScheduler
//设置package包名称以及导入依赖的类
package com.bwsw.tstreamstransactionserver.netty.client
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import com.bwsw.tstreamstransactionserver.exception.Throwable.RequestTimeoutException
import scala.concurrent.{ExecutionContext, Future => ScalaFuture, Promise => ScalaPromise}
import scala.concurrent.duration.Duration
import io.netty.util.{HashedWheelTimer, Timeout}
import org.slf4j.LoggerFactory
object TimeoutScheduler{
private val logger = LoggerFactory.getLogger(this.getClass)
private val timer = new HashedWheelTimer(10, TimeUnit.MILLISECONDS)
def scheduleTimeout(promise:ScalaPromise[_], after:Duration, reqId: Long): Timeout = {
timer.newTimeout((timeout: Timeout) => {
val requestTimeoutException = new RequestTimeoutException(reqId, after.toMillis)
val isExpired = promise.tryFailure(requestTimeoutException)
if (isExpired && logger.isDebugEnabled) logger.debug(requestTimeoutException.getMessage)
}, after.toNanos, TimeUnit.NANOSECONDS)
}
def withTimeout[T](fut:ScalaFuture[T])(implicit ec:ExecutionContext, after:Duration, reqId: Long): ScalaFuture[T] = {
val prom = ScalaPromise[T]()
val timeout = TimeoutScheduler.scheduleTimeout(prom, after, reqId)
val combinedFut = ScalaFuture.firstCompletedOf(collection.immutable.Seq(fut, prom.future))
fut onComplete (_ => timeout.cancel())
combinedFut
}
}