Skip to content

Instantly share code, notes, and snippets.

@ctoestreich
Created June 20, 2016 20:42
Show Gist options
  • Save ctoestreich/0921b0ade5aef0b61a41bb8d4ae32b25 to your computer and use it in GitHub Desktop.
Save ctoestreich/0921b0ade5aef0b61a41bb8d4ae32b25 to your computer and use it in GitHub Desktop.
Promise utility for ensuring timeouts happen in grails 3
import grails.async.Promise
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
@Slf4j
@CompileStatic
class PromiseUtil {
static <T> T promiseGet(Promise<T> promise, String promiseName = '', int timeoutInSeconds = 10) {
T result = null
try{
result = promise.get(timeoutInSeconds, TimeUnit.SECONDS) as T
} catch (InterruptedException ie) {
log.error "Promise - current thread was interrupted while waiting for result: " + promiseName
} catch (ExecutionException ee) {
log.error "Promise - current thread threw an exception during computation: " + promiseName
} catch (TimeoutException te) {
log.error "Promise - current thread timed out while waiting for result: " + promiseName
}
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment