Skip to content

Instantly share code, notes, and snippets.

@siman
Created March 28, 2013 18:10
Show Gist options
  • Save siman/5265515 to your computer and use it in GitHub Desktop.
Save siman/5265515 to your computer and use it in GitHub Desktop.
abstract class IgnoreIfBusyTyped[C](implicit m: Manifest[C])
extends Actor with ActorLogging {
import context.{become, unbecome}
import context.system
class Completed {
override def toString = "Completed"
}
protected def receive: Receive = {
case cmd if isCmdOfRightType(cmd) =>
val completed = new Completed
become {
case `completed` => unbecome()
}
future(run(cmd.asInstanceOf[C]), completed)
case x => log.warning("Unknown command received: " + x)
}
private def isCmdOfRightType[T](cmd: T) =
Manifest.classType(cmd.getClass) <:< m
private def future[T](f: => T, completed: Completed) {
Future(f).onComplete(_ => self ! completed)
}
protected def run(cmd: C)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment