You get a NullPointerException because Akka creates your Retriever actor, not Guice. You need Guice to build your instance and then pass this to Akka, IndirectActorProducer can help you with this, for example:
class RetrieverDependencyInjector implements IndirectActorProducer { final Injector injector; public RetrieverDependencyInjector(Injector injector) { this.injector = injector; } @Override public Class<? extends Actor> actorClass() { return Retriever.class; } @Override public Retriever produce() { return injector.getInstance(Retriever.class); } }
Note that produce() must create a new instance of Actor every time it is called, it cannot return the same instance.
You can then get Akka to retrieve your actor through RetrieverDependencyInjector , for example:
ActorRef myActor = Akka.system().actorOf( Props.create(RetrieverDependencyInjector.class, injector) );
UPDATE
I was thinking about what you will comment further, you can turn RetrieverDependencyInjector into a GenericDependencyInjector by providing the Actor class that you want as a constructor parameter, which will probably allow you to do something like:
Props.create(GenericDependencyInjector.class, injector, Retriever.class)
I have not tried this, but it can give you a starting point.
source share