AbstractBootstrap的研读
AbstractBootstrap是一个工具类,用来配置和启动Channel的,下面看下AbstractBootstrap的类继承,ServerBootstrap用于启动ServerChannel的,是服务端的工具类,Bootstrap是用于启动Channel,是客户端的工具类,bind用于udp等无连接的传输,bind用于有连接的传输。
AbstractBootstrap
该类用来配置启动一个Channel,那么配置启动一个Channel需要那些条件?首先需要一个Channel,AbstractBootstrap提供了一个ChannelFactory对象用来创建Channel,一个Channel会对应一个EventLoop用于IO的事件处理,在Channel的整个生命周期中会绑定一个EventLoop,这里可理解给Channel分配一个线程进行IO事件处理,结束后回收该线程,但是AbstractBootstrap没有提供EventLoop而是提供了一个EventLoopGroup,EventLoop继承EventLoopGroup,在某些情况下可以把EventLoopGroup当EventLoop来用,EventLoopGroup中包含多个Eventloop,当一个连接到达,Netty会注册一个Channel,然后EventLoopGroup会分配一个EventLoop绑定到这个channel。不管是服务器还是客户端的Channel都需要绑定一个本地端口这就有了SocketAddress类的对象localAddress,Channel有很多选项所有有了options对象LinkedHashMap<channeloption<?>, Object>。怎么处理Channel的IO事件呢,我们添加一个事件处理器ChannelHandler对象。
privatevolatileEventLoopGroup group;
privatevolatileChannelFactory channelFactory;
privatevolatileSocketAddress localAddress;
private final Map,Object> options =newLinkedHashMap,Object>();
private final Map,Object> attrs =newLinkedHashMap,Object>();
privatevolatileChannelHandler handler;
AbstractBootstrap还有一个重要方法doBind(),为什么这个方法会在父类实现而不是在ServerBootstrap中实现,注解里面写了bind()方法不仅可以在ServerBootstrap中绑定一个端口用于监听,也可以用在BootStrap中用于无连接的数据传输。这里主要调用Channel的bind方法。因为netty中多有的操作都是异步的。所以所有IOC操作都返回future的子类。然后判断是否OK,有时间写一篇该文章。
Bootstrap
客户端的帮助类,用来配置启动一个channel,该类有2个主要方法,一个init用于初始化通道,一个是doConnect(),这里面会生成一个客户端的Channel。
privateChannelFuture doConnect(finalSocketAddress remoteAddress,finalSocketAddress localAddress){
finalChannelFuture regFuture = initAndRegister();
finalChannel channel = regFuture.channel();
if(regFuture.cause()!=null){
return regFuture;
}
finalChannelPromise promise = channel.newPromise();
if(regFuture.isDone()){
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
}else{
regFuture.addListener(newChannelFutureListener(){
@Override
publicvoid operationComplete(ChannelFuture future)throwsException{
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
}
});
}
return promise;
}
EventLoopGroup group =newNioEventLoopGroup();
try{
Bootstrap b =newBootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true)
.handler(newChannelInitializer<SocketChannel>(){
@Override
publicvoid initChannel(SocketChannel ch)throwsException{
ChannelPipeline p = ch.pipeline();
if(sslCtx !=null){
p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
}
//p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(newEchoClientHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(HOST, PORT).sync();//
// Wait until the connection is closed.
f.channel().closeFuture().sync();
}finally{
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
privatefinalMap<ChannelOption<?>,Object> childOptions =newLinkedHashMap<ChannelOption<?>,Object>();
privatefinalMap<AttributeKey<?>,Object> childAttrs =newLinkedHashMap<AttributeKey<?>,Object>();
privatevolatileEventLoopGroup childGroup;
privatevolatileChannelHandler childHandler;

p.addLast(newChannelInitializer<Channel>(){//当有Channel连接该监听端口是调用该
@Override
publicvoid initChannel(Channel ch)throwsException{
ch.pipeline().addLast(newServerBootstrapAcceptor(
currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
EventLoopGroup bossGroup =newNioEventLoopGroup(1);
EventLoopGroup workerGroup =newNioEventLoopGroup();
try{
ServerBootstrap b =newServerBootstrap();
b.group(bossGroup, workerGroup)
/**
* 设置服务端的Channel,Netty通过Channel工厂类创建不同的Channel,对于服务端,
* Netty需要创建NioServerSocketChannel,
*/
.channel(NioServerSocketChannel.class)
/**
* 设置TCP的参数,这里设置了套接字的最大连接个数。
*/
.option(ChannelOption.SO_BACKLOG,100)
/**
* 设置父类的Handler,父类的handler是客户端新接入的接连SocketChannel对应的ChannelPipeline
* 的handler
*/
.handler(newLoggingHandler(LogLevel.INFO))
/**
* 子类的Hanlder是NioServerSockerChannel对应的ChannelPipeline的Handler
*/
/**
* 区别:ServerBootstrap中的handler是NioServerSocketChannel使用
* 所有连接该监听端口的客户端都会执行它,父类的AbstractBootstrap中的Handler
* 是个工厂类,他为没有新接入的客户端都创建一个新的Handler
*/
.childHandler(newChannelInitializer<SocketChannel>(){
@Override
publicvoid initChannel(SocketChannel ch)throwsException{
ChannelPipeline p = ch.pipeline();
if(sslCtx !=null){
p.addLast(sslCtx.newHandler(ch.alloc()));
}
//p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(newEchoServerHandler());
}
});
/**
*每个channel绑定一个ChannelPipeline,一个ChannelPipeline里面添加了很多handler
*/
// Start the server.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
}finally{
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
AbstractBootstrap的研读的更多相关文章
- 转:研读代码必须掌握的Eclipse快捷键
总结的很不错,而且有相应的用法,推荐!!! from: http://www.cnblogs.com/yanyansha/archive/2011/08/30/2159265.html 研读代码必须掌 ...
- iOS组件化思路-大神博客研读和思考
一.大神博客研读 随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的 ...
- Automatic Generation of Animated GIFs from Video论文研读及实现
论文地址:Video2GIF: Automatic Generation of Animated GIFs from Video 视频的结构化分析是视频理解相关工作的关键.虽然本文是生成gif图,但是 ...
- 【Netty】(4)—源码AbstractBootstrap
源码AbstractBootstrap 一.概念 AbstractBootstrap是一个工具类,用于服务器通道的一系列配置,绑定NioEventLoopGroup线程组,指定指定NIO的模式,指定子 ...
- AD预测论文研读系列2
EARLY PREDICTION OF ALZHEIMER'S DISEASE DEMENTIA BASED ON BASELINE HIPPOCAMPAL MRI AND 1-YEAR FOLLOW ...
- AD预测论文研读系列1
A Deep Learning Model to Predict a Diagnosis of Alzheimer Disease by Using 18F-FDG PET of the Brain ...
- QA系统Match-LSTM代码研读
QA系统Match-LSTM代码研读 背景 在QA模型中,Match-LSTM是较早提出的,使用Prt-Net边界模型.本文是对阅读其实现代码的总结.主要思路是对照着论文和代码,对论文中模型的关键结构 ...
- GoogLeNetv4 论文研读笔记
Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning 原文链接 摘要 向传统体系结构中引入 ...
- GoogLeNetv3 论文研读笔记
Rethinking the Inception Architecture for Computer Vision 原文链接 摘要 卷积网络是目前最新的计算机视觉解决方案的核心,对于大多数任务而言,虽 ...
随机推荐
- 浅学soap--------3
//person.wsdl 标签 <?xml version="1.0" ?> <definitions name="person" targ ...
- js中的点击事件(click)的实现方式
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- Nginx简介及配置介绍
一.什么是Nginx Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发 ...
- 软件架构设计 ADMEMS方法体系
ADMEMS是Architecture Design Method has been Extended to Method System的简称,是由CSAI顾问团架构设计专家组于2009年11月在第六 ...
- bootstrap-table 父子表入门篇
官方文档:http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/#多语言 一.引入js.css <!-- 引入bootstrap ...
- (转)C#正则表达式Regex类的用法
原文地址如下:http://www.studyofnet.com/news/297.html 一.C#正则表达式符号模式 字 符 描 述 \ 转义字符,将一个具有特殊功能的字符转义为一个普通字符,或反 ...
- Azure Blob存储更改缓存时间
Azure的Blob存储可以作为Http的服务来使用.很多客户已经把Blob作为图片存储的服务,这样稍作代码的修改,解决了图片服务器带宽.性能等多种问题. 但同时问题也出现了:在传统Http的服务中很 ...
- winrm service
今天看脚本忽然发现一个服务,叫winRM服务,这是个PowerShell的远程管理.开启它可以很大程度的方便用PowerShell操控! 下面是我找到的一些资料: 在Linux中,我们可以使用安全的S ...
- HDFS之四:HDFS原理解析(总体架构,读写操作流程)
前言 HDFS 是一个能够面向大规模数据使用的,可进行扩展的文件存储与传递系统.是一种允许文件通过网络在多台主机上分享的文件系统,可让多机器上的多用户分享文件和 存储空间.让实际上是通过网络来访问文件 ...
- 2018年长沙理工大学第十三届程序设计竞赛 Dzzq的离散数学教室1
Dzzq的离散数学教室1 链接:https://www.nowcoder.com/acm/contest/96/D来源:牛客网 zzq的离散数学教室1 时间限制:C/C++ 1秒,其他语言2秒 空间限 ...