[编织消息框架][netty源码分析]5 EventLoopGroup 实现类NioEventLoopGroup职责与实现
分析NioEventLoopGroup最主有两个疑问
1.next work如何分配NioEventLoop
2.boss group 与child group 是如何协作运行的
从EventLoopGroup接口约定通过register方法从channel或promise转换成ChannelFuture对象
next方法就是用来分配NioEventLoop
public interface EventLoopGroup extends EventExecutorGroup { @Override
EventLoop next(); ChannelFuture register(Channel channel);
ChannelFuture register(ChannelPromise promise);
@Deprecated
ChannelFuture register(Channel channel, ChannelPromise promise);
}
为了节省篇副,做了代码整理
1.NioEventLoopGroup构造时绑定SelectorProvider.provider(),通过newChild生成单个EventLoop
2.next实现是个环形循环
3.register方法是将channel转换成ChannelFuture
读者如果感兴趣可以在这几个方法打上断点看看
public class NioEventLoopGroup extends MultithreadEventLoopGroup {
public NioEventLoopGroup(int nThreads, Executor executor) {
this(nThreads, executor, SelectorProvider.provider());
}
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
return new NioEventLoop(this, executor, (SelectorProvider) args[0],
((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}
/////////////////////////////GenericEventExecutorChooser实现next//////////////////////////////////
@Override
public EventExecutor next() {
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
} /////////////////////////////SingleThreadEventLoop实现register////////////////////////////////// @Override
public ChannelFuture register(Channel channel) {
return register(new DefaultChannelPromise(channel, this));
} @Override
public ChannelFuture register(final ChannelPromise promise) {
ObjectUtil.checkNotNull(promise, "promise");
promise.channel().unsafe().register(this, promise);
return promise;
}
}
我们用过程的方式来模拟NioEventLoopGroup使用
如果读者有印象netty server 至少有两组NioEventLoopGroup 一个是boss 另一个是child
public class TestBossChildGroup {
static SocketAddress address = new InetSocketAddress("localhost", 8877); @Test
public void server() throws IOException { SelectorProvider bossProvider = SelectorProvider.provider();
SelectorProvider childProvider = SelectorProvider.provider(); int count = 2;
AbstractSelector bossSelector = bossProvider.openSelector();
AbstractSelector[] childSelectors = new AbstractSelector[count];
for (int i = 0; i < count; i++) {
childSelectors[i] = childProvider.openSelector();
} //server绑定访问端口 并向Selector注册OP_ACCEPT
ServerSocketChannel serverSocketChannel = bossProvider.openServerSocketChannel();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.bind(address);
serverSocketChannel.register(bossSelector, SelectionKey.OP_ACCEPT); int i = 0;
while (true) {
int s = bossSelector.select(300);
if (s > 0) {
Set<SelectionKey> keys = bossSelector.selectedKeys();
Iterator<SelectionKey> it = keys.iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
//为什么不用elseIf 因为 key interestOps 是多重叠状态,一次返回多个操作
if (key.isAcceptable()) {
System.out.println("isAcceptable");
//这里比较巧妙,注册OP_READ交给别一个Selector处理
key.channel().register(childSelectors[i++ % count], SelectionKey.OP_READ);
}
//这部分是child eventLoop处理
if (key.isConnectable()) {
System.out.println("isConnectable");
}
if (key.isWritable()) {
System.out.println("isWritable");
}
if (key.isReadable()) {
System.out.println("isReadable");
}
key.interestOps(~key.interestOps());
it.remove();
}
}
}
} @Test
public void client() throws IOException {
SocketChannel clientSocketChannel = SelectorProvider.provider().openSocketChannel();
clientSocketChannel.configureBlocking(true);
clientSocketChannel.connect(address);
}
}
[编织消息框架][netty源码分析]5 EventLoopGroup 实现类NioEventLoopGroup职责与实现的更多相关文章
- [编织消息框架][netty源码分析]5 eventLoop 实现类NioEventLoopGroup职责与实现
分析NioEventLoopGroup最主有两个疑问 1.next work如何分配NioEventLoop 2.boss group 与child group 是如何协作运行的 从EventLoop ...
- [编织消息框架][netty源码分析]6 ChannelPipeline 实现类DefaultChannelPipeline职责与实现
ChannelPipeline 负责channel数据进出处理,如数据编解码等.采用拦截思想设计,经过A handler处理后接着交给next handler ChannelPipeline 并不是直 ...
- [编织消息框架][netty源码分析]4 eventLoop 实现类NioEventLoop职责与实现
NioEventLoop 是jdk nio多路处理实现同修复jdk nio的bug 1.NioEventLoop继承SingleThreadEventLoop 重用单线程处理 2.NioEventLo ...
- [编织消息框架][netty源码分析]11 ByteBuf 实现类UnpooledHeapByteBuf职责与实现
每种ByteBuf都有相应的分配器ByteBufAllocator,类似工厂模式.我们先学习UnpooledHeapByteBuf与其对应的分配器UnpooledByteBufAllocator 如何 ...
- [编织消息框架][netty源码分析]8 Channel 实现类NioSocketChannel职责与实现
Unsafe是托委访问socket,那么Channel是直接提供给开发者使用的 Channel 主要有两个实现 NioServerSocketChannel同NioSocketChannel 致于其它 ...
- [编织消息框架][netty源码分析]9 Promise 实现类DefaultPromise职责与实现
netty Future是基于jdk Future扩展,以监听完成任务触发执行Promise是对Future修改任务数据DefaultPromise是重要的模板类,其它不同类型实现基本是一层简单的包装 ...
- [编织消息框架][netty源码分析]7 Unsafe 实现类NioSocketChannelUnsafe职责与实现
Unsafe 是channel的内部接口,从书写跟命名上看是不公开给开发者使用的,直到最后实现NioSocketChannelUnsafe也没有公开出去 public interface Channe ...
- [编织消息框架][netty源码分析]13 ByteBuf 实现类CompositeByteBuf职责与实现
public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements Iterable<ByteBuf ...
- [编织消息框架][netty源码分析]3 EventLoop 实现类SingleThreadEventLoop职责与实现
eventLoop是基于事件系统机制,主要技术由线程池同队列组成,是由生产/消费者模型设计,那么先搞清楚谁是生产者,消费者内容 SingleThreadEventLoop 实现 public abst ...
随机推荐
- 141 x的平方根
原题网址:http://www.lintcode.com/zh-cn/problem/sqrtx/ 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 您在真实的面试中是否遇到过这 ...
- System.Web.Mvc.RoutePrefixAttribute.cs
ylbtech-System.Web.Mvc.RoutePrefixAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutra ...
- 阿里云CentOs7上安装GitLab
一.安装 基本上可以根据官网的教程来安装:https://www.gitlab.com.cn/installation/#centos-7 只不过我们暂时没有邮件服务器,所以postfix没有安装. ...
- 关于C++ const 的全面总结 分类: ubuntu 2014-12-03 21:03 72人阅读 评论(0) 收藏
C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...
- 国外主机如何ICP备案
想都不要想了,无法备案. 因为,备案是在主机服务器提供商处的备案平台提交申请,国外的主机服务商是没有这种平台服务的.(跟你域名在哪儿买的没关系) 下面,把昨天折腾到半夜的过程记录一下,希望可以帮到需要 ...
- QT之QStatusBar
1.QStatusBar一般处于主窗体(QMainWindow)的左下角用于显示比较重要的状态信息.我们通常调用其showMessage()函数 QT官方显示:[slot] void QStatusB ...
- Collection单列集合中的常用实现类
Collection 集合层次的根接口 List 有序 有索引 可以重复 ArrayList 底层数据结构是数组 查询快 增删快 线程不安全 效率高 LinkedList 底层数据结构是链表 查询慢 ...
- Java 面试题经典 77 问(含答案)!
金三银四了,3月底,4月初,找工作换单位的黄金时期.4月初将会有有一大批职场人士流动... 作为Java开发码农的你是不是也在蠢蠢欲动,或者已经搞了几轮车轮战了? 我们为大家准备了 77 道经典 Ja ...
- 关于tomcat配置了虚拟路径,但是在Idea中无法生效的问题
1. 确认 tomcat 的server.xml 文件中配置的虚拟路径是否正确 其中,path表示访问的虚拟路径,docBase表示真实路径 2. Idea 服务器配置中是否勾选 “Deploy ap ...
- python pywin32学习笔记
参考博客链接 https://blog.csdn.net/polyhedronx/article/details/81988948 参考博客链接 https://www.cnblogs.com/zha ...