• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Scala HashedWheelTimer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala IShadowsocksServiceCallback类代码示例发布时间:2022-05-23
下一篇:
Scala SpringBootApplication类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap