Skip to content

Instantly share code, notes, and snippets.

@agaoglu
Created Jan 10, 2012
Embed
What would you like to do?
HttpClient concurrency
object Run extends App {
LoggerFactory.getLogger(getClass) // initialize SLF4J early
val sprayClient = actorOf(new HttpClient())
Supervisor(
SupervisorConfig(
OneForOneStrategy(List(classOf[Exception]), 3, 100),
List(
Supervise(sprayClient, Permanent)
)
)
)
val urls = Seq(
"http://google.com/",
"http://cnn.com/",
"http://facebook.com/",
"http://bbc.co.uk/",
"http://twitter.com/",
"http://spray.github.com/spray/api/spray-client/index.html",
"http://spray.github.com/spray/api/",
"http://www.google.com/",
"http://twitter.com/",
"http://gapingvoid.com/",
"http://spray.github.com/spray/api/spray-base/index.html",
"http://spray.cc/",
"http://www.facebook.com/",
"http://techcrunch.com/",
"http://www.engadget.com/",
"http://arstechnica.com/",
"http://edition.cnn.com/",
"http://www.reuters.com/",
"http://www.infoq.com/presentations/Why-CouchDB",
"http://www.infoq.com/presentations/Enterprise-Integration",
"http://www.infoq.com/presentations/Running-Spring-Java-and-Scala-Apps-on-Heroku",
"http://www.reuters.com/article/2012/01/07/us-usa-fed-idUSTRE8051BX20120107",
"http://blogs.reuters.com/jackshafer/2012/01/05/what-good-are-endorsements/",
"http://www.reuters.com/article/2012/01/07/us-hedgefund-paulson-idUSTRE80601L20120107",
"http://edition.cnn.com/2012/01/07/world/europe/uk-breast-implant/index.html",
"http://edition.cnn.com/2012/01/04/opinion/lemmon-jolie-movie-women-war/index.html",
"http://arstechnica.com/business/news/2012/01/new-slow-motion-dos-attack-just-a-few-pcs-little-fear-of-detection.ars",
"http://arstechnica.com/tech-policy/news/2012/01/french-court-frowns-on-google-autocomplete-issues-65000-fine.ars",
"http://techcrunch.com/2012/01/06/timehop-takes-you-a-year-back-in-time-through-online-content/",
"http://techcrunch.com/2012/01/06/olpc-xo-3-tablet-to-be-shown-at-ces/",
"http://www.scala-lang.org/api/current/index.html"
)
val sr = actorOf[StatusReporter].start()
urls.foreach( sr ! _ )
}
import akka.actor.Actor
import akka.actor.PoisonPill
import akka.actor.Actor.actorOf
import cc.spray.http._
import cc.spray.can.{HttpClient, HttpConnection, Connect}
import HttpMethods._
import cc.spray.SprayCanConversions._
class StatusReporter extends Actor {
val httpClient = Actor.registry.actorsFor("spray-can-client").head
def receive = {
case urlString:String =>
val url = new java.net.URL(urlString)
val host = url.getHost()
val port = if (url.getPort() > 0) url.getPort() else 80
val connection = (httpClient ? Connect(host, port)).mapTo[HttpConnection]
connection onComplete { conn =>
val request = toSprayCanRequest(HttpRequest(GET, uri = url.getPath()))
val response = conn.value.get.right.get.send(request)
response onComplete { resp =>
println(resp.value.get.right.get.status + " " + urlString)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment