急切地初始化scalaguice中的单身演员

时间:2015-04-09 15:19:05

标签: scala akka guice

我想热切地初始化一个单身演员。我目前正在执行以下操作,然后在我的app启动中获取actor的实例。

`bind[Actor].annotatedWith(Names.named(LockCoordinator.name)).to[LockCoordinator].in[Singleton]`

我试过了 bind[Actor].annotatedWith(Names.named(LockCoordinator.name)).to[LockCoordinator].asEagerSingleton()

但在运行时失败

1) Error injecting constructor,   akka.actor.ActorInitializationException:   You cannot create an instance of [LockCoordinator] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.

我已经为系统创建了一个eagerSingleton,但是无法弄清楚如何将它应用于Actor(而不是ActorRef)

class ActorSystemProvider @Inject() (val config: Config, val injector: Injector) extends Provider[ActorSystem] {
override def get() = {

  val system = ActorSystem(config.getString("mysystem"), config)
  GuiceAkkaExtension(system).initialize(injector)
  system
}
}

是否有免费的样板文件来实现这一目标?因为我想将其应用于其他3-4位演员

1 个答案:

答案 0 :(得分:0)

这有帮助吗?

https://gist.github.com/fancellu/e4e8acdc3d7fd3b9d749352f9d6c68e3

import actors.ActorBrowserActor
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport

class Module extends AbstractModule with AkkaGuiceSupport{
  def configure(): Unit = {
    bindActor[ActorBrowserActor](ActorBrowserActor.NAME, _=>ActorBrowserActor.props)
  }
}

这里我们按名称注入演员

class SampleController @Inject()(implicit system: ActorSystem, val messagesApi: MessagesApi, @Named("actor-browser-actor") actorBrowserActor: ActorRef)
  extends Controller  with I18nSupport{
}