Search code examples
playframeworkplayframework-2.0actor

Injecting Configuration and Service into an Actor


Following the documentation on Play's website, my Actor is configured as follows;

public class SuggestionActor extends UntypedActor {


    public static Props props = Props.create(SuggestionActor.class);

    private DAOService service;
    private Configuration config;

    @Inject
    public SuggestionActor(DAOService service, Configuration config) {
        this.service = service;
        this.config = config;
    }


    @Override
    public void onReceive(Object msg) throws Exception {
        if(msg instanceof SayHello) {
            // check if msg comes
            Logger.info(((SayHello) msg).name + config.getString("dao.mode"));
        }
    }
}

-

public class ActorProtocols {

    public static class SayHello{
        public final String name;


        public SayHello(String name) {
            this.name = name;
        }
    }
}

MyModule (enabled in application.conf)

public class MyModule extends AbstractModule implements AkkaGuiceSupport {

    @Override
    protected void configure() {
        bindActor(SuggestionActor.class, "suggestion-actor");
    }
}

My Controller

@Singleton
public class SuggestionController extends Controller {

    private static Logger.ALogger LOGGER = Logger.of(SuggestionController.class);

    @Inject @Named("suggestion-actor")
    private ActorRef suggestionActor;

    public Result suggest(String message) {
         ask(suggestionActor, new SayHello(message), 10000);
    }
}

If I try to Inject my DAO object and Configuration via constructor injection in SuggestionActor, Play throws a Caused by: java.lang.IllegalArgumentException: no matching constructor found on class actors.SuggestionActor for arguments []

Ideas?


Solution

  • I was trying to use Guice to inject components into SuggestionActor whilst using Props at the same time (in my controller). Once I removed Props, everything started working fine. Note, if you want to use Props, follows this SO Post.