views:

120

answers:

0

Hi there! I'm looking for some way to (busy) wait for an Actor (or Reactor) to be terminated, i.e. make sure that all messages sent before some Stop() message are consumed. What I came up so far with is setting up a CountDownLatch on which I can wait for the Stop() message to be processed.

This approach seems to work, although I'm not sure if this is the 'right' way to do it. Any thoughts on this?

private case class Stop() extends Message

trait Layer extends Reactor[Message] {
  private val cdl = new CountDownLatch(1)
  final def stop() { this ! Stop(); cdl.await() }
  final def stop(millis: Long) { this ! Stop(); cdl.await(millis, TimeUnit.MILLISECONDS) }

  final def act() = loop {
    react {
      case Stop() => cdl.countDown; exit()
      case message: Message => consume(message)
    }
  }

  def consume(message: Message)
}