当前位置: 首页>>代码示例>>Scala>>正文


Scala HashedWheelTimer类代码示例

本文整理汇总了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()
  }
} 
开发者ID:wavesplatform,项目名称:Waves,代码行数:40,代码来源:NetworkSender.scala

示例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
  }
} 
开发者ID:bwsw,项目名称:tstreams-transaction-server,代码行数:34,代码来源:TimeoutScheduler.scala


注:本文中的io.netty.util.HashedWheelTimer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。