Scala Akka 演员 - 遇到死信

阿什温

我正在将 Scala 与 Akka 演员一起使用。我知道演员有邮箱。因此,与actor 的任何通信都是序列化的。我有一个演员做某项工作——比如说它下载一个图像。

class DownloadImageActor(implicit val injector: Injector) extends Actor with Injectable {
  val imageDownloadService = inject[ImageDownloadService]
  implicit val ec = inject[ExecutionContext]

  override def receive: Receive = {
    case DownloadImage(jobId, imageUrl) =>
      imageDownloadService.downloadImage(imageUrl).onComplete {
        case Success(image) =>
          sender() ! ImageDownloadSuccess(imageUrl, image, jobId)
        case Failure(e) =>
          sender() ! ImageDownloadFail(imageUrl, e, jobId)
      }
  }
}

如您所见,演员以异步方式下载图像。imageDownloadService.downloadImage返回一个Future消息完成后发送给发送者。现在这是我收到dead letters encountered消息的地方。

我哪里错了?



编辑 #1

向下载角色发送消息的父角色

class ParentActor(implicit val injector : Injector) extends Actor with Injectable {

  val downloadImageActor = inject[ActorRef](identified by "ImageDownloadActor")


  override def receive: Receive = {
    case DownloadImages(urls, _id) => urls.foreach(url =>
      downloadImageActor ! DownloadImage(id, imageUrl = url)
    )
    case ImageDownloadSuccess(image : Image) =>
  }
}
在线

不知道是否有任何其他问题,但你使用的方法senderFuture是错误的,你需要将其分配到一个新的变量,然后再结合onComplete它形成将不会对其他演员的手柄覆盖封闭回调。

在您的代码中,需要添加 lineA,并将 lineB、lineC 替换为 lineD、lineE。或者你可能想看看pipeToFuture 的功能。

class DownloadImageActor(implicit val injector : Injector)  extends Actor with Injectable{
  val imageDownloadService = inject[ImageDownloadService]
  implicit val ec = inject[ExecutionContext]

  override def receive: Receive = {
    case DownloadImage(jobId, imageUrl) => 
      val client = sender // lineA
      imageDownloadService.downloadImage(imageUrl).onComplete {
      //case Success(image) => sender() !  ImageDownloadSuccess(imageUrl, image, jobId) // lineB
      //case Failure(e) => sender() !  ImageDownloadFail(imageUrl,e, jobId) // lineC
      case Success(image) => client !  ImageDownloadSuccess(imageUrl, image, jobId) // lineD
      case Failure(e) => client !  ImageDownloadFail(imageUrl,e, jobId) // lineE
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章