本文整理汇总了Scala中io.netty.util.HashedWheelTimer类的典型用法代码示例。如果您正苦于以下问题:Scala HashedWheelTimer类的具体用法?Scala HashedWheelTimer怎么用?Scala HashedWheelTimer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HashedWheelTimer类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: NetworkSender
//设置package包名称以及导入依赖的类
package com.wavesplatform.it.util
import java.net.InetSocketAddress
import java.util.concurrent.ConcurrentHashMap
import com.wavesplatform.it.network.client.NetworkClient
import com.wavesplatform.network.{PeerInfo, RawBytes}
import io.netty.channel.Channel
import io.netty.channel.group.DefaultChannelGroup
import io.netty.util.HashedWheelTimer
import io.netty.util.concurrent.GlobalEventExecutor
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
class NetworkSender(address: InetSocketAddress, chainId: Char, name: String, nonce: Long) {
private val retryTimer = new HashedWheelTimer()
val allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE)
val establishedConnections = new ConcurrentHashMap[Channel, PeerInfo]
val c = new NetworkClient(chainId, name, nonce, allChannels, establishedConnections)
c.connect(address)
def sendByNetwork(messages: RawBytes*): Future[Unit] = {
retryTimer.retryUntil(Future.successful(establishedConnections.size()), (size: Int) => size == 1, 1.seconds)
.map(_ => {
val channel = establishedConnections.asScala.head._1
messages.foreach(msg => {
channel.writeAndFlush(msg)
})
})
}
def close(): Unit = {
retryTimer.stop()
c.shutdown()
}
}
示例2: 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
}
}