Why do you think that the `Props` factory taking the actor constructor by name is discouraged? It’s the opposite: You definitely SHOULD use it, the other approach is using reflection at runtime and is not refactoring safe.
Cheers Heiko -- Heiko Seeberger Home: heikoseeberger.de <http://heikoseeberger.de/> Twitter: @hseeberger <https://twitter.com/hseeberger> Public key: keybase.io/hseeberger <https://keybase.io/hseeberger> > On 01 Feb 2016, at 20:46, steven richardson <[email protected]> > wrote: > > 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 > > -- > >>>>>>>>>> Read the docs: http://akka.io/docs/ <http://akka.io/docs/> > >>>>>>>>>> Check the FAQ: > >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html > >>>>>>>>>> <http://doc.akka.io/docs/akka/current/additional/faq.html> > >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > >>>>>>>>>> <https://groups.google.com/group/akka-user> > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > Visit this group at https://groups.google.com/group/akka-user > <https://groups.google.com/group/akka-user>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
