Is there a Groovy equivalent of the Ruby Timeout module?

In Ruby I would use the Timeout module, where it executes a block and will stop executing the code if it passes the timeout.

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

Does Groovy have something like this?

Answers


There is the TimedInterrupt annotation, but I've not tried it out yet...

Gave it a quick test, and this (poor example):

@groovy.transform.TimedInterrupt( 5L )
def loopy() {
  int i = 0
  try {
    while( true ) {
      i++
    }
  }
  catch( e ) {
    i
  }
}

println loopy()

Runs in the groovy console and prints out i after 5 seconds.

I get:

47314150

Need Your Help

How to block external http requests? (securing AJAX calls)

ajax security http ring

I want to use post to update a database and don't want people doing it manually, i.e., it should only be possible through AJAX in a client. Is there some well known cryptographic trick to use in this

Setting up Laravel on a Mac php artisan migrate error: No such file or directory

php mysql laravel mmap

Pulled a perfectly working laravel project from a git into a mac running MAMP. Project ran perfectly on a linux machine.