Netty学习笔记 5 核心模块组件一(Bootstrap、ServerBootstrap、Future、ChannelFuture、Channel、Selector、ChannelHandler)

Netty学习笔记 Bootstrap、ServerBootstrap

Bootstrap 意思是引导,一个 Netty 应用通常由一个 Bootstrap 开始,主要作用是配置整个 Netty 程序,串联各个组件,Netty 中 Bootstrap 类是客户端程序的启动引导类,ServerBootstrap 是服务端启动引导类

常见的方法有
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup),该方法用于服务器端,用来设置两个 EventLoop
public B group(EventLoopGroup group) ,该方法用于客户端,用来设置一个 EventLoop
public B channel(Class<? extends C> channelClass),该方法用来设置一个服务器端的通道实现
public B option(ChannelOption option, T value),用来给 ServerChannel 添加配置
public ServerBootstrap childOption(ChannelOption childOption, T value),用来给接收到的通道添加配置
public ServerBootstrap childHandler(ChannelHandler childHandler),该方法用来设置业务处理类(自定义的 handler)
public ChannelFuture bind(int inetPort) ,该方法用于服务器端,用来设置占用的端口号
public ChannelFuture connect(String inetHost, int inetPort) ,该方法用于客户端,用来连接服务器端

Future、ChannelFuture

Netty 中所有的 IO 操作都是异步的,不能立刻得知消息是否被正确处理。但是可以过一会等它执行完成或者直接注册一个监听,具体的实现就是通过 Future 和 ChannelFutures,他们可以注册一个监听,当操作执行成功或失败时监听会自动触发注册的监听事件

常见的方法有
Channel channel(),返回当前正在进行 IO 操作的通道
ChannelFuture sync(),等待异步操作执行完毕

Channel

Netty 网络通信的组件,能够用于执行网络 I/O 操作。
通过Channel 可获得当前网络连接的通道的状态
通过Channel 可获得 网络连接的配置参数 (例如接收缓冲区大小)

Channel 提供异步的网络 I/O 操作(如建立连接,读写,绑定端口),异步调用意味着任何 I/O 调用都将立即返回,并且不保证在调用结束时所请求的 I/O 操作已完成
调用立即返回一个 ChannelFuture 实例,通过注册监听器到 ChannelFuture 上,可以 I/O 操作成功、失败或取消时回调通知调用方
支持关联 I/O 操作与对应的处理程序
不同协议、不同的阻塞类型的连接都有不同的 Channel 类型与之对应,

常用的 Channel 类型

NioSocketChannel,异步的客户端 TCP Socket 连接。
NioServerSocketChannel,异步的服务器端 TCP Socket 连接。
NioDatagramChannel,异步的 UDP 连接。
NioSctpChannel,异步的客户端 Sctp 连接。
NioSctpServerChannel,异步的 Sctp 服务器端连接,这些通道涵盖了 UDP 和 TCP 网络 IO 以及文件 IO。

Selector

Netty 基于 Selector 对象实现 I/O 多路复用,通过 Selector 一个线程可以监听多个连接的 Channel 事件。
当向一个 Selector 中注册 Channel 后,Selector 内部的机制就可以自动不断地查询(Select) 这些注册的 Channel 是否有已就绪的 I/O 事件(例如可读,可写,网络连接完成等),这样程序就可以很简单地使用一个线程高效地管理多个 Channel

ChannelHandler 及其实现类

ChannelHandler 是一个接口,处理 I/O 事件或拦截 I/O 操作,并将其转发到其 ChannelPipeline(业务处理链)中的下一个处理程序。
ChannelHandler 本身并没有提供很多方法,因为这个接口有许多的方法需要实现,方便使用期间,可以继承它的子类
ChannelHandler 及其实现类一览图(后)
在这里插入图片描述
ChannelInboundHandler 用于处理入站 I/O 事件。
ChannelOutboundHandler 用于处理出站 I/O 操作。

//适配器
ChannelInboundHandlerAdapter 用于处理入站 I/O 事件。
ChannelOutboundHandlerAdapter 用于处理出站 I/O 操作。
ChannelDuplexHandler 用于处理入站和出站事件。

我们经常需要自定义一个 Handler 类去继承 ChannelInboundHandlerAdapter,然后通过重写相应方法实现业务逻辑,我们接下来看看一般都需要重写哪些方法
public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter 
implements ChannelInboundHandler {    
public ChannelInboundHandlerAdapter() { }    
public void channelRegistered(ChannelHandlerContext ctx) throws 
Exception {        ctx.fireChannelRegistered();    }    
public void channelUnregistered(ChannelHandlerContext ctx) throws
 Exception {        ctx.fireChannelUnregistered();    }    //通道就绪事件    
public void channelActive(ChannelHandlerContext ctx) throws
 Exception {        ctx.fireChannelActive();    }    
public void channelInactive(ChannelHandlerContext ctx) throws
 Exception {        ctx.fireChannelInactive();    }    //通道读取数据事件    
public void channelRead(ChannelHandlerContext ctx, Object msg) throws 
Exception {        ctx.fireChannelRead(msg);    }    //数据读取完毕事件    
public void channelReadComplete(ChannelHandlerContext ctx) throws 
Exception {        ctx.fireChannelReadComplete();    }    
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws 
Exception {        ctx.fireUserEventTriggered(evt);    }    
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws 
Exception {        ctx.fireChannelWritabilityChanged();    }    //通道发生异常事件    
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws 
Exception {        ctx.fireExceptionCaught(cause);    }}

猜你喜欢

转载自blog.csdn.net/zyzy123321/article/details/107605326