Blocking calls in Akka Actors

前端 未结 3 499
旧巷少年郎
旧巷少年郎 2020-12-23 14:33

As a newbie, I am trying to understand how actors work. And, from the documentation, I think I understand that actors are objects which gets executed in sync mode and also t

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 14:58

    It really depends on the use-case. If the queries do not need to be serialized, then you can execute the query in a future and send the results back to the sender as follows:

    import scala.concurrent.{ future, blocking}
    import akka.pattern.pipe
    
    val resFut = future {
      blocking {
        executeQuery()
      }
    }
    
    resFut pipeTo sender
    

    You could also create a dedicated dispatcher exclusively for the DB calls and use a router for actor creation. This way you can also easily limit the number of concurrent DB requests.

提交回复
热议问题