How to implement factory of Akka actors?

Skip to first unread message

Shivam Kapoor

May 20, 2019, 3:05:45 AM5/20/19
to Akka User List

Already asked this question on Stackoverflow:

Let's say I have DAO Actors (CassDaoActor, VerticaDaoActor, etc) that respond to message 'Read'.

First of all, is there a way to express a interface or abstract class that defines the message 'Read' that extending actors should implement?

Now assume it's only at runtime that I could get to know which Actor needs to be created based on the configured db. For example, if configured db is cassandra, I need to create CassDaoActor, etc. This apparently is a typical use case for Factory Method Pattern as we know. I want to understand how can we implement such a thing? Evidently we can't pass "context" around since it looses content outside the scope of the actor.

Please suggest.

What I have tried so far is that I am returning respective props based on the configured db to the actor within which I need to create these actors.

object `package` {
  val CASS = "cass"
  val VERTICA = "vertica"

  def getDAOProps(db: String): Props = db match {
    case CASS => CassDaoActor.props
    case VERTICA => VerticaDaoActor.props

// SupervisorActor
val db = configuredDb()
context.actorOf(getDAOProps(db), db)
Reply all
Reply to author
0 new messages