Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka classic vs Akka typed [closed]

Tags:

scala

akka

Now I am learning Akka. But I found that there are two versions of Akka: Classic and Typed. What are differences between it and what I should learn?

Is typed version having stronger type control? If yes, why is it introducing only in 2.6 version?

Thanks for answering.

like image 980
f4x Avatar asked Oct 22 '25 11:10

f4x


1 Answers

"Classic" Actors are untyped. You define an actor that accepts Any object, and use pattern matching to define which messages it will actually handle.

// from https://doc.akka.io/docs/akka/current/actors.html
class MyActor extends Actor {
  val log = Logging(context.system, this)

  def receive = {
    case "test" => log.info("received test")
    case _      => log.info("received unknown message")
  }
}

receive that you have to define for each actor is PartialFunction[Any, Unit]. It doesn't prevent you from sending message to a wrong actor - one that will silently ignore it. It doesn't prevent you from sending the wrong type of message. You can send everything to everyone.

From a certain point of view you have a concurrency model - build on top of a strongly-typed language - which is as strongly typed as JavaScript.

What Akka Typed does, is that it makes you send messages through typed wrappers around untyped actors:

// from https://doc.akka.io/docs/akka/current/typed/actors.html#akka-actors
object HelloWorld {
  final case class Greet(whom: String, replyTo: ActorRef[Greeted])
  final case class Greeted(whom: String, from: ActorRef[Greet])

  def apply(): Behavior[Greet] = Behaviors.receive { (context, message) =>
    context.log.info("Hello {}!", message.whom)
    message.replyTo ! Greeted(message.whom, context.self)
    Behaviors.same
  }
}

object HelloWorldBot {

  def apply(max: Int): Behavior[HelloWorld.Greeted] = {
    bot(0, max)
  }

  private def bot(greetingCounter: Int, max: Int): Behavior[HelloWorld.Greeted] =
    Behaviors.receive { (context, message) =>
      val n = greetingCounter + 1
      context.log.info2("Greeting {} for {}", n, message.whom)
      if (n == max) {
        Behaviors.stopped
      } else {
        message.from ! HelloWorld.Greet(message.whom, context.self)
        bot(n, max)
      }
    }
}

This way you have:

  • typed actors (Behavior) which tells you what kind of messages they accept
  • typed ActorRef which will help you send only the right kind of message to an actor
  • typed ActorSystem which defines what is the kind of messages you can send directly to an actor system.

It was introduced only "recently" but the work on it lasted for a few years, its just only recently the code reached maturity intended by its creators. The module existed in repo for at least 2 years but from what I remember the work on it lasted already for a while. First issue labelled as concerning Akka Typed was from May 2017 but work on anything that was "typed actor" can be traced to as early as May 2014 (which means that authors had to talk about it for a while already, though these earlier concepts were slightly different that what we have today).

Long story short - typed is (for most people, most of the time, YMMV) better, but it took time to get it right. If you want to use them in a greenfield project (that have to use actors for some reason) then I would suggest Typed. If you have to work in an existing project with Actors, then almost certainly they will use classic, so you'll have to know both if you want to make a living out of Akka.

like image 84
Mateusz Kubuszok Avatar answered Oct 24 '25 06:10

Mateusz Kubuszok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!