做网站要多少钱,wordpress换nginx 数据库,哪个网站做二微码,公司logo如何注册一、InboundHandler 和OutboundHandler的区别
在Netty中#xff0c;inbound表示来自外部来源#xff08;如网络连接#xff09;的数据#xff0c;而outbound则表示从应用程序发送到外部目标#xff08;如网络连接或其他服务#xff09;的数据。…一、InboundHandler 和OutboundHandler的区别
在Netty中inbound表示来自外部来源如网络连接的数据而outbound则表示从应用程序发送到外部目标如网络连接或其他服务的数据。
Inbound主要涉及应用程序接收和处理外部数据的过程。这包括从网络连接读取数据、解码处理数据、执行业务逻辑等操作。例如在一个服务器应用程序中inbound操作可能涉及监听和接受客户端连接读取客户端发送的请求数据并将其转发给适当的处理程序进行处理。inbound监听的事件如下
channelRegistered/channelUnregisteredchannelActive/channelInactivechannelReadchannelReadCompletechannelWritabilityChangeduserEventTriggeredexceptionCaught
Outbound主要涉及应用程序发送数据到外部目标的过程。这包括将数据编码为网络传输的格式、通过网络连接发送数据、处理发送的数据等操作。例如在一个客户端应用程序中outbound操作可能涉及将请求数据编码为适当的传输协议通过网络连接发送给服务器并等待响应数据。outbound监听的事件如下
bindconnectdisconnectclosederegisterreadwriteflush
Netty的Pipeline采用责任链设计模式责任链的每个节点是ChannelHandlerContext通过prev和next节点实现双向链表。ChannelHandlerContext对象封装了ChannelHandler对象。Pipeline的首尾节点分别是io.netty.channel.DefaultChannelPipeline.HeadContext和io.netty.channel.DefaultChannelPipeline.TailContext。
Pipeline在分发事件的时候会根据事件类型选择合适的handlerInbound/Outbound本身会通用掩码位注册监听的事件类型可参考 《Netty之ChannelHandlerMask详解》
总结
InboundHandler处理从网络接收的数据负责解析和处理输入数据。OutboundHandler处理向网络发送的数据负责封装和处理输出数据。InboundHandler和OutboundHandler在处理数据的方向上有所区别但它们通常一起使用通过ChannelPipeline连接在一起形成一个完整的数据处理链。
二、客户端消息在handler的流向
我们通过简单的例子来说明
2.1注册一个简单的Netty服务器
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class EchoServer {private int port;public EchoServer(int port) {this.port port;}public void start() throws Exception{EventLoopGroup boss new NioEventLoopGroup();EventLoopGroup worker new NioEventLoopGroup();ServerBootstrap bootstrap new ServerBootstrap();bootstrap.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline socketChannel.pipeline();for (int i 0; i 3; i) {pipeline.addLast(new InboundHandler(i 1));}for (int i 0; i 3; i) {pipeline.addLast(new OutboundHandler(i 1));}System.out.println(handler注册顺序);pipeline.names().forEach(x - {System.out.println(handler: x);});}}).option(ChannelOption.SO_BACKLOG,100000).childOption(ChannelOption.SO_KEEPALIVE,true);bootstrap.bind(port).sync();System.out.println(服务启动监听端口port);}public static void main(String[] args) throws Exception {new EchoServer(8001).start();}static class InboundHandler extends ChannelInboundHandlerAdapter {private int index;public InboundHandler(int index) {this.index index;}Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println(InboundChannel[index]接收消息);ctx.fireChannelRead(msg);if (index 3) {ctx.channel().write(df);}}}static class OutboundHandler extends ChannelOutboundHandlerAdapter {private int index;public OutboundHandler(int index) {this.index index;}Overridepublic void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {System.out.println(OutboundChannel[index]发送消息);ctx.write(msg, promise);}}}
在这个例子启动一个简单的socket服务器并依次注册了三个inboundhandler三个outboundhandler。启动之后通过简单的telnet命令往socket发送数据
2.2使用telnet发送数据
使用windows自带的telnet输入命令telnet localhost 8001然后随便输入一个字符
部分系统需要通过按“WinR”快捷键打开“运行”对话框输入“optionalfeatures”后按回车键在打开的“Windows功能”窗口中找到并勾选“Telnet客户端”
2.3服务端响应 从这个例子可以看出
InboundHandler是按照Pipleline的加载顺序顺序执行
而OutboundHandler是按照Pipleline的加载顺序逆序执行。
需要注意的是由于pipeline采取的是责任链方式在任何一个节点如果没有把事件往下传事件就会在本节点终止。 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println(InboundChannel[index]接收消息);ctx.fireChannelRead(msg); //注释次代码则事件不会往下个handler传递数据}