Created
March 28, 2013 17:49
-
-
Save siman/5265340 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
trait IgnoreIfBusyCmd | |
abstract class IgnoreIfBusyTyped[C <: IgnoreIfBusyCmd](implicit m: Manifest[C]) extends Actor with ActorLogging{ | |
import context.{become, unbecome} | |
import context.system | |
class Completed { | |
override def toString = "Completed" | |
} | |
private val rightClass = m.erasure | |
protected def receive: Receive = { | |
case cmd: IgnoreIfBusyCmd 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 <: IgnoreIfBusyCmd](cmd: T) = rightClass == cmd.getClass | |
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