Created
March 28, 2013 18:10
-
-
Save siman/5265515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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