Netty中ChannelInboundHandlerAdapter的生命周期

方法目录

    • 1.channelRegistered
    • 2.channelUnregistered
    • 3.channelActive
    • 4.channelInactive
    • 5.channelReadComplete
    • 6.userEventTriggered
    • 7.channelWritabilityChanged
    • 8.exceptionCaught
    • 9.handlerAdded
    • 10.handlerRemoved
    • 11.channelRead
    • 12.执行顺序

1.channelRegistered

channel注册事件

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel注册了");
		super.channelRegistered(ctx);
	}

2.channelUnregistered

channel取消注册事件

@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel移除了");
		super.channelUnregistered(ctx);
	}

3.channelActive

channel是否是活跃

@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 活跃");
		super.channelActive(ctx);
	}

4.channelInactive

channel不活跃

@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 不活跃,断开连接");
		super.channelInactive(ctx);
	}

5.channelReadComplete

channel读取完毕事件

@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 读取完毕");
		super.channelReadComplete(ctx);
	}

6.userEventTriggered

channel 用户事件触发

@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
     
		System.out.println("channel 用户事件触发");
		super.userEventTriggered(ctx, evt);
	}

7.channelWritabilityChanged

channel 可写更改

@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 可写更改");
		super.channelWritabilityChanged(ctx);
	}

8.exceptionCaught

channel 捕获到异常

@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
     
		System.out.println("channel 捕获到异常了,关闭了");
		super.exceptionCaught(ctx, cause);
	}

9.handlerAdded

channel 助手类(拦截器)的添加

@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 助手类添加");
		super.handlerAdded(ctx);
	}

10.handlerRemoved

channel 助手类(拦截器)移除

@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
     
		System.out.println("channel 助手类移除");
		super.handlerRemoved(ctx);
	}

11.channelRead

channel读取数据

System.out.println("Server: " + obj);
		String msg = (String) obj;
		String result = "";
		if(!StringUtil.isEmpty(msg)){
     
			String commandType = msg.substring(10,12);
			AbstractExecutor executor = SpringBeanUtil.executor(commandType);
			if(executor == null){
     
				result = "0000";
			}else{
     
				boolean crc16Val = executor.valiCRC16(msg);
				if(crc16Val){
     
					executor.tcpExecutor(msg);
					String content = executor.downCommand(msg);
					System.out.println("发送:"+content);
					result = content;
				}else{
     
					result = "0000";
				}
			}
		}
		result = "0000";
		byte[] bytes =  StringUtil.hexStrToBinaryStr((String) result);
		ctx.writeAndFlush(Unpooled.copiedBuffer(bytes));

12.执行顺序

Netty中ChannelInboundHandlerAdapter的生命周期_第1张图片

你可能感兴趣的:(Netty专题)