public static void main(String[] args) throws Exception {
logger.info("starting");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory");
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
server.requestHandler(request -> {
logger.info("requestHandler start");
// Here I am simulating an operation waits on something and returns a java Future.
// I'm setting an artificial time to complete of five seconds.
ScheduledFuture<String> future = scheduler.schedule(() -> {
logger.info("Scheduled Event");
return "some return value";
}, 5, TimeUnit.SECONDS);
Single<String> single = Single.fromFuture(future);
logger.info("launched single");
single.subscribe((value) -> {
logger.info("single subscribe");
HttpServerResponse response = request.response();
response.putHeader("content-type", "text/plain");
response.end("Hello World!");
logger.info("sent response");
});
logger.info("exiting requestHandler");
});
server.listen(8080);
logger.info("server listening on 8080");
}1098 [main] INFO verthello.Main - server listening on 8080
4224 [vert.x-eventloop-thread-1] INFO verthello.Main - requestHandler start
4274 [vert.x-eventloop-thread-1] INFO verthello.Main - launched single
6390 [vertx-blocked-thread-checker] WARN i.v.core.impl.BlockedThreadChecker - Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 2171 ms, time limit is 2000
7391 [vertx-blocked-thread-checker] WARN i.v.core.impl.BlockedThreadChecker - Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 3172 ms, time limit is 2000
8393 [vertx-blocked-thread-checker] WARN i.v.core.impl.BlockedThreadChecker - Thread Thread[vert.x-eventloop-thread-1,5,main] has been blocked for 4175 ms, time limit is 2000
9226 [pool-1-thread-1] INFO verthello.Main - Scheduled Event
9226 [vert.x-eventloop-thread-1] INFO verthello.Main - single subscribe
9234 [vert.x-eventloop-thread-1] INFO verthello.Main - sent response
9234 [vert.x-eventloop-thread-1] INFO verthello.Main - exiting requestHandler
--
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/a6fe23bf-bd39-4ef4-8ace-030fbaae9023%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On 9 Jan 2018, at 17:38, Clayton Wohl <clayt...@gmail.com> wrote:I already know I'm doing it the wrong way. I pointed this out in the original post and I had found this out the hard way.
I'm looking for how to do this the right way.I'm reading docs like http://vertx.io/docs/vertx-core/java/ and I don't see how I am supposed to basic things:- What is the best practices way to work with a Java Future?
- How do I turn a Java Future into a Vert.x Future? What is the purpose/benefit of this?
- How would I have known that Single.fromFuture ultimately calls get() and stealthily converts non-blocking APIs into blocking APIs.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/7cdf6ed4-fae1-43cd-b7c9-7ac7a6fd4fb1%40googlegroups.com.
Can you suggest a small code change to the self-contained program that I pasted?