public Object findTemplateSource(final String fullName) throws IOException {
...
Future future = Future.future();
// Basically, perform some async database queries
jdbcClient.getConnection(h -> {
...
future.complete(someResult);
});
try {
// Wait for async code to complete
future.wait();
return allReadyFuture.result();
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/b75d532e-05f4-4f77-90b0-cfd094c7c38b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public class MyVerticle extends AbstractVerticle {
@Override
public void start() {
vertx.eventBus().consumer("test", this::testTimer);
}
public void testTimer(Message<JsonObject> msg) {
CompletableFuture future = new CompletableFuture();
vertx.setTimer(1000, t -> {
future.complete(true);
});
try {
future.get();
msg.reply(new JsonObject());
} catch (Exception e) {
e.printStackTrace();
msg.fail(1, e.getMessage());
}
}
}
@RunWith(VertxUnitRunner.class)
public class WtfTest {
@Test
public void testTest(TestContext context) throws Exception {
Vertx vertx = Vertx.vertx();
Async async = context.async();
vertx.deployVerticle("MyVerticle", d -> {
vertx.eventBus().send("test", new JsonObject(), h -> {
async.complete();
});
});
}
}
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/e7bc7b70-0f3c-4f05-b363-58e9d74cc51e%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/879cf8e8-e818-4e51-9b85-8a007ccf0cde%40googlegroups.com.
CompletableFuture future = new CompletableFuture();
WorkerExecutor executor = vertx.createSharedWorkerExecutor("test-executor-pool");
executor.executeBlocking(h -> { // Worker thread
vertx.setTimer(1000, t -> { // test-executor-pool-thread
h.complete(true);
});
}, res -> {
future.complete(res.result());
});
try {
future.get();
msg.reply(new JsonObject());
} catch (Exception e) {
e.printStackTrace();
msg.fail(1, e.getMessage());
}
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/8f376c32-49ed-4354-8750-99983846c2ed%40googlegroups.com.
CompletableFuture future = new CompletableFuture();
new Thread(() -> // Runs on worker tread
vertx.setTimer(2000, h -> { // Runs on newly created thread
future.complete(new JsonObject()); // Runs on eventloop thread
})
).start();
try {
Object o = future.get(); // Blocks worker thread
msg.reply(o);
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/63e78d39-5a5b-4fd0-8130-31b6d9bfeaf2%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/ob4VrXTrqlU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/69A7CFCB-6921-4F20-B7BE-EE24E3153314%40gmail.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/AEC4A4BF-1CF8-4A6A-BFD3-72AF2DD78854%40gmail.com.
Unfortunately, switching to 3.4.1 seemed to have broken the code:
WorkerExecutor executor = vertx.createSharedWorkerExecutor("sqltemplateloader-pool");
...
/**
* Override Freemarkers findTemplateSource method.
* This is a synchroneous method. We must return something when the method exits
* but we need to perform asynchroneous database queries.
*/
public Object findTemplateSource(final String fullName) throws IOException {
...
// Execute the query on a separate thread because this tread will be blocked by the future.get() below
CompletableFuture<JsonObject> completableFuture = new CompletableFuture();
logger.info("1: "+Thread.currentThread().getName());
executor.executeBlocking(h -> {
logger.info("2: "+Thread.currentThread().getName()); <!-- THIS POINT IS NEVER REACHED IN 3.4.1
// Perform some time async sql query
h.complete(null);
},
result -> {
if (result.failed()) {
completableFuture.completeExceptionally(result.cause());
} else {
completableFuture.complete((JsonObject) result.result());
}
});
try {
logger.info("3: "+Thread.currentThread().getName());
JsonObject src = completableFuture.get(60, TimeUnit.SECONDS);
...
return src;
} catch (Exception e) {
...
return null;
}
}
When I use the code with vertx 3.4.0.Beta, the method completes and the log shows1: vert.x-worker-thread-14: vert.x-worker-thread-12: sqltemplateloader-pool-0When I use the code with vertx 3.4.0 or 3.4.1, the method does not complete and the log shows1: vert.x-worker-thread-14: vert.x-worker-thread-1In this case the code that should run the executeBlocking block is apparently already blocked by the future.get call.
If there's a solution out there, I'd really like to know :) For now, I see no other options than using a non-vertx, synchroneous connection and perform synchroneous database queries...