Skip to content

Instantly share code, notes, and snippets.

@derekwyatt
Created May 3, 2011 16:00
Show Gist options
  • Save derekwyatt/953612 to your computer and use it in GitHub Desktop.
Save derekwyatt/953612 to your computer and use it in GitHub Desktop.
A simple Request/Response one-shot Akka Actor example
object ReqRspActor {
object ReqRspProtocol {
case class UnknownMessage(msg: String) extends Exception(msg)
case class FutureException(exception: Throwable)
}
object ResendOrNot extends Enumeration {
type ResendOrNot = Value
val Resend, DoNotResend = Value
}
def spawn(owner: ActorRef)(logic: => ReqRspActor): Unit = {
if (owner.faultHandler == NoFaultHandlingStrategy)
owner.faultHandler = OneForOneStrategy(List(classOf[Throwable]), 3, 1000)
val a = actorOf(logic)
owner.startLink(a)
a ! 'Go
}
}
abstract class ReqRspActor(recipient: ActorRef, message: Any) extends Actor {
import ReqRspActor._
import ReqRspActor.ResendOrNot._
self.lifeCycle = Permanent
def myref = self
def receiveResponse: Receive
private def sendRequest(to: ActorRef, msg: Any): Unit = {
(to !!! msg) onComplete { (future) =>
future.exception match {
case Some(exception) =>
if (handleProblem(ReqRspProtocol.FutureException(exception)) == DoNotResend)
stop
else
sendRequest(recipient, message)
case _ =>
receiveResponse(future.result.get)
stop
}
}
}
def receive: Receive = {
case 'Go =>
sendRequest(recipient, message)
become(unknownHandler)
}
protected def unknownHandler: Receive = {
case msg =>
handleProblem(ReqRspProtocol.UnknownMessage("Unrecognized message was received: (" + msg + ")"))
stop
}
protected def stop {
self ! PoisonPill
}
def handleProblem(message: Any): ResendOrNot = {
DoNotResend
}
}
ReqRspActor.spawn(self)(new ReqRspActor(someServer, DoSomethingForMe) {
import ReqRspActor._
def receiveResponse: Receive = {
case HereYouGo(data) =>
println("Got my data: " + data)
}
override def handleProblem(message: Any): ResendOrNot.ResendOrNot = {
match message {
case _ =>
ResendOrNot.Resend
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment