I have read many example of definition of actors class to build a program. What I notice is all of them use ActorRef
as paramaters type. For example,
class LogProcessor(dbWriter: ActorRef)
extends Actor with ActorLogging with LogParsing {
import LogProcessor._
def receive = {
case LogFile(file) =>
val lines: Vector[DbWriter.Line] = parse(file)
lines.foreach(dbWriter ! _)
}
}
the dbWriter
's actual type is DBWriter
defined as:
class DbWriter(databaseUrl: String) extends Actor {
val connection = new DbCon(databaseUrl)
import DbWriter._
def receive = {
case Line(time, message, messageType) =>
connection.write(Map('time -> time,
'message -> message,
'messageType -> messageType))
}
override def postStop(): Unit = {
connection.close()
}
}
At the example, why not use DbWriter
as dbWriter's type?
If we use ActorRef
anywhere, out program just look like a weak type system.
Besides, Akka actor use actorOf
method to create child actor and it return ActorRef.I also confuse why not return the real type of the actor?
Some comments about actors: