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被加固的 ...
随机推荐
- CI框架扩展系统类库
CI框架不支持像yii2框架那样,可以直接在controllers下创建CommonController并继承父类,那么我们想要做登录控制或权限控制时,直接在父类控制器操作是不合理的. 这时比较方便的 ...
- 基于TCP协议的远程终端控制并发socketserver实现以及粘包问题处理
# 客户端 # -*- coding: utf-8 -*- import socketserver import struct import json import subprocess class ...
- sklearn.feature_extraction.text 的TfidfVectorizer函数
TfidfVectorizer函数主要用于,将文档(句子)等通过 tf-idf值来进行表示,也就是用一个tf-idf值的矩阵来表示文档(句子也可). from sklearn.feature_extr ...
- Linux访问控制列表(Access Control List,简称ACL)
Linux访问控制列表(Access Control List,简称ACL) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ACL概述 ACL:Access Control L ...
- 查看Linux 系统的配置,内核版本和增减用户/增减组/增减权限
今天购买了一款腾讯云服务器,一年120RMB 配置也很一般 1核的CPU 2GB内存 1Mbps 带宽 普通云硬盘 50G 操作系统: CentOS 7.2.64 现在来验收一下 17 2019-0 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- axure快速上手
Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写.Axure RP是美国Axur ...
- web自动化测试-获得验证信息
一.概述 1.在编写功能测试用例时,会假定一个预期结果,在执行用例过程中把得到的实际结果与预期结果进行比较,从而判断用例的通过与失败 2.自动化测试用例是由机器去执行,通常机器并不像人一样有思维和判断 ...
- TensorFlow的GPU设置
在使用GPU版的TensorFlow跑程序的时候,如果不特殊写代码注明,程序默认是占用所有主机上的GPU,但计算过程中只会用其中一块.也就是你看着所有GPU都被占用了,以为是在GPU并行计算,但实际上 ...
- 使用Ajax实现三级联动
首先准备数据库只有一张表 分析数据库根据 parentid来查 jsp代码 servlet代码 <%-- Created by IntelliJ IDEA. User: 60590 Date: ...