Bind an actor factory.
Bind an actor factory.
This is useful for when you want to have child actors injected, and want to pass parameters into them, as well as have Guice provide some of the parameters. It is intended to be used with Guice's AssistedInject feature.
Let's say you have an actor that looks like this:
class MyChildActor @Inject() (db: Database, @Assisted id: String) extends Actor { ... }
So db
should be injected, while id
should be passed. Now, define a trait that takes the id, and returns
the actor:
trait MyChildActorFactory { def apply(id: String): Actor }
Now you can use this method to bind the child actor in your module:
class MyModule extends AbstractModule with AkkaGuiceSupport { def configure = { bindActorFactory[MyChildActor, MyChildActorFactory] } }
Now, when you want an actor to instantiate this as a child actor, inject MyChildActorFactory
:
class MyActor @Inject() (myChildActorFactory: MyChildActorFactory) extends Actor with ActorInject { def receive { case CreateChildActor(id) => val child: ActorRef = injectActor(myChildActoryFactory(id)) sender() ! child } }
The class that implements the actor that the factory creates
The class of the actor factory