Netty执行流程分析与重要组件介绍
一、环境搭建
创建工程,引入Netty依赖

二、基于Netty的请求响应Demo
1、TestHttpServerHandle 处理器。读取客户端发送过来的请求,并且向客户端返回hello world响应
package com.example.firstexample; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil; public class TestHttpServerHandle extends SimpleChannelInboundHandler<HttpObject>{ //读取客户端发送过来的请求,并且向客户端返回响应
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception { if(httpObject instanceof HttpRequest){
ByteBuf content = Unpooled.copiedBuffer("Hello world", CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
//写回客户端
channelHandlerContext.writeAndFlush(response);
} }
}
2、TestServerInitializer 类
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("httpServerCode", new HttpServerCodec());
pipeline.addLast("testHttpServerHandler", new TestHttpServerHandle());
}
}
3、TestServer 类
package com.example.firstexample; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class TestServer { public static void main(String[] args) throws Exception { //bossGroup获取连接
EventLoopGroup bossGroup = new NioEventLoopGroup();
//workerGroup处理连接
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
//1、启动Bootstrap服务器,服务器关联两个事件循环组bossGroup,workerGroup
//2、并且关联事件处理器,处理器使用TestServerInitializer定义,如TestServerInitializer里使用了处理器TestHttpServerHandle
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new TestServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync(); }finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
} }
}
启动main方法
4、测试

5、总结Netty执行流程
1)启动Bootstrap服务器,服务器关联两个事件循环组bossGroup(获取连接),workerGroup(处理连接)
2)并且关联事件处理器,处理器使用TestServerInitializer定义,如TestServerInitializer里使用了处理器TestHttpServerHandle
Netty执行流程分析与重要组件介绍的更多相关文章
- 报时机器人的rasa shell执行流程分析
本文以报时机器人为载体,介绍了报时机器人的对话能力范围.配置文件功能和训练和运行命令,重点介绍了rasa shell命令启动后的程序执行过程. 一.报时机器人项目结构 1.对话能力范围 (1)能够 ...
- Spring 文件上传MultipartFile 执行流程分析
在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...
- 深入浅出Mybatis系列(十)---SQL执行流程分析(源码篇)
最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅.在前九篇中,介绍了mybatis的配置以及使用, 那么本篇将走进mybatis的源码,分析mybatis 的执行流程, ...
- 深入浅出Mybatis系列十-SQL执行流程分析(源码篇)
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅.在前 ...
- ThinkPHP 框架执行流程分析
总体来说,应用的流程涉及到几个文件:Index.phpThinkPHP.phpThink.class.phpApp.class.phpDispatcher.class.phpThinkPHP/Mode ...
- [转]两表join的multi update语句在MySQL中的执行流程分析
出自:http://hedengcheng.com/?p=209 两表join的multi update语句,执行结果与预计不一致的分析过程 — multi update结论在实际应用中,不要轻易使用 ...
- Hive SQL执行流程分析
转自 http://www.tuicool.com/articles/qyUzQj 最近在研究Impala,还是先回顾下Hive的SQL执行流程吧. Hive有三种用户接口: cli (Command ...
- spark-sql执行流程分析
spark-sql 架构 图1 图1是sparksql的执行架构,主要包括逻辑计划和物理计划几个阶段,下面对流程详细分析. sql执行流程 总体流程 parser:基于antlr框架对 sql解析,生 ...
- Dalvik模式下System.loadLibrary函数的执行流程分析
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78212010 Android逆向分析的过程中免不了碰到Android so被加固的 ...
随机推荐
- WPF中的RecognizesAccessKey问题
Groupbox,CheckBox的模板中默认设置RecognizesAccessKey位True.从而导致下划线没法显示. 只能重写绑定. <CheckBox IsChecked=" ...
- Flask整合WebLoader 用于大附件拆分上传再合并
博客:https://blog.csdn.net/jinixin/article/details/77545140 github:https://github.com/jinixin/upload-d ...
- VMware15.5版本安装CentOS7
VMware15.5版本安装CentOS7 一.在VMware15.5中新建虚拟机 1.打开VMware,在首页面选择创建新的虚拟机. 2.新建虚拟机向导,选择典型配置.3.选择稍后安装操作系统.4. ...
- OpenStack共享组件-Memcache缓存系统
1. 缓存系统 1.1 静态web页面 1> 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Requ ...
- 一些替代Xshell的软件推荐
FinalShell: 面附上一些截图和官方连接: 官网:http://www.hostbuf.com/ FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发, ...
- Codeforces H. Malek Dance Club(找规律)
题目描述: Malek Dance Club time limit per test 1 second memory limit per test 256 megabytes input standa ...
- centos服务器上git clone下载加速
最近在服务器上直接git clone github上的仓库,下载速度只有十几KB,简直不要太慢! 网上搜了一些加速的,自己于是写了下面的总结. 1. nslookup命令 如果执行这个命令找不到,请先 ...
- db2 mysql oracle 邮件 tomcat ssh telnet ftp samba 账号密码
db2 mysql oracle 邮件 tomcat ssh telnet ftp samba 账号密码 检测
- 0031ActiveMQ的下载安装与启动
消息中间件activemq的作用主要是解耦.异步.削峰. 我们按如下步骤详细讲解一下activemq的下载.安装与启动. 1.activemq的下载 下载地址: http://activemq.apa ...
- Json在序列化注意问题
Java中的Json序列化,不容忽视的getter 问题重现 public class AjaxJson { private boolean success; private String msg; ...