Skip to content

Instantly share code, notes, and snippets.

@chirino
Created May 29, 2013 16:51
Show Gist options
  • Select an option

  • Save chirino/5671833 to your computer and use it in GitHub Desktop.

Select an option

Save chirino/5671833 to your computer and use it in GitHub Desktop.
package example
import org.vertx.java.core._
import org.vertx.java.core.buffer._
import org.vertx.java.core.http._
import scala.concurrent._
import scala.concurrent.duration._
import scala.async.Async.{async, await}
import scala.util.{Try,Success,Failure}
import scala.concurrent.duration._
object Example {
val vertx = VertxFactory.newVertx()
def request(host:String, path:String) = {
val promise = Promise[Buffer]()
println("Requesting: "+host)
vertx.createHttpClient().setPort(80).setHost(host).getNow(path, new Handler[HttpClientResponse]() {
def handle(response:HttpClientResponse) = {
response.bodyHandler(new Handler[Buffer]() {
def handle(data:Buffer) {
promise complete Success(data)
}
})
}
})
promise.future
}
def main(args:Array[String]):Unit = {
import ExecutionContext.Implicits.global
val result = async {
val google = await(request("google.com", "/"))
println("Google came in")
val yahoo = await(request("yahoo.com", "/"))
println("Yahoo came in")
"===================\n"+google +"\n===================\n"+ yahoo
}
println(Await.result(result, 5 seconds))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment