Multiple Implementation of Actors in Scala

Reading Time: 2 minutes

Actor in Scala have multiple way of implementations. Here we are going to discuss about multiple ways of actors implementations.

Typesafe Stack combines the Scala language with the Akka framework.

Nowthough Scala has actors in its standard library, Akka uses its own implementation. And, if we look for other implementations, We can find that Lift and Scalaz have implementations too!

Design Philosophy of Actor

Scalaz Actors

Minimal complexity. Maximal generality, modularity and extensibility.

Lift Actors

Lift is an expressive and elegant framework for writing web applications. It stresses the importance of security, maintainability, scalability and performance, while allowing for high levels of developer productivity.

minimal complexity, Garbage Collection by JVM rather than worrying about an explicit lifecycle. Error handling behaviour consistent with other Scala & Java programs,

object ScheduledTask extends LiftActor {

  case class DoNow()
  case class StopIt()

  private var stopped = false

   def messageHandler = {
     case DoNow if !stopped =>
        Schedule.schedule(this, DoNow, 10 minutes)
       // ... do useful work here

     case StopIt =>
       stopped = true
   }
}

Scala Actors

Provide the full Erlang actor model in Scala, lightweight/small memory footprint.

Akka Actors

Akka Actors have simple and transparently distributable, high performance, lightweight and highly adaptable.

The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems

object HelloMain {

  final case class SayHello(str: String)

  def apply(): Behavior[SayHi] =
    Behaviors.setup { context =>
      val greets = context.spawn(HelloWorld(), "greets")

      Behaviors.receiveMessage { msg =>
        val replyTo = context.spawn(HelloWorldBot(max = 3), msg.name)
        greeter ! HelloWorld.Greeting(msg.name, replyTo)
        Behaviors.same
      }
    }

}

Level of state isolation

If user defines public methods on their Actors, are they callable from the outside?

  • Scalaz Actors: n/a. Actor is a sealed trait.
  • Lift Actor : Yes
  • Scala Actors: Yes
  • Akka Actors: No, actor instance is shielded behind an ActorRef.

Message processing

Supports nested receives?

  • Scalaz Actors: —
  • Lift : Yes (with a little hand coding).
  • Scala Actors: both thread-based receive and event-based react.
  • Akka Actors: No, nesting receives can lead to memory leaks and degraded performance over time.

Message send modes

                    Scalaz Actors   Lift Actors     Scala Actors    Akka Actors
Fire-forget         a ! message     actor ! msg     actor ! msg     actorRef ! msg
                    a(message)
Send-receive-reply                  actor !? msg    actor !? msg    actorRef !! msg
                                    actor !! msg

Send-receive-future                                 actor !! msg    actorRef !!! msg

Send-result-of-     promise(message).                               future.onComplete( f => to ! f.result )
future              to(actor)
Compose actor with  actor comap f   No              No              No
function            
knoldus

Written by 

Vipul Kumar is Senior Software Consultant having 6years of experience. He has knowledge of languages like Functional Java, Scala, Akka, Akka-http. He is familiar with Object-Oriented Programming Paradigms and also has an interest in Functional Programming technologies. His hobbies include travelling, riding bikes and listening to music.