问题描述
我的用例非常简单-在两个对象之间交换很少(
My use case is very simple - to exchange few (< 100) messages between two objects, Scala actors were excellent for this purpose (light weight, no complicated life cycle management, start/terminate at any time).
现在我正在从Scala演员迁移到Akka,但是我再也找不到那些轻量级的演员了!
Now I am migrating from Scala Actors to Akka, however I can no longer find those light weight Actors!
使用Akka,不仅需要创建ActorSystem / Props来创建Actor,还需要照顾ActorSystem的生命周期。
With Akka, not only I need to create ActorSystem/Props for Actor creation, but also need to take care of the life cycle of ActorSystem.
是否有Akka Actor?不需要复杂的ActorSystem生命周期管理?
Is there an Akka Actor which does not require complicated ActorSystem life cycle management?
推荐答案
您可以创建自己的默认 ActorSystem
在全局单例对象中,您不必手动停止它-它会在程序关闭时停止。
You could create your own default ActorSystem
in global singleton object and you don't have to stop it manually - it will stop on program shutdown.
UPD
请参见: //stackoverflow.com/users/958431/roland-kuhn\">Roland Kuhn 解决方案如何防止 ActorSystem
阻止JVM关闭。
See the answer by Roland Kuhn for solution how to prevent ActorSystem
from keeping the JVM from shutting down.
object global {
import com.typesafe.config.ConfigFactory
// From answer by Roland Kuhn
implicit lazy val actorSystem: ActorSystem =
ActorSystem("default", ConfigFactory.parseString("akka.daemonic=on"))
}
您可以将包对象
用于此目的。
您还可以使用用于轻量级Actor创建语法:
You could also use ActorDSL for light-weight actor creation syntax:
import ActorDSL._
import global._
val a = actor( new Act {
become {
case msg => ...
}
})
这篇关于阿卡(Akka)中有轻量演员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!