- Create a Vertx instance
Vertx vertx = Vertx.vertx();
- Add configuration
Vertx vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(40));
- Don't call us, we'll call you.
Most of Vert.x's APIs are event-driven. Vert.x calls your handler by sending events .vertx.setPeriodic(1000, id -> { //1. This handler will get called every second //2. Called by a event loop thread System.out.println("timer fired!"); });
When an event from Vert.x is delivered to your handler, Vert.x will be called asynchronously, so the business logic in the handler cannot be a blocking operation.
-
The
difference between Multi-Reactor and Node.js is that the number of threads of eventloop is configurable. This mode is called Multi-Reactor. Corresponding to the different event loops, there are three modes:- Standard Verticle: The most common type, always executed in the event loop.
- Worker Verticle: They use the worker pool thread pool to run. A verticle instance will never execute concurrently in two or more threads.
- Multi-threaded worker verticle: They use the worker pool thread pool to run. A verticle instance can be executed concurrently in multiple threads.
-
Event loop In
most cases, Vert.x calls your handler through a thread called event loop. -
Reactor pattern
without blocking, event loop can continuously and quickly deliver events to different handlers. One event loop thread can process a large number of events in a short time. For example, one event loop can handle thousands of HTTP request. This is the legendary Reactor mode -
Perform blocking operation
vertx.executeBlocking(promise -> { // Call some blocking API that takes a significant amount of time to return String result = someAPI.blockingMethod("hello"); promise.complete(result); }, res -> { System.out.println("The result is: " + res.result()); });
-
The Context object
AN vert.x Event to the Provides the when A Handler or Calls The Start of Methods A or STOP Verticle,
The Execution IS A Associated with the Context. A context Usually IS AN context Event Loop-A and IS tied to specific Event Thread Loop.
By tying Set thread method to ensure thread safety -
EventBus
inherits Verticle and is executed in start()public static void main(String[] args) { Consumer<Vertx> runner = vertx -> { try { vertx.deployVerticle(Receiver.class.getName()); } catch (Throwable t) { t.printStackTrace(); } }; Vertx.clusteredVertx(new VertxOptions(), res -> { if (res.succeeded()) { Vertx vertx = res.result(); runner.accept(vertx); } else { res.cause().printStackTrace(); } }); }
-
Router
public class RouteTest { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); Route route = router.route(HttpMethod.POST, "/name/age/:name/:age/"); route.handler(routingContext -> { String name = routingContext.request().getParam("name"); String age = routingContext.request().getParam("age"); routingContext.response().end(name+":"+age); }); server.requestHandler(router).listen(8080); }
}
```