9
votes

How to implement simple actors without Akka? I don't need high-performance for many (non-fixed count) actor instances, green-threads, IoC (lifecycle, Props-based factories, ActorRef's), supervising, backpressure etc. Need only sequentiality (queue) + handler + state + message passing.

As a side-effect I actually need small actor-based pipeline (with recursive links) + some parallell actors to optimize the DSP algorithm calculation. It will be inside library without transitive dependencies, so I don't want (and can't as it's a jar-plugin) to push user to create and pass akkaSystem, the library should have as simple and lightweight interface as possible. I don't need IoC as it's just a library (set of functions), not a framework - so it has more algorithmic complexity than structural. However, I see actors as a good instrument for describing protocols and I actually can decompose the algorithm to small amount of asynchronously interacting entities, so it fits to my needs.

Why not Akka

Akka is heavy, which means that:

  • it's an external dependency;
  • has complex interface and implementation;
  • non-transparent for library's user, for example - all instances are managed by akka's IoC, so there is no guarantee that one logical actor is always maintained by same instance, restart will create a new one;
  • requires additional support for migration which is comparable with scala's migration support itself.
  • It also might be harder to debug akka's green threads using jstack/jconsole/jvisualvm, as one actor may act on any thread.

Sure, Akka's jar (1.9Mb) and memory consumption (2.5 million actors per GB) aren't heavy at all, so you can run it even on Android. But it's also known that you should use specialized tools to watch and analyze actors (like Typesafe Activator/Console), which user may not be familiar with (and I wouldn't push them to learn it). It's all fine for enterprise project as it almost always has IoC, some set of specialized tools and continuous migration, but this isn't good approach for a simple library.

P.S. About dependencies. I don't have them and I don't want to add any (I'm even avoiding the scalaz, which actually fits here a little bit), as it will lead to heavy maintenance - I'll have to keep my simple library up-to-date with Akka.

2
What is the reason you don't want to use Akka?ZhekaKozlov
Akka is not heavyZhekaKozlov
OK. Have a look at Actor implementation from "FP in Scala" book: github.com/fpinscala/fpinscala/blob/master/answers/src/main/…ZhekaKozlov
Not sure why you are calling Akka an IoC container. It's not. The fact that it wraps Actors in ActorRefs is just an implementation detail which simplifies lifecycle management and network transparency, but it does not "invert the control" even a bit.Vladimir Matveev
@Vladimir Matveev O_o? IoC (in general sense not just IoD or DI) - is actually calling client-code from library-code. 1) Akka maintains Actor's lifecycle by reinstantiating them, 2) system.actorOf - is a factory which manages their creating (Service Locator), 3) even calling receive from dispatcher is a kind of IoC. 4) ActorRef is invented primarily because you can't inject actors with circular dependencies, so it's a part of IoD (secondary because of network abstraction)dk14

2 Answers

10
votes

Here is most minimal and efficient actor in the JVM world with API based on Minimalist Scala actor from Viktor Klang: https://github.com/plokhotnyuk/actors/blob/41eea0277530f86e4f9557b451c7e34345557ce3/src/test/scala/com/github/gist/viktorklang/Actor.scala

It is handy and safe in usage but isn't type safe in message receiving and cannot send messages between processes or hosts.

Main features:

Example of stateful counter:

  def process(self: Address, msg: Any, state: Int): Effect = if (state > 0) { 
     println(msg + " " + state)
     self ! msg
     Become { msg => 
        process(self, msg, state - 1)
     }
  } else Die

  val actor = Actor(self => msg => process(self, msg, 5))

Results:

scala> actor ! "a"
a 5

scala> a 4
a 3
a 2
a 1
4
votes

This will use FixedThreadPool (and so its internal task queue):

import scala.concurrent._

trait Actor[T] {
  implicit val context = ExecutionContext.fromExecutor(java.util.concurrent.Executors.newFixedThreadPool(1))
  def receive: T => Unit
  def !(m: T) = Future { receive(m) }
}

FixedThreadPool with size 1 guarantees sequentiality here. Of course it's NOT the best way to manage your threads if you need 100500 dynamically created actors, but it's fine if you need some fixed amount of actors per application to implement your protocol.

Usage:

class Ping(pong: => Actor[Int])  extends Actor[Int] {     
      def receive = {
          case m: Int => 
             println(m)
             if (m > 0) pong ! (m - 1)
      }    
}

object System { 
      lazy val ping: Actor[Int] = new Ping(pong) //be careful with lazy vals mutual links between different systems (objects); that's why people prefer ActorRef
      lazy val pong: Actor[Int] = new Ping(ping)
}

System.ping ! 5

Results:

import scala.concurrent._
defined trait Actor
defined class Ping
defined object System
res17: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@6be61f2c
5
4
3
2
1
0

scala> System.ping ! 5; System.ping ! 7
5
7
4
6
3
5
2
res19: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@54b053b1
4
1
3
0
2
1
0

This implementation is using two Java threads, so it's "twice" faster than counting without parallelization.