我有这样的方法:
def myFuture: Future[Either[MyLeft, MyRight]] = Future {
.
.
.
}
如果我想管道结果,我使用:
Patterns.pipe(myFuture,ec).to(destinationActor)
但我希望在Left的情况下,将结果发送给一个actor并且在Right发送结果的情况下发送给另一个actor。像这样的伪代码:
MyPatterns.eitherPipe(myFuture,ec).to(leftConsumerActor,rightConsumerActor)
答案 0 :(得分:4)
akka本身的源代码是一个很好的暗示应该做什么。看看akka.pattern.PipeToSupport
:
def pipeTo(recipient: ActorRef)(implicit sender: ActorRef = Actor.noSender): Future[T] = {
future andThen {
case Success(r) ⇒ recipient ! r
case Failure(f) ⇒ recipient ! Status.Failure(f)
}
}
因此我们基本上可以在调度Either
:
val result: Future[Either[Int, Throwable]] = Future.successful(Left(5))
result andThen {
case Success(Left(value)) => leftActor ! value
case Success(Right(exception)) => rightActor ! exception
case Failure(exception) => println("Failure")
}
实现所需的DSL:
我们可以尝试实现您的DSL(,管道()和到(...)),如下所示:
trait MyEitherPipeSupport extends PipeToSupport {
final class PipeableEitherFuture[L, R](val future: Future[Either[L, R]])(implicit executionContext: ExecutionContext) {
def to(leftRef: ActorRef, rightRef: ActorRef, exceptionRef: ActorRef) = future andThen {
case Success(Left(value)) ⇒ leftRef ! value
case Success(Right(exception)) ⇒ rightRef ! exception
case Failure(exception) ⇒ exceptionRef ! Status.Failure(exception)
}
}
implicit def eitherPipe[L, R](future: Future[Either[L, R]])(implicit executionContext: ExecutionContext): PipeableEitherFuture[L, R] = new PipeableEitherFuture(future)
}
现在你的演员只需要混合MyEitherPipeSupport
,就可以这样写:
val result: Future[Either[Int, Throwable]] = Future.successful(Left(5))
eitherPipe(result).to(left, right, anotherOne)
答案 1 :(得分:0)
如果你做这样的事情?
myFuture onComplete {
case Success(s) => s match {
case Right(r) => rightConsumerActor ! r
case Left(l) => leftConsumerActor ! l
}
case Failure(f) => println("failure")
}