14.3.1 ブロック操作
PgxFuture
でget()
メソッドをコールすることにより、結果を簡単に取得できます。get()
は、結果が使用可能になるまでコール元のスレッドをブロックします。
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は、すべてのAsyncメソッド(get()
メソッドをコールする)に、ブロックのためのコンビニエンス・メソッドを提供します。通常、これらのメソッドはそれらがラップする非同期メソッドと同じ名前になりますが、接尾辞Asyncは付きません。たとえば、前述のコード・スニペットは次と同等です。
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
}
親トピック: グラフ・サーバー(PGX)での非同期リクエストの処理