4
votes

I'm looking at using Akka's new typed channels as a way to maintain the actor model in my project without the pain of lots of explicit casts where you really do need to wait on a response.

I've had great luck creating typed channels and integrating them, but I'm so far not seeing any easy solutions for creating pools of these actors for parallel execution, which is easy to do with regular untyped actors. Do I need to build my own router system?

E.g., for untyped actors I can do something like this, and I magically get four actors.

akkaSystem.actorOf(Props[UntypedActor]
                   .withRouter(RoundRobinRouter(nrOfInstances=4)), "routed")

For typed channels, I have to do something like this:

ChannelExt(akkaSystem).actorOf(new TCActor, "singleton")

Obviously I could write a second typed channel that creates a pool of actors and rotates between them, but that seems like something that someone would have done before!

1
Note that typed channels were an experimental feature which we decided to remove in Akka 2.3.0-RC1: its implementation relied on an experimental feature of Scala for which there is no correspondence in Java and other languages and its usage was not intuitive. - Patrik Nordwall
@PatrikNordwall so what is the new recommendation? The Akka team has (convincingly) argued against using typed actors, but the solution really can't be to build your entire application without types… can it? - Sarah G
Actor messages are untyped, but that doesn't mean that the entire application is without types. An actor can change behaviour and that influence what types of messages the actor can receive, at runtime. I seldom run into problems due to untyped messages. - Patrik Nordwall
With untyped messages, we're basically back in the realm of dynamic languages — yes, it works, but then again are we not supposed to be leveraging Scala's powerful type system as best we can? If not, why should actor systems be an exception as compared to any other Scala program? - Erik Kaplun

1 Answers

0
votes

As noted in the comments, Akka has dropped support for typed channels and Patrik Nordwall suggested the use of untyped actors. One way to bridge things on the reply side is with the ask pattern, the Future.mapTo method, and pattern matching.

For example, suppose you have Some class hierarchy rooted in Base with subclasses like OakLeaf, ShinyLeaf and Needle. From outside an actor, you can then do things like this:

import akka.pattern.ask

(myActor ? MessageToActor).mapTo[Base] match {
  case oak: OakLeaf => ...
  case shiny: ShinyLeaf => ...
  case needle: Needle => ...
}

I do not have suggestions on ways to solve the request side of what typed channels provided.

Personally, I think that dropping this feature from Akka for the sake of Java compatibility and because it used macros was a mistake. Why make Scala programmers pay a price because Java can't support something?