如何在演员之间或从演员到未来之间发送迭代?

时间:2014-05-04 03:56:46

标签: scala akka

程序主方法的未来会向其actor发送一个msg,要求提供一个可迭代对象。然后,actor创建另一个未来,从远程actor请求可迭代对象(比如ArrayBuffer)。从远程actor接收到ArrayBuffer之后,actor如何将它发送回main方法的第一个未来?它似乎创建了一个sender的本地别名,并且创建一个单独的case类来表示iterable并不会阻止遇到dead letters

以下是示例代码:

case class SequenceObject(sqnce:Seq[someArrayBuffer])

//...

implicit val timeout = Timeout(10 seconds)
val fut1: Future[Any] = myActor ? iNeedAnArrayBufferObject 

fut1.onSuccess {
  case listOfItems: SequenceObject => {
    //do sth with listofItems.sqnce
  }

class myActor extends Actor {
  implicit val timeout = Timeout(1 seconds)

  def receive = {
  case a: iNeedAnArrayBufferObject => {
    val originalSender = sender
    val fut: Future[Any] = (remoteActor ? a) 
    fut.onSuccess {
      case list: SequenceObject => {
        originalSender ! SequenceObject(list.sqnce)
   }    
}

远程演员代码是:

class ServerActorClass extends Actor {
  def receive = {
    case a: iNeedAnArrayBufferObject => {
      val closer = sender()

      closer ! SequenceObject(ArrayBufferObject[information])
    }
  }

以上似乎不起作用。远程参与者和本地参与者可以进行通信并正确接收消息。但是,可迭代对象永远不会发送回fut1。这是为什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

询问:发送和接收未来部分

中查看pipeTo pattern