-
I need to execute 3 select queries and put the 3 results into a JSON object to be sent in the body of an http response. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
@vic0824 you could use the You could use a CountDownLatch counter = new CountDownLatch(3);
Resultset resultset1 = database.async().query("sql", "select from X", new AsyncResultsetCallback() {
public void onComplete() { counter.countDown(); }
});
Resultset resultset2 = database.async().query("sql", "select from Y", new AsyncResultsetCallback() {
public void onComplete() { counter.countDown(); }
});
Resultset resultset3 = database.async().query("sql", "select from Z", new AsyncResultsetCallback() {
public void onComplete() { counter.countDown(); }
});
counter.await();
// OR YOU CAN SPECIFY A TIMEOUT, LIKE 10 SECONDS TOP
// counter.await(10, TimeUnit.SECONDS); |
Beta Was this translation helpful? Give feedback.
-
Great, thanks. |
Beta Was this translation helpful? Give feedback.
-
Hold on, how do you get the query results out of the onNext method? I get the dreaded "must be final or effectively final" error when trying to assign the result to a variable defined outside the scope of the anonymous inner class. |
Beta Was this translation helpful? Give feedback.
-
Excellent, thanks. |
Beta Was this translation helpful? Give feedback.
@vic0824 you could use the
waitCompletion()
, but it'd wait for any other pending requests, so if you're other things running it's not the ideal.You could use a
CountDownLatch
to do that. This is an example (not tested. checkout for misspelling):