Bind an actor.
Bind an actor.
This will cause the actor to be instantiated by Guice, allowing it to be dependency injected itself. It will bind the returned ActorRef for the actor will be bound, qualified with the passed in name, so that it can be injected into other components.
The class that implements the actor.
The name of the actor.
A function to provide props for the actor. The props passed in will just describe how to create the actor, this function can be used to provide additional configuration such as router and dispatcher configuration.
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 InjectedActorSupport { def receive { case CreateChildActor(id) => val child: ActorRef = injectedChild(myChildActorFactory(id), id) sender() ! child } }
The class that implements the actor that the factory creates
The class of the actor factory
Support for binding actors with Guice.
Mix this trait in with a Guice AbstractModule to get convenient support for binding actors. For example:
Then to use the above actor in your application, add a qualified injected dependency, like so: