我有一些伪代码,我希望一些有经验的人能告诉我,akka是否会像这样使用,如果不是,我如何重新设计我的设计,以适应akka的最佳实践。
class Article(val words: Word) {
val tokens: List[Word]
}
class Word(..) {
def parseWords() {
val word = .....
tokenActor ! word // this is suppose to add the word to the tokens List
}
}
因此,模式基本上是,我将创建一个Article
,它将有一个word
对象。然后,word
对象将开始进行一些解析,有时需要将数据传回给Article
对象,以“通过通信共享内存”,比如Go的协同。
我知道Scala有一个收件箱类型的系统,但是我们应该使用Akka,但是我发现Akka在示例等方面的文档能力很差。
有人能告诉我上面的设计是否可行吗?我能在游戏应用程序里面用这个吗?
发布于 2014-05-14 08:30:45
给你一份样品。在这里,Tokenizer获取要解析的项的seq,并将它们分发给Parser进行解析。解析器,而不是将结果报告给托卡器。
import akka.actor._
import com.typesafe.config.ConfigFactory
case object Go
case object Done
case object GoAway
class Tokenizer(items: Seq[String]) extends Actor {
val kid = context.actorOf(Props[Parser])
var tokens: List[String] = List.empty
override def receive: akka.actor.Actor.Receive = {
case Go => // start process
items.foreach(kid ! _)
kid ! GoAway
case Done => // report results
println(tokens.mkString(":"))
self ! PoisonPill // shut down
case s: String => // data from kid
tokens = s :: tokens
}
}
class Parser extends Actor {
override def receive: Receive = {
case GoAway =>
sender ! Done
self ! PoisonPill // or context.stop(self)
case s: String =>
s.split("\\.").foreach(sender ! _)
}
}
object G extends App {
val config = ConfigFactory.parseString("" +
"akka.loglevel=DEBUG\n" +
"akka.debug.lifecycle=on\n" +
"akka.debug.receive=on\n" +
"akka.debug.event-stream=on\n" +
"akka.debug.unhandled=on\n" +
""
)
val system = ActorSystem("mine", config)
val doer = system.actorOf(Props(new Tokenizer(Seq("191.168.1.1", "192.168.1.2"))))
doer ! Go
system.shutdown()
system.awaitTermination()
}
https://stackoverflow.com/questions/23657181
复制相似问题