I'm getting null pointer exception on the field injection of a server which is started as an akka actor.
Schedular part:
private ActorRef myActor = Akka.system().actorOf(
new Props(Retreiver.class));
@Override
public void onStart(Application app) {
log.info("Starting schedular.....!");
Akka.system()
.scheduler()
.schedule(Duration.create(0, TimeUnit.MILLISECONDS),
Duration.create(30, TimeUnit.MINUTES), myActor, "tick",
Akka.system().dispatcher());
}
Retreiver class part:
public class Retreiver extends UntypedActor {
private Logger.ALogger log = Logger.of(Retreiver .class);
@Inject
private myDataService dataService;
@Override
public void onReceive(Object arg0) throws Exception {
if (0 != dataService.getDataCount()) {
....
....
....
}
}
I'm getting null for dataService. Please advice me on this.
Thanks.
You're getting the
NullPointerException
because Akka is instantiating yourRetriever
actor and not Guice. You need to get Guice to construct your instance and then pass that to Akka,IndirectActorProducer
can help you achieve this, e.g.:Note that
produce()
must create a newActor
instance each time it is invoked, it cannot return the same instance.You can then get Akka to retrieve your actor through the
RetrieverDependencyInjector
, e.g.:UPDATE
I thought about you comment further, you might be able to turn
RetrieverDependencyInjector
into aGenericDependencyInjector
by providing the class of theActor
you want as a constructor parameter, that perhaps will allow you to do something like:I haven't tried this, but it might give you a starting point.
For anyone who needs this:
}
AND
Thats it...!!!
There can be other ways, eg, you can put a static injector handle in Boot or Some-Injector-Holder-class, and inject when you create the actor by call the inherited method: preStart ()
and also, you can also inject the injector in actor provider to initialize the actor by the injector in a scope of UntypedActorFactory: