-->

why cant I bind to single actor instance (akka rou

2019-09-18 06:20发布

问题:

I´m currently struggeling with implementing my Akka router logic using scaldi for dependency injection. Why cant I bind to a single actor instance with scaldi, since my actor is a router and I only want to have one single instance of it? The way I came to ask this question was another stackoverflow entry.

My scaldi Module:

class DAOModule extends Module {
  bind toProvider new UserDaoWorker
  binding to new UserDaoRouter
}

This way only one instance is created and as soon as I inject my router multiple times it gets a dead letter actor as sender from the sender() method.

When I change the binding to...

binding toProvider new UserDaoRouter

... it works perfectly fine, but every injection means a new instance of my router. Am I right?

So how can I achieve having only a single instance of my router which is injectable?

Thanks in advance

回答1:

May be like this: bind [UserDaoRouter] to new UserDaoRouter ??



回答2:

This is what worked for me:

class DAOModule extends Module {
  binding toProvider new UserDaoWorker
  binding toProvider new UserDaoRouter
  binding identifiedBy 'singletonUserRouter to {
    implicit val system = inject[ActorSystem]
    AkkaInjectable.injectActorRef[UserDaoRouter]
  }
}

And then in my controller:

val userDaoRouter = inject[ActorRef] ('singletonUserRouter)

I hope this will help someone else ;)