Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created May 21, 2014 22:45
Show Gist options
  • Save rightfold/556a73d45bebd04e3370 to your computer and use it in GitHub Desktop.
Save rightfold/556a73d45bebd04e3370 to your computer and use it in GitHub Desktop.
class Geocoder {
val queue = new LinkedBlockingQueue[(String, Promise[Point])]()
val httpClient = HttpClients.createDefault()
var stop = false
new Thread(new Runnable {
override def run() =
while (!stop) {
val (query, promise) = queue.poll()
val response = httpClient.execute(new HttpGet(s"http://maps.bing.com/blablabla?q=$query&blabla=bla"))
try {
println(response)
promise.success(Point(0.0, 0.0))
} finally {
response.close()
}
}
})
def schedule(query: String) = {
val promise = Promise[Point]()
queue.put((query, promise))
promise.future
}
def close() = {
stop = true
httpClient.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment