netty--NioEventLoop滴干活
netty是最近项目要用到的nio框架,找了各种资料,发现称赞它的有点多,所以决定用它:其实也就二选一嘛,mina或netty或自己写。对于mina,也不熟,不过看各种介绍,貌似netty干活还是很不错的,尤其是最新的4.x和5.x重构后,且使用结构清晰就先了解了解了。
首先要把应用跑起来啦(官网的例子比较多),我这是一个关于mqtt的一个例子:
m_bossGroup = new NioEventLoopGroup();
m_workerGroup = new NioEventLoopGroup(); final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging); ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector));
pipeline.addFirst("idleStateHandler", new IdleStateHandler(, , Constants.DEFAULT_CONNECT_TIMEOUT));
pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
//pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
pipeline.addLast("decoder", new MQTTDecoder());
pipeline.addLast("encoder", new MQTTEncoder());
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler);
}
})
.option(ChannelOption.SO_BACKLOG, )
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(Constants.PORT);
LOG.info("Server binded");
f.sync();
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
再回想下,我们自己写serversocket的时候是怎么写的呢(这是一个笨拙的实例代码):
ServerSocket socket;
channel = ServerSocketChannel.open(); // 打开通道
socket = channel.socket(); //得到与通到相关的socket对象
socket.bind(new InetSocketAddress(port)); //将scoket榜定在制定的端口上
//配置通到使用非阻塞模式,在非阻塞模式下,可以编写多道程序同时避免使用复杂的多线程
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_ACCEPT);
try {
while (true) {
this.selector.select();
Iterator<SelectionKey> iter = this.selector.selectedKeys().iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
this.handleKey(key); }
}
} catch (IOException ex) {
ex.printStackTrace();
}
原理还是那些,channel.open(),然后register key,然后遍历,再然后才进行handleKey()的干活。
那netty的写法为什么那么潇洒呢,怀着这个莫名的疑问,我先不管它的结构什么的,直接进行search,发现了这么个东东:
NioEventLoop(NioEventLoopGroup parent, ThreadFactory threadFactory, SelectorProvider selectorProvider) {
super(parent, threadFactory, false);
if (selectorProvider == null) {
throw new NullPointerException("selectorProvider");
}
provider = selectorProvider;
selector = openSelector();
}
其中第8行从名称上来看,有点点意思了,往下看:
private Selector openSelector() {
final Selector selector;
try {
selector = provider.openSelector();
其中的provider就是我们熟悉的:java.nio.channels.spi.SelectorProvider类。
所以这个就是做了selector.open的工作。
接下来能看到NioEventLoop:
protected void run() {
for (;;) {
oldWakenUp = wakenUp.getAndSet(false);
try {
if (hasTasks()) {
selectNow();
} else {
select();
再继续看,该类中处理的selectedKey:
final NioUnsafe unsafe = ch.unsafe();
if (!k.isValid()) {
// close the channel if the key is not valid anymore
unsafe.close(unsafe.voidPromise());
return;
} try {
int readyOps = k.readyOps();
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
unsafe.read();
if (!ch.isOpen()) {
// Connection already closed - no need to handle write.
return;
}
}
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
// Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
ch.unsafe().forceFlush();
}
if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
// remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
// See https://github.com/netty/netty/issues/924
int ops = k.interestOps();
ops &= ~SelectionKey.OP_CONNECT;
k.interestOps(ops); unsafe.finishConnect();
}
} catch (CancelledKeyException e) {
unsafe.close(unsafe.voidPromise());
}
现在明白了吧,其实netty也是走这么一套逻辑。
然后再网上看,逻辑是这样:
NioEventLoopGroup extends MultithreadEventExecutorGroup,其初始化了n个单线程的线程池(children = new SingleThreadEventExecutor[nThreads];)
每个单线程的对象child[i]=NioEventLoop对象,每个NioEventLoop有一个Selector字段。
其run方法是该group都需要干活的具体业务逻辑代码。
后续再加上别的类说明。
netty--NioEventLoop滴干活的更多相关文章
- NioEventLoop启动流程源码解析
NioEventLoop的启动时机是在服务端的NioServerSocketChannel中的ServerSocketChannel初始化完成,且注册在NioEventLoop后执行的, 下一步就是去 ...
- 从Netty EventLoop实现上可以学到什么
本文主要讨论Netty NioEventLoop原理及实践,关于Netty NioEventLoop,首先要知道NioEventLoop是什么,为什么它会是Netty核心Reactor处理器,实现原理 ...
- Netty中的这些知识点,你需要知道!
一.Channel Channel是一个接口,而且是一个很大的接口,我们称之为“大而全”,囊括了server端及client端接口所需要的接口. Channel是一个门面,封装了包括网络I/O及相关的 ...
- IE6下png格式图片显示问题
一开始是使用 _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/bgBtn.png'); /* IE6 * ...
- java nio 网络框架实现
maven项目 https://github.com/solq360/common 链式编/解码 链路层链式处理 管道管理socket 多协议处理非常方便 仿netty NioEventLoop 单线 ...
- 从输入 URL 到页面加载完的过程中都发生了什么事情?
1) 把URL分割成几个部分:协议.网络地址.资源路径.其中网络地址指示该连接网络上哪一台计算机,可以是域名或者IP地址,可以包括端口号:协议是从该计 算机获取资源的方式,常见的是HTTP.FTP,不 ...
- 小学生玩ACM----优先队列
思来想去,本人还是觉得,这个优先队列啊,不学不行,怎么说咧?虽说有时候我可以模仿它的功能,但是有的题目会坑的我大放血,况且多学会用一个小东东总不会伤身的撒,何况我是永举不垂的,哦耶,嘿嘿 优先队列嘛就 ...
- java nio 网络框架实现(转)
maven项目https://github.com/solq360/common 链式编/解码 链路层链式处理 管道管理socket 多协议处理非常方便 仿netty NioEventLoop 单线程 ...
- 学习go语言编程系列之helloworld
1. 下载https://golang.org/dl/ # Go语言官网地址,在国内下载太慢,甚至都无法访问.通过如下地址下载:https://golangtc.com/download. 2. 安装 ...
随机推荐
- FIFO 、LRU、LFU三种算法
提到缓存,有两点是必须要考虑的:(1)缓存数据和目标数据的一致性问题.(2)缓存的过期策略(机制). 其中,缓存的过期策略涉及淘汰算法.常用的淘汰算法有下面几种:(1)FIFO:First I ...
- pymongo CursorNotFound错误
部分报错如下: File "D:\anaconda\lib\site-packages\pymongo\cursor.py", line 1189, in next if len( ...
- hibernate经常报的几个不起眼的错误, 都是因为不细心或者手滑了输入有误造成了
最近会经常用到hibernate了, 也经常报错, 看着屏幕上一根大红条是非常不爽的, 这几天集合了一下经常报的错误, 整合一下放到这里, 以后再出现这个错误直接去解决就好了 1, org.hiber ...
- 18-从n个数中选m个
#include <iostream>using namespace std; int f(int n, int m){ if(n < m) //这个条 ...
- php session阻塞页面分析及优化 (session_write_close session_commit使用)
转: http://www.tuicool.com/articles/bqeeey 首先看下下面代码, session1.php 文件 <?php ini_set('session.save_p ...
- Html使用Iframe无刷新上传文件,后台接收
html代码:我是发送请求到teacher_center.aspx,不是到.ashx一般处理程序里,需要加 runat="server",有空我再试试发送请求到 .ashx 里 & ...
- 在windows系统下安装oracle 11g
oracle 11g 安装在windows server 2012 系统下. 最近,需要配置数据库,要求在windows操作系统下,安装oracle 11g 数据库,因为以前没有安装过,所以成功后, ...
- PAT 1034 有理数四则运算(20)(代码框架+思路+测试点错误分析)
1034 有理数四则运算(20)(20 分)提问 本题要求编写程序,计算2个有理数的和.差.积.商. 输入格式: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的 ...
- 转载 springboot 配置读取
前言:了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...
- oracle执行多个pl/sql块
DECLARE V_SQL_DROP_TABLE VARCHAR2(50) := 'DROP TABLE MY_TEST2'; V_SQL_CREATE_TABLE VARCHAR2( ...