Java 8, Guice 4.0 and Akka 2.3.9 here. I am trying to figure out how to annotate my actor classes with JSR330-style @Inject
annotations, and then wire them all up v
Use Creator to create ActorRef
s in provider methods of your guice module. To distinguish between the different ActorRef
s, which are untyped, use annotations on your provider methods and injection points as you would any guice system. For example,
In your guice module:
@Override
protected void configure() {
bind(ActorSystem.class).toInstance(ActorSystem.apply());
bind(FizzService.class).toInstance(new FizzServiceImpl());
}
@Provides @Singleton @Named("fizzActor")
ActorRef serviceActorRef(final ActorSystem system, final FizzService fizzService) {
return system.actorOf(Props.create(new Creator() {
@Override
public Actor create() throws Exception {
return new FizzActor(fizzService);
}
}));
}
Then to use the actor service, inject a specific ActorRef
:
class ClientOfFizzActor {
@Inject
ClientOfFizzActor(@Named("fizzActor") ActorRef fizzActorRef) {..}
}
It looks cleaner if the Props.create(..)
clause is a static factory method in your actor class.