Akka and ReactiveMongo

前端 未结 2 858
挽巷
挽巷 2021-02-03 12:02

I am trying to find the best approach to sharing the same pool of connection between actors withing the cluster workers. I have the following structure:

Master Actor ->

2条回答
  •  温柔的废话
    2021-02-03 12:31

    I would create the driver and connection in the master actor. I would then set up the the worker actors to take an instance of MongoConnection as a constructor argument so that each worker has a reference to the connection (which is really a proxy to a pool of connections). Then, in something like preStart, have the master actor create the workers (which I am assuming are routed) and supply the connection as an arg. A very simplified example could look like this:

    class MongoMaster extends Actor{
      val driver = new MongoDriver
      val connection = driver.connection(List("localhost"))
    
      override def preStart = {
        context.actorOf(Props(classOf[MongoWorker], connection).withRouter(FromConfig()))
      } 
    
      def receive = {
        //do whatever you need here
        ...
      }
    }
    
    class MongoWorker(conn:MongoConnection) extends Actor{
      def receive = {
        ...
      }
    }
    

    This code is not exact, but at least it shows the high level concepts I described.

提交回复
热议问题