Netty4.0学习笔记系列之四:混合使用coder和handler
Posted 奔跑-起点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Netty4.0学习笔记系列之四:混合使用coder和handler相关的知识,希望对你有一定的参考价值。
Handler如何使用在前面的例子中已经有了示范,那么同样是扩展自ChannelHandler的Encoder和Decoder,与Handler混合后又是如何使用的?本文将通过一个实际的小例子来展示它们的用法。
该例子模拟一个Server和Client,两者之间通过http协议进行通讯,在Server内部通过一个自定义的StringDecoder把httprequest转换成String。Server端处理完成后,通过StringEncoder把String转换成httpresponse,发送给客户端。具体的处理流程如图所示:
其中红色框中的Decoder、Encoder及request都是Netty框架自带的,灰色框中的三个类是我自己实现的。
Server端的类有:Server StringDecoder BusinessHandler StringEncoder四个类。
1、Server 启动netty服务,并注册handler、coder,注意注册的顺序:
[java] view plain copy
- package com.guowl.testmulticoderandhandler;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioserverSocketChannel;
- import io.netty.handler.codec.http.HttpRequestDecoder;
- import io.netty.handler.codec.http.HttpResponseEncoder;
- // 测试coder 和 handler 的混合使用
- public class Server
- public void start(int port) throws Exception
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try
- ServerBootstrap b = new ServerBootstrap();
- b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer<SocketChannel>()
- @Override
- public void initChannel(SocketChannel ch) throws Exception
- // 都属于ChannelOutboundHandler,逆序执行
- ch.pipeline().addLast(new HttpResponseEncoder());
- ch.pipeline().addLast(new StringEncoder());
- // 都属于ChannelIntboundHandler,按照顺序执行
- ch.pipeline().addLast(new HttpRequestDecoder());
- ch.pipeline().addLast(new StringDecoder());
- ch.pipeline().addLast(new BusinessHandler());
- ).option(ChannelOption.SO_BACKLOG, 128)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
- ChannelFuture f = b.bind(port).sync();
- f.channel().closeFuture().sync();
- finally
- workerGroup.shutdownGracefully();
- bossGroup.shutdownGracefully();
- public static void main(String[] args) throws Exception
- Server server = new Server();
- server.start(8000);
[java] view plain copy
- package com.guowl.testmulticoderandhandler;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import io.netty.handler.codec.http.HttpContent;
- import io.netty.handler.codec.http.HttpHeaders;
- import io.netty.handler.codec.http.HttpRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.guowl.utils.ByteBufToBytes;
- public class StringDecoder extends ChannelInboundHandlerAdapter
- private static Logger logger = LoggerFactory.getLogger(StringDecoder.class);
- private ByteBufToBytes reader;
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
- logger.info("StringDecoder : msg's type is " + msg.getClass());
- if (msg instanceof HttpRequest)
- HttpRequest request = (HttpRequest) msg;
- reader = new ByteBufToBytes((int) HttpHeaders.getContentLength(request));
- if (msg instanceof HttpContent)
- HttpContent content = (HttpContent) msg;
- reader.reading(content.content());
- if (reader.isEnd())
- byte[] clientMsg = reader.readFull();
- logger.info("StringDecoder : change httpcontent to string ");
- ctx.fireChannelRead(new String(clientMsg));
[java] view plain copy
- package com.guowl.testmulticoderandhandler;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import org.slf4j.Logger;
- Memcached学习笔记之四:Memcached统计命令
web自动化测试-D3-学习笔记之四(Selenium-ActionChainsApi接口详解)
Unity Shaders学习笔记——SurfaceShader混合纹理
[分布式系统学习]阅读笔记 Distributed systems for fun and profit 之四 Replication 拷贝