How to introduce services to participants using Play 2.4?

I can implement services in my application class without any problems. But for some reason I can not go into the actors themselves.

My actor:

class PollerCrow @Inject()(
     @Named("pollService") pollService: PollService[List[ChannelSftp#LsEntry]]
     , @Named("redisStatusService") redisStatusService: StatusService
     , @Named("dynamoDBStatusService") dynamoDbStatusService: StatusService
) extends BaseCrow {
... impl and stuff ...
}

My companion object for actors:

object PollerCrow extends NamedActor {
  override def name: String = this.getClass.getSimpleName

  val filesToProcess = ConfigFactory.load().getString("poller.crow.files.to.process")    
  def props = Props[PollerCrow]
}

I get the following when I run it:

IllegalArgumentException: no matching constructor found on class watcher.crows.PollerCrow for arguments []

How can i fix this?

Edit:

I tied my actors:

class ActorModule extends AbstractModule with AkkaGuiceSupport {

  override def configure() {
    bindPollerActors()
  }

  private def PollActors() = {
    bindActor[PollerCrow](PollerCrow.name)
  }
}

Edit 2:

Additional class information:

abstract class BaseCrow extends Crow with Actor with ActorLogging

class PollerCrow @Inject()(
            @Named(ServiceNames.PollService) pollService: PollService[List[ChannelSftp#LsEntry]]
          , @Named(ServiceNames.RedisStatusService) redisStatusService: StatusService
          , @Named(ServiceNames.DynamoDbStatusService) dynamoDbStatusService: StatusService
) extends BaseCrow {

  override def receive: Receive = {
    ...
  }
}

object PollerCrow extends NamedActor {
  override def name: String = this.getClass.getSimpleName

  def props = Props[PollerCrow]
}

trait NamedActor {
  def name: String
  final def uniqueGeneratedName: String = name + Random.nextInt(10000)
}
+4
source share
1 answer

You can make Guiceactors aware of you. This is a clean approach:

import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport


class ActorModule extends AbstractModule with AkkaGuiceSupport {
  override def configure(): Unit =  {
    bindActor[YourActor]("your-actor")
  }
}

@Singleton
class YourActor @Inject()(yourService: IYourService) extends Actor {

  override def receive: Receive = {
    case msg => unhandled(msg)
  }

}

And application.conf:

play.modules {
  enabled += "ActorModule"
}

For those who don't want the hassle, just call the injector directly and don't forget to import Applicationinto scope:

Play.application.injector.instanceOf[YourService]
Play.application.injector.instanceOf(BindingKey(classOf[YourService]).qualifiedWith("your-name"));
+4

Source: https://habr.com/ru/post/1608712/


All Articles