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

Scala after类代码示例

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

本文整理汇总了Scala中akka.pattern.after的典型用法代码示例。如果您正苦于以下问题:Scala after类的具体用法?Scala after怎么用?Scala after使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了after类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: ErrorHandling

//设置package包名称以及导入依赖的类
package au.csiro.data61.magda.util

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.actor.Scheduler
import akka.pattern.after

object ErrorHandling {
  
  def retry[T](op: () => Future[T], delay: FiniteDuration, retries: Int, onRetry: (Int, Throwable) => Unit = (_, _) => {})(implicit ec: ExecutionContext, s: Scheduler): Future[T] =
    Future { op() } flatMap (x => x) recoverWith {
      case e: Throwable if retries > 0 => after(delay, s)({
        onRetry(retries - 1, e)
        retry(op, delay, retries - 1, onRetry)
      })
    }

  object CausedBy {
    def unapply(e: Throwable): Option[Throwable] = Option(e.getCause)
  }

  object RootCause {
    def unapply(e: Throwable): Option[Throwable] = Option(getRootCause(e))
  }

  def getRootCause(e: Throwable): Throwable =
    Option(e.getCause) match {
      case Some(cause) => getRootCause(cause)
      case None        => e
    }
} 
开发者ID:TerriaJS,项目名称:magda,代码行数:33,代码来源:ErrorHandling.scala


示例2: logger

//设置package包名称以及导入依赖的类
package eventsource.slick

import akka.actor.Scheduler
import akka.pattern.after
import play.api.LoggerLike

import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}

trait Retrying {
  def logger: LoggerLike

  def retry[T](retries: Int,
               delay: FiniteDuration,
               errorHandler: PartialFunction[(Throwable, Int), Future[T]] = PartialFunction.empty)(
      f: => Future[T])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
    f.recoverWith {
      case e if errorHandler.isDefinedAt(e, retries) =>
        errorHandler.apply(e, retries)
      case e if retries > 0 =>
        logger.warn(s"Retrying on ${e.getMessage}")
        after(delay, s)(retry(retries - 1, delay, errorHandler)(f))
      case e: Throwable =>
        logger.error("Retries exhausted", e)
        Future.failed(e)
    }
  }
} 
开发者ID:21re,项目名称:play-eventsource,代码行数:29,代码来源:Retrying.scala


示例3: Job

//设置package包名称以及导入依赖的类
package org.zalando.benchmarks

import akka.actor.ActorSystem
import akka.pattern.after
import org.openjdk.jmh.infra.Blackhole

import scala.concurrent.Future
import scala.util.Random

case class Job(id: Int) {
  val payload = Array.fill(16000)(Random.nextInt())
}
case class JobResult(job: Job, result: Int)
case class PublishResult(result: JobResult)

object Computer {
  import ComputationFollowedByAsyncPublishing._

  def compute(job: Job): JobResult = {
    // jmh ensures that this really consumes CPU
    Blackhole consumeCPU numTokensToConsume
    JobResult(job, job.id)
  }
}

object Publisher {
  import ComputationFollowedByAsyncPublishing._

  // we use the scheduler and the dispatcher of the actor system here because it's so very convenient
  def publish(result: JobResult, system: ActorSystem): Future[PublishResult] =
    after(publishDuration, system.scheduler) {
      Future(PublishResult(result))(system.dispatcher)
    } (system.dispatcher)
} 
开发者ID:narayana-glassbeam,项目名称:scala-concurrency-playground,代码行数:35,代码来源:Model.scala


示例4: HelloWorldBackpressured

//设置package包名称以及导入依赖的类
package eu.svez.backpressuredemo.A_local

import akka.NotUsed
import akka.pattern.after
import akka.stream.OverflowStrategy
import akka.stream.scaladsl.{Flow, Sink, Source}
import eu.svez.backpressuredemo.StreamDemo
import kamon.Kamon

import scala.concurrent.Future
import scala.concurrent.duration._

object HelloWorldBackpressured extends StreamDemo {

  sourceRate.send(5)
  sinkRate.send(5)

  def valve[T](rate: => Int): Flow[T, T, NotUsed] =
    Flow[T].mapAsync(1) { x =>
      after(1.second / rate, system.scheduler)(Future.successful(x))
    }

  def meter[T](name: String): Flow[T, T, NotUsed] = {
    val msgCounter = Kamon.metrics.counter(name)

    Flow[T].map { x =>
      msgCounter.increment()
      x
    }
  }

  Source.repeat("Hello world!")
    .via(valve(sourceRate.get))
    .via(meter("source"))

    .buffer(100, OverflowStrategy.backpressure)
    .via(valve(sinkRate.get))
    .via(meter("sink"))
    .runWith(Sink.ignore)



