public interface AkkaGuiceSupport
public class MyModule extends AbstractModule implements AkkaGuiceSupport { protected void configure() { bindActor(MyActor.class, "myActor"); } }Then to use the above actor in your application, add a qualified injected dependency, like so:
public class MyController extends Controller { @Inject @Named("myActor") ActorRef myActor; ... }
Modifier and Type | Method and Description |
---|---|
default <T extends akka.actor.Actor> |
bindActor(java.lang.Class<T> actorClass,
java.lang.String name)
Bind an actor.
|
default <T extends akka.actor.Actor> |
bindActor(java.lang.Class<T> actorClass,
java.lang.String name,
java.util.function.Function<akka.actor.Props,akka.actor.Props> props)
Bind an actor.
|
default <T extends akka.actor.Actor> |
bindActorFactory(java.lang.Class<T> actorClass,
java.lang.Class<?> factoryClass)
Bind an actor factory.
|
default <T extends akka.actor.Actor> void bindActor(java.lang.Class<T> actorClass, java.lang.String name, java.util.function.Function<akka.actor.Props,akka.actor.Props> props)
actorClass
- The class that implements the actor.name
- The name of the actor.props
- 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.default <T extends akka.actor.Actor> void bindActor(java.lang.Class<T> actorClass, java.lang.String name)
actorClass
- The class that implements the actor.name
- The name of the actor.default <T extends akka.actor.Actor> void bindActorFactory(java.lang.Class<T> actorClass, java.lang.Class<?> factoryClass)
public class MyChildActor extends UntypedActor { final Database db; final String id; @Inject public MyChildActor(Database db, @Assisted String id) { this.db = db; this.id = id; } ... }So
db
should be injected, while id
should be passed. Now, define an interface that takes the id,
and returns the actor:
public interface MyChildActorFactory { MyChildActor apply(String id); }Now you can use this method to bind the child actor in your module:
public class MyModule extends AbstractModule implements AkkaGuiceSupport { protected void configure() { bindActorFactory(MyChildActor.class, MyChildActorFactory.class); } }Now, when you want an actor to instantiate this as a child actor, inject `MyChildActorFactory`:
public class MyActor extends UntypedActor implements InjectedActorSupport { final MyChildActorFactory myChildActorFactory; @Inject public MyActor(MyChildActor myChildActorFactory) { this.myChildActorFactory = myChildActorFactory; } public onReceive(Object msg) { if (msg instanceof CreateChildActor) { ActorRef child = injectedChild(myChildActorFactory.apply(((CreateChildActor) msg).getId())); sender().send(child, self); } } }
actorClass
- The class that implements the actor.factoryClass
- The factory interface for creating the actor.