I am running into an issue using Props.create when providing a class and its arguments. The example below is contrived but will hopefully serve its purpose.
trait Provider {}
class DefaultProvider extends Provider {}
class MyActor(provider: Provider) extends Actor {
def receive = ...
}
Then let's say I am injecting the provider using Guice or another container:
class ProviderPropsFactory @Inject()(protected val provider: Provider) {
def buildProps: Props = Props.create(classOf[MyActor], provider)
}
At runtime if I try to start an actor using these props as shown here, I get an IllegalArgumentException when Akka attempts to find the matching constructor for the actor since it is only seems to match on the concrete type rather than allowing a compatible trait or an abstract type from the instance's hierarchy.
system.actorOf(factory.buildProps)
java.lang.IllegalArgumentException: no matching constructor found on class com.sample.MyActor for arguments [class com.sample.DefaultProvider]
at akka.util.Reflect$.error$1(Reflect.scala:82) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.util.Reflect$.findConstructor(Reflect.scala:106) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.actor.ArgsReflectConstructor.<init>(Props.scala:353) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.actor.IndirectActorProducer$.apply(Props.scala:312) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.actor.Props.producer(Props.scala:179) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.actor.Props.<init>(Props.scala:192) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
at akka.actor.Props$.create(Props.scala:99) ~[akka-actor_2.11-2.3-bin-rp-15v01p06.jar:na]
I could use this snippet below instead which I know would work, but this type of approach is discouraged/deprecated and I would like to be doing things the recommended way. Do you have any suggestions?
class ProviderPropsFactory @Inject()(protected val provider: Provider) {
def buildProps: Props = Props(new MyActor(provider))
}
Any and all help is appreciated.
Thanks,
Steve