public interface EventService{public class EventServiceImpl implements EventService{ public String whoAmI(){
return "I am the Event Service"; }}
public class EventSupervisor extends UntypedActor{
final String name; @Inject EventService eventService; public EventSupervisor(String name) { this.name = name; } @Override public void onReceive(Object msg) throws Exception{ if (msg instanceof model.Event) { Logger.info( String.format("Received message `%s` in actor %s", ((model.Event) msg).id , getSelf().path().name()) ); Logger.info(getSender().path().toString()); Logger.info(eventService.whoAmI()); } else unhandled(msg); }}package actor;
import com.google.inject.Injector;
import akka.actor.Actor;import akka.actor.IndirectActorProducer;
public class ActorDependencyInjector implements IndirectActorProducer{
final Injector injector; final Class<? extends Actor> actorClass; public ActorDependencyInjector(Injector injector, Class<? extends Actor> actorClass){ this.injector = injector; this.actorClass = actorClass; } @Override public Class<? extends Actor> actorClass(){ return actorClass; }
@Override public Actor produce(){ return injector.getInstance(actorClass); }
}public class EventController extends Controller{ static final ActorRef eventRouter2 = Akka.system().actorOf(Props.create(ActorDependencyInjector.class, Guice.createInjector(Stage.PRODUCTION, new GuiceModule()),EventSupervisor.class));
.... Guice configuration errors:
1) No implementation for data.service.EventService was bound.
while locating data.service.EventService
for field at actor.EventSupervisor.eventService(EventSupervisor.java:10)
while locating actor.EventSupervisor
I will definitely give it a try. I just want to understand all the mechanics first. One more question. I see that the akka extension GuiceExtension has an initialize method that takes the injector. Where is that called? Are you using the the same injector that you use in the GlobalSettings to inject controllers? How are you passing it in?