cy
04/03/2017, 12:21 PMfun Application.main() {
val heavyPool = ThreadPoolExecutor(1,
Runtime.getRuntime().availableProcessors(),
1L, TimeUnit.MINUTES,
ArrayBlockingQueue(100)
)
val havyDispatcher = heavyPool.toCoroutineDispatcher()
install(Routing) {
get("/") {
call.respondText("Hello, World!")
}
get("/long-running") {
try {
withTimeout(20L, TimeUnit.SECONDS) { // most likely a user is not going to wait so long
future(havyDispatcher) {
// do some long running blocking stuff
}.await()
}
} catch (r: CancellationException) {
call.respond(HttpStatusCode.ServiceUnavailable)
} catch (r: RejectedExecutionException) {
call.respond(HttpStatusCode.ServiceUnavailable)
}
}
}
}