sqldump

(coffee) => code

ExecutionContext for Long Running Tasks

Rule #1 of Akka: don’t block inside actors. If you do have blocking / high latency calls, wrap them in a future and toss them into a different execution context specifically meant for high latency tasks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.concurrent.Executors

class ClassyActor extends Actor {
  val numThreads = 10
  val pool = Executors.newFixedThreadPool(numThreads)
  val ctx = ExecutionContext.fromExecutorService(pool)

  def receive: Actor.Receive = {
    // An message that needs some high latency work done
    case m: Message =>
      val future = Future {
        // do something with m: Message
      } (ctx)

      future.onComplete({
        case Success(s) =>
          // do something when the task successfully completed
        case Failure(f) =>
          // do something when the task failed
      }) (ctx)
  }
}