本文整理汇总了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
}
}
示例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)
}
}
}
示例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)
}
示例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()
}
示例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))
}
}
示例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))
}
}
示例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)
}
示例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))
}
}
}