我试图编译这段代码,以了解角色的实际工作方式,但它给了我一条错误消息: value actorOf不是object akka.actor.Actor的成员
我是akka的新手,实际上不能理解如何开始和了解实现整个定义的结构,比如每个类的接收方法。另外,ActorLogging的实际工作是什么?有谁能帮帮忙吗?谢谢。
import akka.actor.Actor
case object Start
object Main {
def main(args: Array[String]): Unit = {
println("Start");
val echo = Actor.actorOf[EchoActor]
echo.start()
echo ! "Hello World"
echo.stop()
}
}
class EchoActor extends Actor {
def receive = {
case s: String => println(s)
}
}发布于 2013-12-12 04:59:09
看起来你正在尝试使用(我认为) Akka 1.x时代的代码和Akka 2.x。一些语义有很大的不同。以下是正确实现它的一种方法:
import akka.actor._
object Main {
def main(args: Array[String]) {
println("Start");
val system = ActorSystem("hello-world")
val echo = system.actorOf(Props[EchoActor])
echo ! "Hello World"
echo ! PoisonPill
}
}
class EchoActor extends Actor with ActorLogging {
override def preStart() {
log.info("starting actor")
}
def receive = {
case s: String => log.info(s)
}
override def postStop() {
log.info("stopping actor")
context.system.shutdown
}
}基本上,您需要使用actor系统来创建actor,而不再需要显式启动。发送消息的方式与此相同。要停止执行元,可以向其发送PoisonPill对象,该对象将导致执行元在到达其邮箱中的消息时立即关闭。
另外,我向执行元添加了一个停止后钩子,以便在执行元停止时关闭系统,否则应用程序将永远不会退出。
ActorLogging特性允许您轻松地将参与者连接到Akka的日志记录框架。当您将其混合到actor中时,您将获得一个可以像常规记录器一样使用的log。
发布于 2013-12-12 04:57:47
您可能需要先查看Akka文档。这是链接:http://doc.akka.io/docs/akka/snapshot/scala/actors.html
你想做的是:
import akka.actor.ActorSystem
// ActorSystem is a heavy object: create only one per application
val system = ActorSystem("mySystem")
val myActor = system.actorOf(Props[MyActor], "myactor2")(摘自文档)
因此,首先要创建一个actor系统,并且应该使用该系统来创建Actor。
https://stackoverflow.com/questions/20529366
复制相似问题