Scala 演员叫演员麻烦
Scala actor call an actor trouble
我开始与 akka actor 一起工作,并编写代码,哪些有效,但并不完全符合我的预期:
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("------------")
如果没有 CTRL+C,进程看起来像冻结没有结束
但我想在 Activities
中 case Commands.GetActivities(uid)
之后 运行ning 函数 get(uid)
启动 actor Ids
- 我做错了什么?
概念是Ids.getIds( Activities.get(uid) )
非常感谢@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)
}
}
我开始与 akka actor 一起工作,并编写代码,哪些有效,但并不完全符合我的预期:
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("------------")
如果没有 CTRL+C,进程看起来像冻结没有结束
但我想在 Activities
中 case Commands.GetActivities(uid)
之后 运行ning 函数 get(uid)
启动 actor Ids
- 我做错了什么?
概念是Ids.getIds( Activities.get(uid) )
非常感谢@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)
}
}