当前位置: 首页 > news >正文

Netty:ChannelInitializer添加到ChannelPipeline完成任务以后会自动删除自己

说明

io.netty.channel.ChannelInitializer是一个特殊的ChannelInboundHandler。它的主要作用是向 Channel对应的ChannelPipeline中增加ChannelHandler。执行完ChannelInitializer的initChannel(C ch)函数以后,ChannelInitializer就会从ChannelPipeline自动删除自己,防止重复进入。
一般是通过Bootstrap.handler(ChannelHandler) 、ServerBootstrap.childHandler(ChannelHandler)的方式分别在客户端、服务端中将ChannelInitializer这个ChannelHandler增加到ChannelPipeline中。

示例

思路

我们来验证下ChannelInitializer是否会被自动删除。我们在ChannelInitializer的initChannel(C ch)函数末尾打印出来当前ChannelPipeline中已经添加的ChannelHandler名称;在Channel连接成功以后,再打印出来当前ChannelPipeline中已经添加的ChannelHandler名称,两者对照下,就能看出来是否被自动删除了。

代码片段

package com.thb.power.terminal;import java.io.BufferedReader;
import java.io.InputStreamReader;import com.thb.power.packet.register.RegisterRequestPacket;import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;/*** 主函数* @author thb**/
public class Terminal {// 要连接的服务端的host static final String HOST = System.getProperty("host", "127.0.0.1"); // 要连接的服务端的端口号 static final int PORT = Integer.parseInt(System.getProperty("port", "22335"));public static void main(String[] args) throws Exception {// 配置客户端EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new TerminalInitializer());// 启动客户端Channel ch = b.connect(HOST, PORT).sync().channel();		// 查看ChannelPipeline添加的ChannelHandler名称System.out.println("after channel connected:");for (String name : ch.pipeline().names()) {System.out.println("handler name: " + name);} ChannelFuture lastWriteFuture = null;BufferedReader in = new BufferedReader(new InputStreamReader(System.in));System.out.println("please input(register):");for (;;) {String line = in.readLine();if (line == null) {break;}				// 如果用户输入register,表示命令客户端发送注册请求给服务器if (line.toLowerCase().equals("register")) {RegisterRequestPacket registerRequest = new RegisterRequestPacket();// 返回的ByteBuf存放着注册请求的数据ByteBuf buf = registerRequest.build(ch);lastWriteFuture = ch.writeAndFlush(buf);						}}if (lastWriteFuture != null) {lastWriteFuture.sync();}} finally {// 关闭event loop以便终止所有的线程group.shutdownGracefully();}}}// ChannelInitializer的子类
package com.thb.power.terminal;import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;public class TerminalInitializer extends ChannelInitializer<SocketChannel> {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline p = ch.pipeline();p.addLast(new LoggingHandler(LogLevel.INFO));// 查看ChannelPipeline添加的ChannelHandler名称System.out.println("in initChannel(SocketChannel ch) method of TerminalInitializer:");for (String name : ch.pipeline().names()) {System.out.println("handler name: " + name);} }
}

运行输出

在这里插入图片描述

从上面输出可以看出,ChannelInitializer开始被添加到ChannelPipeline中,后来又被自动删除了。

http://www.lryc.cn/news/113628.html

相关文章:

  • 【VUE】项目本地开启https访问模式(vite4)
  • 【状态估计】一维粒子滤波研究(Matlab代码实现)
  • 设计模式-迭代器模式在Java中使用示例
  • Maven入职学习
  • 【多音音频测试信号】具有指定采样率和样本数的多音信号,生成多音信号的相位降低波峰因数研究(Matlab代码实现)
  • LeetCode150道面试经典题-删除有序数组中的重复项(简单)
  • 人大金仓数据库Docker部署
  • Leetcode-每日一题【剑指 Offer 07. 重建二叉树】
  • Shell编程快速入门
  • wpf 3d 坐标系和基本三角形复习
  • 如何安全变更亚马逊收款账户?
  • 大数据面试题:Hadoop中的几个进程和作用
  • 题解:ABC276D - Divide by 2 or 3
  • 后台管理系统
  • C++数据结构之平衡二叉搜索树(一)——AVL的实现(zig与zag/左右双旋/3+4重构)
  • 静态库和动态库
  • 用于Voronoi图构建的Fortune算法的C++实现
  • 笔记汇总 | 斯坦福 CS229 机器学习
  • git 版本管理工具 学习笔记
  • Bean基本注解开发和Bean依赖注入注解开发
  • 使用IIS服务器搭建一个网站
  • HCIP 三层交换机
  • 利用python 进行数据分析(第三版)第二章小结
  • 【ASP.NET MVC】使用动软(四)(12)
  • 【web逆向】全报文加密及其登录流程的分析案例
  • MyBatis枚举映射类讨论
  • 微信开发之朋友圈自动点赞的技术实现
  • Linux命令200例:sed对文本进行修改、替换和删除等操作的强大工具(常用)
  • python 合并多个excel文件
  • 【Docker】性能测试监控平台搭建:InfluxDB+Grafana+Jmeter+cAdvisor