Netty启动分析
基于Netty-3.2.5
先看一段Netty的服务端代码:
- import java.net.InetSocketAddress;
- import java.util.concurrent.Executors;
- import org.jboss.netty.bootstrap.ServerBootstrap;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.channel.ChannelPipeline;
- import org.jboss.netty.channel.ChannelPipelineFactory;
- import org.jboss.netty.channel.ChannelStateEvent;
- import org.jboss.netty.channel.Channels;
- import org.jboss.netty.channel.ExceptionEvent;
- import org.jboss.netty.channel.MessageEvent;
- import org.jboss.netty.channel.SimpleChannelHandler;
- import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
- import org.jboss.netty.handler.codec.string.StringDecoder;
- import org.jboss.netty.handler.codec.string.StringEncoder;
- public class NettyServer
- {
- public static void main(String[] args)
- {
- ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
- // Set up the default event pipeline.
- bootstrap.setPipelineFactory(new ChannelPipelineFactory()
- {
- @Override
- public ChannelPipeline getPipeline() throws Exception
- {
- return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
- }
- });
- // Bind and start to accept incoming connections.
- Channel bind = bootstrap.bind(new InetSocketAddress(8000));
- System.out.println("Server已经启动,监听端口: " + bind.getLocalAddress() + ", 等待客户端注册。。。");
- }
- private static class ServerHandler extends SimpleChannelHandler
- {
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
- {
- if (e.getMessage() instanceof String)
- {
- String message = (String) e.getMessage();
- System.out.println("Client发来:" + message);
- e.getChannel().write("Server已收到刚发送的:" + message);
- System.out.println("\n等待客户端输入。。。");
- }
- super.messageReceived(ctx, e);
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception
- {
- super.exceptionCaught(ctx, e);
- }
- @Override
- public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
- {
- System.out.println("有一个客户端注册上来了。。。");
- System.out.println("Client:" + e.getChannel().getRemoteAddress());
- System.out.println("Server:" + e.getChannel().getLocalAddress());
- System.out.println("\n等待客户端输入。。。");
- super.channelConnected(ctx, e);
- }
- }
- }
下面分析这个netty服务的启动过程
核心启动代码:
- ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
- // Set up the default event pipeline.
- bootstrap.setPipelineFactory(new ChannelPipelineFactory()
- {
- @Override
- public ChannelPipeline getPipeline() throws Exception
- {
- return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
- }
- });
- // Bind and start to accept incoming connections.
- Channel bind = bootstrap.bind(new InetSocketAddress(8000));
第一节:建立MainReactor和SubReactor线程池
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
第二节:完成Client请求的ChannelHandle链的建立
- // Set up the default event pipeline.
- bootstrap.setPipelineFactory(new ChannelPipelineFactory()
- {
- @Override
- public ChannelPipeline getPipeline() throws Exception
- {
- return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ServerHandler());
- }
- });
- 第三节:建立一个Server端启动所对应的ChannelHandle链
- // Bind and start to accept incoming connections.
- Channel bind = bootstrap.bind(new InetSocketAddress(8000));
Netty启动分析的更多相关文章
- Netty源码分析第1章(Netty启动流程)---->第3节: 服务端channel初始化
Netty源码分析第一章:Netty启动流程 第三节:服务端channel初始化 回顾上一小节的initAndRegister()方法: final ChannelFuture initAndRe ...
- Netty源码分析第1章(Netty启动流程)---->第4节: 注册多路复用
Netty源码分析第一章:Netty启动流程 第四节:注册多路复用 回顾下以上的小节, 我们知道了channel的的创建和初始化过程, 那么channel是如何注册到selector中的呢?我们继 ...
- Netty源码分析第1章(Netty启动流程)---->第5节: 绑定端口
Netty源码分析第一章:Netty启动步骤 第五节:绑定端口 上一小节我们学习了channel注册在selector的步骤, 仅仅做了注册但并没有监听事件, 事件是如何监听的呢? 我们继续跟第一小节 ...
- Netty启动流程剖析
编者注:Netty是Java领域有名的开源网络库,特点是高性能和高扩展性,因此很多流行的框架都是基于它来构建的,比如我们熟知的Dubbo.Rocketmq.Hadoop等,针对高性能RPC,一般都是基 ...
- SpringBoot源码解析:tomcat启动分析
>> spring与tomcat的启动分析:war包形式 tomcat:xml加载规范 1.contex-param: 初始化参数 2.listener-class: contextloa ...
- Nginx学习笔记(八) Nginx进程启动分析
Nginx进程启动分析 worker子进程的执行循环的函数是ngx_worker_process_cycle (src/os/unix/ngx_process_cycle.c). 其中,捕获事件.分发 ...
- netty启动过程
netty先启动work线程,work线程打开selector 绑定pipeline 启动boss线程,绑定端口,注册selector,绑定op_accetp事件 ------------------ ...
- mkimage工具 加载地址和入口地址 内核启动分析
第三章第二节 mkimage工具制作Linux内核的压缩镜像文件,需要使用到mkimage工具.mkimage这个工具位于u-boot-2013. 04中的tools目录下,它可以用来制作不压缩或者压 ...
- Android Binder------ServiceManager启动分析
ServiceManager启动分析 简述: ServiceManager是一个全局的manager.调用了Jni函数,实现addServicew getService checkService ...
随机推荐
- 20169210《Linux内核原理与分析》第八周作业
第一部分:实验 首先还是网易云课堂的学习,这次的课程是进程的创建和进程的描述. linux进程的状态与操作系统原理中的描述的进程状态有些不同,例如就绪状态和运行状态都是TASK_RUNNING. Li ...
- webbrowser selstart selLength
附件:http://files.cnblogs.com/xe2011/Webbrowser_SelStart.rar 1 获得webBrowser光标所在的位置 2 设置webBrowser光标的位置 ...
- ArcGIS Engine中如何获取Map中已经选择的要素呢
1.使用IEnumFeturea对象获取map中的FeatureSelection,该方法可以获取所有图层的选择要素.IMap中的FeatureSelection可不是IFeatureSelectio ...
- 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
目前国内对于双卡智能手机的需求还是很大的,各种复杂的业务会涉及到双卡模块:而android标准的api又不提供对双卡的支持.导致国内双卡模块标准混乱,各个厂商各玩各的.目前我知道的双卡解决方案就有:m ...
- GCC扩展(转--对看kernel代码有帮助
http://my.oschina.net/senmole/blog?catalog=153878 Linux Kernel的代码,上次就发现一个结构体的定义形式看不懂,后来才知道它用的不是标准的AN ...
- (转)spring ioc原理(看完后大家可以自己写一个spring)
最近,买了本Spring入门书:spring In Action .大致浏览了下感觉还不错.就是入门了点.Manning的书还是不错的,我虽然不像哪些只看Manning书的人那样专注于Manning, ...
- Redis介绍
Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...
- [XML] ResourceManager一个操作Resource的帮助类 (转载)
点击下载 ResourceManager.zip /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更 ...
- 对 JDBC 做一个轻量封装,待完善。。。
对 JDBC 做一个轻量地封装,顺便复习,熟悉sql,io,util,lang.Reflect等包的使用,泛型的使用,待完善... package com.webproj.utils; import ...
- Asp.Net MVC Ajax
将ASP.NET MVC中的form提交改为ajax提交 在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascrip ...