斯卡拉演员称演员有麻烦

时间:2015-03-17 13:56:54

标签: scala akka actor

我开始与akka演员合作,编写代码,什么有用,但不完全是我期望的:

import com.mongodb.casbah.Imports
import com.mongodb.casbah.Imports._
import org.bson.types.ObjectId
import org.joda.time.DateTime
import org.joda.time.DateTimeZone.UTC
import com.mongodb.casbah.commons.conversions.scala._
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props



object Commands{
  case class GetActivities(uid: String)
  case class GetIds(activities: Iterable[DBObject])
  case class GetDataForIds(data: Map[String, List[ObjectId]])

}

class Commands extends Actor {
  def receive = {
    case Commands.GetActivities(uid) =>
      sender ! Commands.GetActivities(uid)
    case Commands.GetIds(activities) =>
      sender ! Commands.GetIds(activities)
    case Commands.GetDataForIds(data) =>
      sender ! Commands.GetDataForIds(data)
  }
}

class Activities extends Actor{

  override def preStart(): Unit = {

    val commands = context.actorOf(Props[Commands], "commands")

    commands ! Commands.GetActivities("53f76b6553544244db060000")
  }

  def receive = {
    case Commands.GetActivities(uid) => {
      println(get(uid))
      println("get activities")
      println("------------")
      sender ! Commands.getIds(get(uid))
    }
  }
  def get(userId: String): Iterable[DBObject] ={}
}

class Ids() extends Actor{
  def receive = {
    case Commands.GetIds(activities) => {
      println(getIds(activities))
      println("get ids")
      println("------------")
      sender ! Commands.GetDataForIds(getIds(activities))
    }
  }
  def getIds(activities: Iterable[DBObject]): Map[String, List[ObjectId]] =
}

class DataForIds() extends Actor{
  def receive = {
    case Commands.GetDataForIds(activities) => println(getDataForIds(activities))
  }

  def getDataForIds(data: Map[String, List[ObjectId]]): Map[String, Any] = {}

object Driver {

  def main{
    val system = ActorSystem("Main")
    val ac = system.actorOf(Props[Activities])
  }

}

当我编译并运行Driver.main时 - 我只得到

println(get(uid))
println("get activities")
println("------------")

过程看起来像冻结没有CTR没有CTRL + C

但在运行函数Activities后,我想在case Commands.GetActivities(uid)get(uid)启动演员Ids - 我做错了什么? 概念是Ids.getIds( Activities.get(uid) )

1 个答案:

答案 0 :(得分:0)

非常感谢@ Sarvesh-Kumar-Singh,以及答案

class Commands extends Actor {
  def receive = {
    case Commands.GetActivities(uid) =>
      sender ! Commands.GetActivities(uid)
    case Commands.GetIds(activities) =>
      val ids = context.actorOf(Props[Ids], "ids")
      ids ! Commands.GetIds(activities)
    case Commands.GetDataForIds(data) =>
      val dataforids = context.actorOf(Props[DataForIds], "daraforids")
      dataforids ! Commands.GetDataForIds(data)
  }
}