Skip to content

Instantly share code, notes, and snippets.

@augustoerico
Created February 5, 2017 16:09
Show Gist options
  • Save augustoerico/12c795a2453e7a01295f92150ed106c3 to your computer and use it in GitHub Desktop.
Save augustoerico/12c795a2453e7a01295f92150ed106c3 to your computer and use it in GitHub Desktop.
def helloHandler = { Message message ->
def thread = Thread.currentThread()
println "HelloVerticle.helloHandler: $thread.id"
def name = message.body()
def now = new SimpleDateFormat().format(new Date())
def task = new TimerTask() {
@Override
void run() {
println 'Stopping thread'
if (thread && !thread.interrupted) {
thread.interrupt()
message.fail(503, 'Thread interrupted: taking too long')
}
}
}
println 'Starting timer'
Timer timer = new Timer()
timer.schedule(task, 3000) // Here is my timeout! If the process take more than 3s, I don't care for the result anymore
try {
def random = new Random().nextInt(10)
if (random < 2) {
println 'Doing intense calculation'
Thread.sleep(65000)
}
println 'Cancelling timer'
timer.cancel()
timer.purge()
message.reply("Hello, $name at $now".toString())
} catch(Exception e) {
e.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment