The Vertximpl of Vertx core class

Source: Internet
Author: User
Tags eventbus

In Vert.x, Vertx interfaces are the most important interface, and the underlying functionality of Vertx-core is provided in this interface. In this article we will analyze Vertx the internal implementation of the interface system and the creation process. This article corresponds to the vert.x version of 3.2.1.

Vertx Interface System

Let's take Vertx a look at the UML relationships of interfaces:

You can see VertxImpl <:< VertxInternal <:< Vertx that there is this inheritance chain. Here we don't study Measured MetricsProvider the two interfaces first. Let VertxInternal 's look at the structure first:

You can see that there are various ways to manipulate multithreading, perform callbacks, and so on. The VertxInternal interface is only for Vertx-core internal invocation.

VertxImplA class is an implementation of a pair VertxInternal and an Vertx interface. The Vertx instances we created are all VertxImpl .

Vertx creating processes and internal implementations

In general, we will Vertx create Vertx instances through static methods in the interface vertx :

1
Vertx vertx = Vertx.vertx ();

vertxmethod to create a Vertximpl instance from Factory mode:

1
2
3
Vertx() {
return Factory.vertx ();
}
1
2
3
4
5
6
7
8
vertxfactory {

@Override
vertx() {
new Vertximpl ();
}
// ...
}

Let's explore VertxImpl the creation process and internal implementation of the class. Let's look VertxImpl at the instance members of the class first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
PrivateFinal FileSystem FileSystem = Getfilesystem ();
PrivateFinal Shareddata Shareddata;
PrivateFinal Vertxmetrics metrics;
PrivateFinal Concurrentmap<long, internaltimerhandler> timeouts =New Concurrenthashmap<> ();
PrivateFinal Atomiclong Timeoutcounter =New Atomiclong (0);
PrivateFinal Clustermanager Clustermanager;
PrivateFinal Deploymentmanager Deploymentmanager;
PrivateFinal Fileresolver Fileresolver;
PrivateFinal Map<serverid, httpserverimpl> sharedhttpservers =New Hashmap<> ();
PrivateFinal Map<serverid, netserverimpl> sharednetservers =New Hashmap<> ();
Private final executorservice Workerpool;
Private final executorservice Internalblockingpool;
Private final orderedexecutorfactory workerorderedfact;
Private final orderedexecutorfactory internalorderedfact;
Private final threadfactory eventloopthreadfactory;
Private final nioeventloopgroup Eventloopgroup;
Private final nioeventloopgroup Acceptoreventloopgroup;
Private final blockedthreadchecker checker;
Private final boolean haenabled;
private Eventbus eventbus;
private Hamanager Hamanager;
Private boolean closed;

This contains a series of important classes. We will analyze the role of these members in the initialization section. Here's a look at the constructor:

1
2
3
4
5
6
7
8
9
10
11
Vertximpl () {
This (new Vertxoptions ());
}

Vertximpl (vertxoptions options) {
null);
}

Vertximpl (vertxoptions options, handler<asyncresult<vertx>> resulthandler) {
// ...
}

You can see that VertxImpl(VertxOptions options, Handler<AsyncResult<Vertx>> resultHandler) this constructor is eventually called, so let's look at the analysis.

First, VERTX checks whether a Vertx instance is currently running (by factory.context() method). If an instance is running, a warning is given.

1
2
3
4
Sanity check
NULL) {
Log.warn ("You ' re already on a vert.x context, is you sure you want to create a new Vertx instance?");
}

The vertx then initializes the checker member, which is a BlockedThreadChecker function that checks if there are any blocked threads in the VERTX context and warns if the thread is blocked.

1
2
New Blockedthreadchecker (Options.getblockedthreadcheckinterval (), Options.getmaxeventloopexecutetime (),
Options.getmaxworkerexecutetime (), Options.getwarningexceptiontime ());

