简单阐述一下Netty中Channel的生命周期(SimpleChannelInboundHandler)

2021/5/17 18:26:56

本文主要是介绍简单阐述一下Netty中Channel的生命周期(SimpleChannelInboundHandler),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 1.channelRegistered(注册事件)
  • 2.channelUnregistered(取消注册事件)
  • 3.channelActive(是否是活跃)
  • 4.channelInactive(不活跃)
  • 5.channelReadComplete(读取完毕事件)
  • 6.userEventTriggered(用户事件触发)
  • 7.channelWritabilityChanged(可写更改)
  • 8.exceptionCaught(捕获到异常)
  • 9.handlerAdded(拦截器的添加)
  • 10.handlerRemoved(拦截器移除)
  • 11.channelRead0(读取数据)
  • 12.执行顺序(当浏览器发送一次请求后)


1.channelRegistered(注册事件)

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

2.channelUnregistered(取消注册事件)

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

3.channelActive(是否是活跃)

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

4.channelInactive(不活跃)

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

5.channelReadComplete(读取完毕事件)

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

6.userEventTriggered(用户事件触发)

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

7.channelWritabilityChanged(可写更改)

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

8.exceptionCaught(捕获到异常)

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

9.handlerAdded(拦截器的添加)

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

10.handlerRemoved(拦截器移除)

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

11.channelRead0(读取数据)

@Override
	protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) 
			throws Exception {
		Channel channel = ctx.channel();//获取当前channel
		System.out.println(channel.remoteAddress()); //显示客户端的远程地址
		//定义发送的数据消息
		ByteBuf content = Unpooled.copiedBuffer("Hello cxl",CharsetUtil.UTF_8);
		//构建一个http响应
		FullHttpResponse response =
				new DefaultFullHttpResponse
				(HttpVersion.HTTP_1_1,
						HttpResponseStatus.OK,
						content);
		
		response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
		response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
		
		//把响应刷到客户端
		ctx.writeAndFlush(response);				
	}

12.执行顺序(当浏览器发送一次请求后)

在这里插入图片描述



这篇关于简单阐述一下Netty中Channel的生命周期(SimpleChannelInboundHandler)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程