Vert.x in detail, full asynchronous framework

Source: Internet
Author: User
Tags xapi eventbus

Vert.x is a JVM, lightweight, high-performance application platform, very suitable for the latest mobile end of the background, the Internet, enterprise Application architecture. Vert.x is based on full asynchronous Java server Netty and extends a number of useful features


GitHub: https://github.com/vert-x3

official website : http://vertx.io/

Introduction:

Vert.x was born in 2011, then called Node.x, but later for some reason renamed bit vert.x. After more than three years of development, now to 3.3.3, the community is also more and more active, in the latest official website Vertx.io, the author described it in a word, the reative Development Kit on the JVM. Vert.x is now seen as the most powerful, Third-party library relies on the least Java framework, it relies only on Netty4 and Jacskon, and if you need to build distributed vert.x then rely on Hazelcast This distributed framework, note that VERT.X3 must be based on JAVA8. Because it is based on the JVM, Vert.x can implement your business in other languages.

Vert.x is an asynchronous, non-blocking network framework with a reference to Node.js. Basically node.js capable things, vert.x are capable. Vert.x uses Netty4 's eventloop to do a single-threaded event loop, so the business running on Vert.x cannot do CPU-intensive operations, which can cause the entire thread to block.


Features:

1. Supports a variety of programming languages at the same time-Java, Scala, JavaScript, Ruby, Python, Groovy, Clojure, Ceylon, etc. are currently supported. For programmers, the direct benefit is that you can use a variety of language-rich Lib, but also no longer for programming language selection and entanglements;

2, asynchronous lock-free programming-the classic multithreaded programming model can meet a lot of web development scenarios, but with the increase in the number of mobile Internet concurrent connections, multithreaded concurrency control model performance is difficult to expand, and to control the concurrent lock requires a higher skill, the current reactor asynchronous programming model began racing enclosure, And Vert.x is one of the first choice of this kind of asynchronous lock-free programming;

3, the rich support for various IO-currently vert.x asynchronous model has supported TCP, UDP, filesystem, DNS, Eventbus, sockjs and so on;

Excellent distributed development support--vert.x through the Eventbus event bus, it is easy to write distributed decoupling program, which has good expansibility.

4, the ecological system is maturing--vert.x under the Eclipse Foundation, asynchronous drives have supported common components such as Postgres, MySQL, MongoDB, Redis, and have several vert.x application cases in the production environment.


Vert.x is event-based, providing an event-driven programming model that allows programmers to write event handler handler only when using Vert.x as a server. When the TCP socket has data, event handler understands that the call is created, and it can also be activated in the following situations: ' When the event bus is accepted to the message, ' when an HTTP message is received, ' When a connection is disconnected ', ' when the timer is out of time. '

As a layer of service layer processing logic, the level of domain model processing in traditional Java is basically corresponding. Various service invocations, and calls to the data tier. It's almost a link. In traditional models, this layer is basically synchronous, even if there is an asynchronous call, It is also asynchronous, separated from the business logic. If full asynchrony can cause business logic to break. The code is difficult to describe. You'll find that vert.x is not really good enough to integrate into the service layer with strong business. The main reasons are as follows
1, the asynchronous system itself, not suitable for the description of the business logic of the order
2. Because of the asynchronous problem, the Access data layer must also be asynchronous, resulting in further fragmentation of the business model.


Vert.x's executive unit is called Verticle. The entry of the program, each language may be implemented in a different way, such as Java needs to inherit a abstractverticle abstract class

There are two types of verticle, one based on eventloop for I/o-intensive, and one for CPU-intensive worker verticle. and verticle communicate with each other only through the Eventbus, can support point to point communication, but also can support publish & Subscribe communication mode.

Important interface
Org.vertx.java.core.Handler

Vert.x runtime core interface for result callback processing, most of your logical code will be executed here. So there's no need to explain.

Org.vertx.java.core.Context
The meaning of this noun is a running program based on VERT.XAPI. Here we simply understand scripts that inherit from Org.vertx.java.platform.Verticle Java classes or other languages

That context interface represents the context of an executable unit, where one thing to do is to deal with the contents of Handler Void Runoncontext (handler<void> action);

