Last active
March 9, 2017 11:45
-
-
Save ddtxra/cd21ce84a739dc16382da4db9259302c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.Duration; | |
import java.time.LocalDateTime; | |
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.Future; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.http.HttpServer; | |
import io.vertx.ext.web.Router; | |
public class HelloVertX { | |
private static class LongRunningWorker extends AbstractVerticle { | |
@Override | |
public void start(Future<Void> startFuture) { | |
vertx.eventBus().consumer("work", message -> { | |
LocalDateTime start = LocalDateTime.now(); | |
System.err.println("Starting to work on the length of " + message.body().toString()); | |
sleepInSeconds(10); | |
LocalDateTime end = LocalDateTime.now(); | |
message.reply("Finish working. URI length " + message.body().toString().length() + " started at " + start + " ended at " + end + " time elpased was: " + Duration.between(start, end)); | |
}); | |
} | |
} | |
private static class Server extends AbstractVerticle { | |
@Override | |
public void start() throws Exception { | |
HttpServer server = vertx.createHttpServer(); | |
Router router = Router.router(vertx); | |
router.route().handler(routingContext -> { | |
vertx.eventBus().send("work", routingContext.request().absoluteURI(), work -> { | |
if (work.succeeded()) { | |
routingContext.response().end(work.result().body().toString()); | |
} | |
}); | |
}); | |
server.requestHandler(router::accept).listen(8082); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Vertx vertx = Vertx.vertx(); | |
vertx.deployVerticle(new Server()); | |
// Launching 2 workers | |
vertx.deployVerticle(new LongRunningWorker()); | |
vertx.deployVerticle(new LongRunningWorker()); | |
} | |
private static void sleepInSeconds(int seconds) { | |
try { | |
Thread.sleep(seconds * 1000); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment