scala - 无法在 [[scala.concurrent.Future]] 上获取 `pipeTo` 方法

标签 scala akka

正如 akka 的文档所解释的,您应该能够通过这种方式获得 [[scala.concurrent.Future]] 上的 pipeTo 方法:

import akka.pattern.pipe
val future = ...
future pipeTo sender()

不幸的是,我不能这样做,我在 IDE 中收到错误“无法解析符号 pipelineTo”。

作为解决方法,我必须以这种方式使用语法

pipe(future) pipeTo sender()

但它仍然让我不明白为什么(顺便说一句,我对 scala 很陌生)。非常感谢您帮助理解这个难题。

scala 2.12.2 Akka 2.5.3

最佳答案

您需要在范围内有一个隐式 ExecutionContext,下面是一个示例:

import akka.actor.{Actor, ActorSystem, Props}
import akka.pattern.pipe

import scala.concurrent.Future

// Get the implicit ExecutionContext from this import
import scala.concurrent.ExecutionContext.Implicits.global

object Hello extends App {

  // Creating a simple actor
  class MyActor extends Actor {
    override def receive: Receive = {
      case x => println(s"Received message: ${x.toString}")
    }
  }

  // Create actor system
  val system = ActorSystem("example")
  val ref = system.actorOf(Props[MyActor], "actor")

  // Create the future to pipe
  val future: Future[Int] = Future(100)

  // Test
  future pipeTo ref
}

控制台:

sbt run
[info] <stuff here>
[info] Running example.Hello 
Received message: 100
<小时/>

您必须这样做的原因是因为 pipeToPipeableFuture 上的实例函数,并且您的常规 Future 必须是“增强”为 PipeableFuture。这是PipeableFuture的构造函数,注意隐式executionContext:ExecutionContext参数:

final class PipeableFuture[T](val future: Future[T])(implicit executionContext: ExecutionContext)

完整的类在这里,您可以在其中看到 pipeTo 函数:

final class PipeableFuture[T](val future: Future[T])(implicit executionContext: ExecutionContext) {
  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)
    }
  }
  def pipeToSelection(recipient: ActorSelection)(implicit sender: ActorRef = Actor.noSender): Future[T] = {
    future andThen {
      case Success(r) ⇒ recipient ! r
      case Failure(f) ⇒ recipient ! Status.Failure(f)
    }
  }
  def to(recipient: ActorRef): PipeableFuture[T] = to(recipient, Actor.noSender)
  def to(recipient: ActorRef, sender: ActorRef): PipeableFuture[T] = {
    pipeTo(recipient)(sender)
    this
  }
  def to(recipient: ActorSelection): PipeableFuture[T] = to(recipient, Actor.noSender)
  def to(recipient: ActorSelection, sender: ActorRef): PipeableFuture[T] = {
    pipeToSelection(recipient)(sender)
    this
  }
}

由于 pipe(future) 不是 Future 上的实例函数,因此它可以在您的示例中工作。

关于scala - 无法在 [[scala.concurrent.Future]] 上获取 `pipeTo` 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042893/

相关文章:

scala - 使用 Maven 打包并运行 Scala Spark 项目

scala - jooq + scala 代码生成 : method createIndex in object AbstractKeys cannot be accessed in object org. jooq.impl.AbstractKeys

scala - 列表中具有奇数和偶数索引的Zip元素

scala - Scala 中 "for (elm <- myList) yield f(_)"和 "myList map f(_)"的区别

scala - 在构建FlowGraph时,如何连接两个Flow?

scala - Akka TCP IO 向我发送两次连接消息

scala - 使用akka http路由dsl提取路径参数

java - Akka远程actor部署文档错误

scala - 将无形 HList 转换为元组

scala - 如何在 scala 中获取(不可变和可变)集合的列表?