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> {

	 @Override
	 public 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中,后来又被自动删除了。

相关推荐
步步为营DotNet9 分钟前
深度解析CancellationToken:.NET中的优雅取消机制
java·前端·.net
JH30738 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_10 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
invicinble10 小时前
对tomcat的提供的功能与底层拓扑结构与实现机制的理解
java·tomcat
较真的菜鸟10 小时前
使用ASM和agent监控属性变化
java
黎雁·泠崖10 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_124987075311 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_12 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.12 小时前
Day06——权限认证-项目集成
java
瑶山12 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard