Scala akka演员能像这样把消息传回给客户吗
我有一些伪代码,我希望有经验的人能告诉我akka是否会这样使用,如果不会,我如何重新设计以适应akka的最佳实践Scala akka演员能像这样把消息传回给客户吗,scala,playframework,akka,Scala,Playframework,Akka,我有一些伪代码,我希望有经验的人能告诉我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 to
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
}
}
因此模式基本上是我将创建一篇文章
,它将有一个单词
对象。然后,word
对象将开始进行一些解析,有时它需要将数据传递回Article
对象以“通过通信共享内存”,就像Go的协同程序一样
我知道Scala有一个收件箱类型的系统,但我们不打算使用Akka,但我发现Akka在示例等方面的文档记录很差
有人能告诉我上述设计是否可行吗?我可以在游戏应用程序中使用这个吗?给你一个示例。
这里,标记器获取要解析的项的seq,并将它们交给解析器进行解析。
解析器然后将结果报告给标记器
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()
}
你为什么不想在未来内部进行文字解析?顺便说一句,我如何使用logback添加日志记录?我收到了这条[INFO]消息
[INFO][05/15/2014 14:51:23.293][mine akka.actor.default-dispatcher-4][akka://mine/user/$a]来自Actor的消息[java.lang.String][akka://mine/user/$a/$a#1727619572]给演员[akka://mine/user/$a#1651939709]没有交货。[2] 遇到死信。可以使用配置设置“akka.log dead letters”和“akka.log dead letters in shutdown”关闭或调整此日志记录。
这是两条终止的消息,一条来自解析器,另一条来自标记器。没有人被终止订阅,所以这些信息就进入了死信信箱。请在你熟悉演员的时候暂时忽略它。您将看到许多内部消息。如果您想接收终止的消息和其他生命周期消息,请在示例代码中使用context.watch(kid)订阅。