Next, Vertx initializes the EventLoop thread factory eventLoopThreadFactory , which is used to generate EventLoop threads. It is then initialized eventLoopGroup and configured. NioEventLoopGroupis the concept of Netty, which will be introduced later.

1
2
3
New Vertxthreadfactory (false);
New Nioeventloopgroup (Options.geteventlooppoolsize (), eventloopthreadfactory);
Eventloopgroup.setioratio (Netty_io_ratio);

Next, Vertx initializes the acceptor EventLoop thread factory and configures it. The workerPool internalBlockingPool two thread pools are then initialized. workerPoolIt is used to execute the worker thread, which internalBlockingPool executes the blocking operation thread.

1
2
3
4
5
6
7
8
9
Threadfactory acceptoreventloopthreadfactory =New Vertxthreadfactory ("Vert.x-acceptor-thread-", checker,FALSE);
The Acceptor event loop thread needs to is from a different pool otherwise can get lags in accepted connections
//Under a lot of load
acceptoreventloopgroup = new Nioeventloopgroup ( 1, Acceptoreventloopthreadfactory);
Acceptoreventloopgroup.setioratio ();
Workerpool = Executors.newfixedthreadpool (Options.getworkerpoolsize (),
                                        &NBSP; new vertxthreadfactory ( "vert.x-worker-thread-", Checker, True));
Internalblockingpool = Executors.newfixedthreadpool (Options.getinternalblockingpoolsize (),
                           ,         &NB Sp               new Vertxthreadfactory ( "vert.x-internal-blocking-", Checker, True));

Vertx then initializes the two thread pool factories workerOrderedFact and internalOrderedFact their type OrderedExecutorFactory , which contains a pool of threads that can execute threads sequentially.

1
2
New Orderedexecutorfactory (Workerpool);
New Orderedexecutorfactory (Internalblockingpool);

Next, Vertx initializes the file parser, the fileResolver Deployment Manager, the deploymentManager SPI Manager, metrics and, depending on the configuration, determines whether to initialize the cluster manager and the clusterManager highly available manager haManager . The VERTX then invokes createAndStartEventBus(options, resultHandler) the method, creating and starting Eventbus. Finally, the shared data member sharedData is initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
This.fileresolver =New Fileresolver (this);
This.deploymentmanager =New Deploymentmanager (this);
This.metrics = initialisemetrics (options);
this.haenabled = options.isclustered () && options.ishaenabled ();
if (options.isclustered ()) {
This.clustermanager = Getclustermanager (options);
This.clusterManager.setVertx (this);
This.clusterManager.join (AR-and {
if (ar.failed ()) {
Log.error ("Failed to join Cluster", Ar.cause ());
}else {
Provide a memory barrier as we are setting from a different thread
      synchronized (Vertximpl. This) {
       hamanager = new Hamanager ( this, Deploymentmanager, Clustermanager, Options.getquorumsize (),
                    and nbsp            options.gethagroup (), haenabled);
       createandstarteventbus (options, Resulthandler);
    &NBSP;}
   }
 });
} else {
  this.clustermanager = null;
 createandstarteventbus (options, Resulthandler);
}
this.shareddata = new Shareddataimpl ( this, clustermanager);
} /span>

After a series of constructs, the VertxImpl creation is complete.

Above.

Here are the links:

From VERTX China User Group,

(Different versions, the code may be slightly different (I am using 3.3.2), but it does not hinder reading and understanding)

http://mp.weixin.qq.com/s?__biz=MzA4MjUwNzQ0NQ==&mid=2650547613&idx=1&sn= 5435d20496e4c9f57d958a45855f9f59&chksm= 878c2647b0fbaf51bb8a937c2468117a159e2e0acc8a5bf4ba6404b6282d1f6fd1241f01de25&mpshare=1&scene=23& Srcid=1206gxisvtqnu9b885nqyjzl#rd

The Vertximpl of Vertx core class

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.