title: Check if actor system has finished its work
body: <p>I have written an Akka-based automaton which can accept roman numbers from 1 to 20. The automaton works fine, but after handling the complete input I want to shutdown the system.</p>
  
  <p>Because I do not know when the automaton finishes I do not know when to send the stop signal. This results in the problem, that the system is halted too early and the automaton can not finish its tasks. What is the best way to check if there are tasks in the actor system which are not yet completed?</p>
  
  <p>The automaton:</p>
  
  <pre><code>import akka.actor.{Props, ActorSystem, ActorRef, Actor}
  
  object RomanNumberAcceptor extends App {
  
    val allLegalNumbers = List("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
      "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"
    )
  
    val someWrongNumbers = List("IXX", "VV", "VX", "IIII", "IVV", "XXX", "XXI", "XXV", "IIX", "IIIV")
  
    for (number &lt;- allLegalNumbers)
      State testNumber number
  
    for (number &lt;- someWrongNumbers)
      State testNumber number
  
    // this stop signal is too early
    State.stop()
  }
  
  case class WordData(fullWord: String, remainder: String)
  
  object State {
    val ErrorStateId: Int = -1
  
    private val data = Map(
      0 -&gt; (Map('I' -&gt; 1, 'V' -&gt; 5, 'X' -&gt; 10), false),
      1 -&gt; (Map('I' -&gt; 2, 'V' -&gt; 4, 'X' -&gt; 9), true),
      2 -&gt; (Map('I' -&gt; 3, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      3 -&gt; (Map('I' -&gt; ErrorStateId, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      4 -&gt; (Map('I' -&gt; ErrorStateId, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      5 -&gt; (Map('I' -&gt; 6, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      6 -&gt; (Map('I' -&gt; 2, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      9 -&gt; (Map('I' -&gt; ErrorStateId, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      10 -&gt; (Map('I' -&gt; 1, 'V' -&gt; 5, 'X' -&gt; 20), true),
      20 -&gt; (Map('I' -&gt; ErrorStateId, 'V' -&gt; ErrorStateId, 'X' -&gt; ErrorStateId), true),
      ErrorStateId -&gt; (Map.empty[Char, Int], false)
    )
  
    val system = ActorSystem("RomanNumberAcceptor")
  
    val states: Map[Int, ActorRef] =
      for ((id, (map, accepted)) &lt;- State.data)
        yield id -&gt; system.actorOf(Props(State(id, map, accepted)), name = "s"+id)
  
    def testNumber(s: String) {
      states(0) ! WordData(s, s)
    }
  
    def stop() {
      for (a &lt;- states.values)
        system stop a
      system.shutdown()
    }
  }
  
  case class State(id: Int, transMap: Map[Char, Int], accepted: Boolean) extends Actor {
    def receive = {
      case WordData(fullWord, _) if `id` == State.ErrorStateId =&gt;
        println("Error: "+fullWord)
      case WordData(fullWord, remainder) if remainder.isEmpty =&gt;
        println((if (accepted) "Success: " else "Error: ") + fullWord)
      case WordData(fullword, remainder) =&gt;
        // maybe some heavy operation here
        (0 to 1e4.toInt).sum
  
        val nextAktor = transMap(remainder.head)
        State.states(nextAktor) ! WordData(fullword, remainder.tail)
    }
  }
  </code></pre>