Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Minimalist example of failure
/*
Minimalist example of failure. Uses:
scala: 2.10.0
scalatra: 2.2.0
*/
import akka.actor._
import org.scalatra.{ScalatraServlet, AsyncResult, FutureSupport}
import org.scalatra.{Accepted, BadRequest, Forbidden, Ok, Unauthorized}
class Sleeper extends Actor {
var waiting = Set.empty[ActorRef]
override def preStart() = {
import scala.concurrent.duration._
context.setReceiveTimeout(5.seconds)
}
def receive = {
case 'wakeup =>
waiting += sender
case ReceiveTimeout =>
waiting foreach { actor =>
actor ! 'awake
}
waiting = waiting.empty
}
}
class MyActorApp extends ScalatraServlet with FutureSupport {
import scala.concurrent.ExecutionContext
import _root_.akka.pattern.ask
implicit val timeout = akka.util.Timeout(60)
implicit val system = ActorSystem()
protected implicit def executor: ExecutionContext = system.dispatcher
val sleeper = system.actorOf(Props[Sleeper])
get("/async") {
new AsyncResult { def is = sleeper ? 'wakeup }
}
get("/fire-forget") {
sleeper ! "Hey, you know what?"
Accepted()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.