There are two kinds of contexts in vert.x, namely EventLoop and worker, and worker will be executed in order of worker and multithreaded worker. Here we look at two types of eventloop and worker, what is eventloop.
In Vert.x, all events including IO are dependent on the Netty EventLoop interface, and this interface is called at a certain frequency in netty. That is, when IO events occur, Netty allocates CPU resources to respond to this event in time.
In Vert.x you can simply understand that IO-related events are OK, use a specific line pool to respond to such requests. And the worker in the Vert.x default is a set of sequential handler, that is, the first-come-first-order execution, the request of this class is another thread pool execution .


All business logic actually runs on the EventLoop in Netty, and EventLoop through the Loop event queue to execute all the business logic, so that some I/O operations frequent events can be separated from the CPU in time, Finally, register a callback handler to handle all event callbacks


Org.vertx.java.core.Vertx
This is actually an API, a Vert.x extender, and everything you can use is here. Based on the above three interfaces, can actually abstract an asynchronous model, through the Vertx interface to invoke a API,API inside will hold a context, After the non-business logic of the API itself is executed, the handler is passed into the context execution. This is probably the entire vert.x internal execution process, three interfaces to abstract a world this is the philosophy of software design.

Important Concepts

verticle

The code package based on the Vert.x framework is a verticle, simply put, a code that can be executed by the Vert.x framework calls the VERT.XAPI code is a verticle. He can use Scala Clojure JS Language implementations such as Ruby. Multiple verticle instances can be executed in parallel. A vert.x based service may require multiple verticles to implement, And to be deployed on multiple servers. They communicate with each other through Vert.x events. You can start between vert.x commands, or you can wrap verticle into vert.x modules.

Module

Vert.x applications are implemented by one or more modules. A module is implemented by multiple verticles. You can imagine the module as a Java package, which may be the implementation of a particular business, or a common service implementation (those services that can be reused). Vert.x prepared module, which can be posted to Maven's warehouse. In a binary format in zip. Or publish to the Vert.x module registry. In fact, the development of this modular approach supports the entire Vert.x ecosystem. Module more information, I need to open a separate series to explain.

EventBus:

It is the core of the vert.x, in the cluster communication between the containers, each verticle communication between the event bus to achieve, later will launch a special article on this, please wait.

Shared Data:

It is a simple shared map and set provided by Vert.x to resolve data sharing between verticle

Frame Chart


Example:

public class Helloworldembedded {public
	static void Main (string[] args) {
	    //Create a HTTP server which simply R Eturns "Hello world!" to each request.
	    Vertx.vertx (). Createhttpserver (). RequestHandler (req-> req.response (). End ("Hello world!"). Listen (8080);
	  }


Start a verticle

Vertx.deployverticle (DingtalksrvVertical.class.getName (), deploymentoptions, result-> {
		      if ( Result.succeeded ()) {
		        String Deployid = Result.result ();
		        SYSTEM.OUT.PRINTLN ("Dingtalksrv deployment succeeded [" + Deployid + "]");
		        Future.complete ();
		      } else {
		        System.out.println ("Dingtalksrv deployment failed  " + result.cause (). GetMessage ());
		        Future.fail (Result.cause ());
		      }
		    );

Start a service

Httpserveroptions options = CreateOptions ();
    Server = Vertx.createhttpserver (options);
    Server.requesthandler (mainrouter::accept);
    Server.listen (Result-> {
      if (result.succeeded ()) {
        
      } else {
       
      }
    });
Mainrouter = Router.router (VERTX);
Mainrouter.mountsubrouter ("/api", router);
Router Router = Router.router (VERTX);
Router.route (Httpmethod.post, "/hello"). Handler (Hellohandler ():: HandleRequest);


Asynchronous encapsulation

Executor.executeblocking ((future<t> f)-> {
      try {
          jdbc_routine ();
        }
        F.complete ();
      } catch (SQLException e) {
        f.fail (e);
      }
    }, Handler);

VERT.X3 supports many common tools: metrics, hot deployment, Consul, Kafka, MONGO, Redis, etc.

be interested in viewing the components on the GitHub

Vert.x's executive unit is called Verticle. The entry of the program, each language may be implemented in a different way, such as Java needs to inherit a abstractverticle abstract class

There are two types of verticle, one based on eventloop for I/o-intensive, and one for CPU-intensive worker verticle. and verticle communicate with each other only through the Eventbus, can support point to point communication, but also can support publish & Subscribe communication mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.