环境准备
源码下载
国内:https://gitee.com/mirrors/vertx
github: https://github.com/eclipse-vertx/vert.x
解决依赖报错
本系列以3.8为基础进行分析,所以请讲源码调整到3.8分支,
并修改pom.xml:
<stack.version>3.8.0</stack.version>
由于maven仓库的3.8.0不存在后边的SNAPSHOT,如果不修改则maven找不到相应的配置文件
启动流程
初始化vertx
@Override
public Vertx vertx() {
return vertx(new VertxOptions());
}
@Override
public Vertx vertx(VertxOptions options) {
if (options.getEventBusOptions().isClustered()) {
throw new IllegalArgumentException("Please use Vertx.clusteredVertx() to create a clustered Vert.x instance");
}
return VertxImpl.vertx(options);
}
VertxOptions配置请查看另外一篇博客
static VertxImpl vertx(VertxOptions options) {
VertxImpl vertx = new VertxImpl(options, Transport.transport(options.getPreferNativeTransport()));
vertx.init();
return vertx;
}
//transport 与netty的相同,具体请自行学习netty相关知识
private VertxImpl(VertxOptions options, Transport transport) {
// Sanity check
if (Vertx.currentContext() != null) {
log.warn("You're already on a Vert.x context, are you sure you want to create a new Vertx instance?");
}
//关闭vertx时触发的handler
closeHooks = new CloseHooks(log);
//阻塞线程检查(检查Map<VertxThread, Object> threads中注册的线程,线程通过registerThread来注册)
checker = new BlockedThreadChecker(options.getBlockedThreadCheckInterval(), options.getBlockedThreadCheckIntervalUnit(), options.getWarningExceptionTime(), options.getWarningExceptionTimeUnit());
//通过VertxThreadFactory创建EventLoop线程池
eventLoopThreadFactory = new VertxThreadFactory("vert.x-eventloop-thread-", checker, false, options.getMaxEventLoopExecuteTime(), options.getMaxEventLoopExecuteTimeUnit());
//根据eventLoopThreadFactory的配置真正的创建eventLoopGroup
eventLoopGroup = transport.eventLoopGroup(options.getEventLoopPoolSize