  readRatesFromStdIn()
} 
开发者ID:svezfaz,项目名称:akka-backpressure-scala-central-talk,代码行数:45,代码来源:HelloWorldBackpressured.scala


示例5: Flows

//设置package包名称以及导入依赖的类
package eu.svez.backpressuredemo

import akka.NotUsed
import akka.actor.ActorSystem
import akka.pattern.after
import akka.stream.scaladsl.Flow
import kamon.Kamon

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

object Flows {

  def meter[T](name: String): Flow[T, T, NotUsed] = {
    val msgCounter = Kamon.metrics.counter(name)

    Flow[T].map { x =>
      msgCounter.increment()
      x
    }
  }

  def valve[T](rate: => Int)(implicit system: ActorSystem, ec: ExecutionContext): Flow[T, T, NotUsed] =
    Flow[T].mapAsync(1) { x =>
      after(1.second / rate, system.scheduler)(Future.successful(x))
    }

} 
开发者ID:svezfaz,项目名称:akka-backpressure-scala-central-talk,代码行数:29,代码来源:Flows.scala


示例6: retry

//设置package包名称以及导入依赖的类
package com.google.api.services.pubsub

import akka.actor.ActorSystem
import akka.pattern.after

import scala.concurrent._
import scala.concurrent.duration._

trait RetryLogic {

  def retry[B](op: () => Future[B], retries: Int)(implicit ec: ExecutionContext, s: ActorSystem): Future[B] =
    op() recoverWith {
      case _ if retries > 0 => after(2 seconds, using = s.scheduler)(retry(op, retries - 1))
    }

  def retry[A,B](op: A => Future[B], retries: Int)(elem: A)(implicit ec: ExecutionContext, s: ActorSystem): Future[B] =
    op(elem) recoverWith {
      case _ if retries > 0 => after(2 seconds, using = s.scheduler)(retry(op, retries - 1)(elem))
    }

} 
开发者ID:fullstackanalytics,项目名称:pubsub4s,代码行数:22,代码来源:RetryLogic.scala


示例7: Retry

//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal.javadsl.persistence.jpa

import java.util.concurrent.CompletionStage
import java.util.function.Supplier

import akka.actor.Scheduler
import akka.pattern.after

import scala.concurrent.duration.Duration.fromNanos
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ ExecutionContext, Future }
import scala.util.control.NonFatal

// With thanks to https://gist.github.com/viktorklang/9414163
private[lagom] class Retry(delay: FiniteDuration, delayFactor: Double, maxRetries: Int) {
  def apply[T](op: => T)(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
    def iterate(nextDelay: FiniteDuration, remainingRetries: Int): Future[T] =
      Future(op) recoverWith {
        case NonFatal(throwable) if remainingRetries > 0 => {
          onRetry(throwable, nextDelay, remainingRetries)
          after(nextDelay, s)(iterate(finiteMultiply(nextDelay, delayFactor), remainingRetries - 1))
        }
      }

    iterate(delay, maxRetries)
  }

  // For convenient use from Java 8
  def retry[T](op: Supplier[T])(implicit ec: ExecutionContext, s: Scheduler): CompletionStage[T] = {
    import scala.compat.java8.FutureConverters._

    apply(op.get()).toJava
  }

  protected def onRetry(throwable: Throwable, delay: FiniteDuration, remainingRetries: Int): Unit = ()

  private def finiteMultiply(duration: FiniteDuration, factor: Double): FiniteDuration =
    fromNanos((duration.toNanos * factor).toLong)
}

private[lagom] object Retry {
  def apply[T](delay: FiniteDuration, delayFactor: Double, maxRetries: Int)(op: => T)(implicit ec: ExecutionContext, s: Scheduler): Future[T] =
    (new Retry(delay, delayFactor, maxRetries))(op)
} 
开发者ID:lagom,项目名称:lagom,代码行数:45,代码来源:Retry.scala


示例8: retry

//设置package包名称以及导入依赖的类
package org.bjean.sample.wordcount.aws.support

import akka.actor.Scheduler

import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future}
import akka.pattern.after

trait Retrying {
  def retry[T](op: => T, retries: Int)(implicit ec: ExecutionContext): Future[T] = {
    val eventualT: Future[T] = Future(op)
    eventualT recoverWith {
      case _ if retries > 0 => retry(op, retries - 1)
    }
  }

  def retry[T](eventualT: => Future[T], delay: FiniteDuration, retries: Int)(implicit ec: ExecutionContext, s: Scheduler): Future[T] = {
    eventualT recoverWith {
      case _ if retries > 0 => after(delay, s)(retry(eventualT, delay, retries - 1))
    }
  }
} 
开发者ID:bjet007,项目名称:word-count-spark-aws,代码行数:23,代码来源:Retrying.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala AccumulableParam类代码示例发布时间:2022-05-23
下一篇:
Scala ExecutionEnv类代码示例发布时间: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