Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created November 9, 2013 15:00
Show Gist options
  • Save shuhei/7386322 to your computer and use it in GitHub Desktop.
Save shuhei/7386322 to your computer and use it in GitHub Desktop.
Akka with skinny-framework. Combined skinny-framework's boilerplate and Scalatra's akka example. http://scalatra.org/2.2/guides/async/akka.html
package controller
import akka.actor.ActorSystem
import scala.concurrent.{Promise, ExecutionContext}
import org.scalatra.{AsyncResult, FutureSupport}
import dispatch._
import skinny.controller.SkinnyServlet
object DispatchAkka {
def retrievePage()(implicit cxt: ExecutionContext): Future[String] = {
val prom = Promise[String]()
dispatch.Http(url("http://slashdot.org/") OK as.String) onComplete {
case r => prom.complete(r)
}
prom.future
}
}
class RootController extends SkinnyServlet with FutureSupport {
protected implicit def executor: ExecutionContext = {
val system: ActorSystem = this.servletContext.getAttribute("ActorSystem").asInstanceOf[ActorSystem]
system.dispatcher
}
def index = {
contentType = "text/html"
new AsyncResult { val is =
DispatchAkka.retrievePage()
}
}
}
import _root_.akka.actor.{ ActorSystem, Props }
import skinny._
import skinny.controller._
import _root_.controller._
class ScalatraBootstrap extends SkinnyLifeCycle {
val system = ActorSystem()
override def initSkinnyApp(ctx: ServletContext) {
ctx.setAttribute("ActorSystem", system)
Controllers.root.mount(ctx)
AssetsController.mount(ctx)
}
override def destroy(ctx: ServletContext) {
system.shutdown()
super.destroy(ctx)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment