似乎没有必要在类中使用main方法来运行Akka How to run akka actors in IntelliJ IDEA。但是,这就是我所拥有的:
object Application extends App {
val system = ActorSystem()
val supervisor = system.actorOf(Props[Supervisor])
implicit val timeout = Timeout(100 seconds)
import system.dispatcher
system.scheduler.schedule(1 seconds, 600 seconds) {
val future = supervisor ? Supervisor.Start
val list = Await.result(future, timeout.duration).asInstanceOf[List[Int]]
supervisor ! list
}
}
我知道我必须在配置中指定一个名为“akka.Main”的main方法。但是,我应该从object Application
移动当前代码?
答案 0 :(得分:5)
您可以编写类似
的内容import _root_.akka.Main
object Application extends App {
Main.main(Array("somepackage.Supervisor"))
}
和主管演员应该将{prestart函数重写为@cmbaxter建议。
然后在intellij中运行sbt console并编写run。
答案 1 :(得分:4)
我同意@kdrakon你的代码很好,但如果你想利用akka.Main
功能,那么像这样的简单重构会使事情有效:
package code
class ApplicationActor extends Actor {
override def preStart = {
val supervisor = context.actorOf(Props[Supervisor])
implicit val timeout = Timeout(100 seconds)
import context.dispatcher
context.system.scheduler.schedule(1 seconds, 600 seconds) {
val future = (supervisor ? Supervisor.Start).mapTo[List[Int]]
val list = Await.result(future, timeout.duration)
supervisor ! list
}
}
def receive = {
case _ => //Not sure what to do here
}
}
在这种情况下,ApplicationActor
是您要传递给akka.Main
的arg,它基本上是您层次结构中创建的所有其他actor的根管理员。这里唯一可疑的是作为Actor
,它需要receive
实现,我不认为任何其他演员会在这里发送消息,因此它实际上没有做任何事情。但是这种方法的强大之处在于,当ApplicationActor
停止时,停止也将被级联到其启动的所有其他actor,从而简化了正常关闭。我想你可以让ApplicationActor
处理一个消息来关闭actor系统给定某种输入(也许ShutdownHookThread
可以启动它)并且毕竟给这个actor一些目的。无论如何,如前所述,你目前的方法看起来很好,但如果你愿意的话,这也可能是一个选择。
修改强>
因此,如果您想通过ApplicationActor
运行此akka.Main
,请按照here说明从命令提示符执行此操作:
java -classpath <all those JARs> akka.Main code.ApplicationActor
您当然需要向<all those JARS>
提供您的依赖项,包括akka。您至少需要在类路径中使用scala-library
和akka-actor
来进行此操作。
答案 2 :(得分:1)
如果你引用http://doc.akka.io/docs/akka/snapshot/scala/hello-world.html,你会发现akka.Main需要你的root / parent Actor。在你的情况下,主管。至于你现有的代码,它可以直接复制到actor代码中,可能在一些初始化调用中。例如,请参阅HelloWorld的preStart函数。
但是,在我看来,您现有的代码也很好。 Akka.main是一个很好的帮手,微内核二进制文件也是如此。但是创建自己的主要可执行文件也是一个可行的选择。