14.3.1 Blocking Operation

You can easily get the result by calling the get() method on the PgxFuture. The get() blocks the caller thread until the result is available:

PgxFuture<PgxSession> sessionPromise = instance.createSessionAsync("my-session");
try {
    // block caller thread
    PgxSession session = sessionPromise.get();
    // do something with session
    ...
} catch (InterruptedException e) {
    // caller thread was interrupted while waiting for result
} catch (ExecutionException e) {
    // an exception was thrown during asynchronous computation
    Throwable cause = e.getCause(); // the actual exception is nested
}

PGX provides blocking convenience methods for every Async method, which calls the get() method. Typically, those methods have the same name as the asynchronous method they wrap, but without the Async suffix. For example, the preceding code snippet is equal to:

try {
    // block caller thread
    PgxSession session = instance.createSession("my-session");
    // do something with session
    ...
} catch (InterruptedException e) {
    // caller thread was interrupted while waiting for result
} catch (ExecutionException e) {
    // an exception was thrown during asynchronous computation
    Throwable cause = e.getCause(); // the actual exception is nested
}