博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Netty in Action笔记(Chpater1 ~ 2) First Netty application
阅读量:6664 次
发布时间:2019-06-25

本文共 2458 字,大约阅读时间需要 8 分钟。

【Chapter 1 Netty and Java NIO APIs】

The entire Netty API is asynchronous.

Asynchronous processing encourages you to use your resources more efficiently by allowing you to start a task and get notified when it's done rather than waiting for it to complete. You're free to do something else while the task is running.

实现异步的两种方法:

  1. 回调(实现接口的方法,即监听器)
  2. Future(A Future object either holds the result of a computation or, in the case of a failed computation, an exception.)。可以取消、检查是否完成,阻塞直到完成

On Linux-like OSs the selector makes use of the epoll- IO event notification facility. This is a high-performance technique in which the OS works asynchronously with the networking stack.Unfortunately, even today the famous epoll- bug can lead to an invalid state in the

selector, resulting in 100% CPU-usage and spinning.

【Chapter 2 Your first Netty application】

Handler可以被多个Channel共享,这时Handler必须被@Sharable注解且必须是线程安全的

ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)        //writeAndFlush是异步操作,返回ChannelFuture    .addListener(ChannelFutureListener.CLOSE);  //通过ChannelFuture.addListener()注册监听器,当Future.isDone()等于true时会执行监听器

对于一个Channel,其pipeline中至少要有一个Handler能够处理异常

Next, override the channelRead0() method. The method is called once data is received.Note that the bytes may be fragmented, which means that if the server writes 5 bytes it s not guaranteed that all 5 bytes will be received at once. For 5 bytes, the channelRead0() method could be called twice, for example. The first time it may be called with a ByteBuf that holds 3 bytes and the second time with a ByteBuf that holds 2 bytes. The only guarantee is that the bytes will be received in the same order as they re sent. But this is only true for TCP or other stream-orientated protocols.

SimpleChannelInboundHandler与ChannelInboundHandlerAdapter的区别:

  • ChannelInboundHandlerAdapter的channelRead(ctx,
    msg)需要释放msg,如果msg是ByteBuf类型,则需要调用ByteBuf.release()方法(因为Netty默认使用支持池的Buf,不release的会发生内存泄露,即这个BUf在JVM退出之前永远不会被GC回收);
  • 而SimpleChannelInboundHandler呢,it will release the message once the
    channelRead0(...) method completes. This is done by Netty to handle
    all messages that are implement ReferenceCounted.
  • But why not do the same in the EchoServerHandler? The reason for this is that we want to echo back the message, which means we can not release it yet as the write operation may completes after channelRead(...) returns (remember write is asynchronous). Once the write completes Netty will automatically release the message.

转载地址:http://mbcxo.baihongyu.com/

你可能感兴趣的文章
C++ 字符串
查看>>
代码即财富之我学正则表达式
查看>>
使用c实现简单的rpc
查看>>
How To Install Maven On Mac OSX
查看>>
Spring事务异常回滚,捕获异常不抛出就不会回滚
查看>>
修改Ubuntu下Eclipse鼠标悬停提示背景颜色
查看>>
Java NIO:浅析I/O模型
查看>>
Android 之 ListView使用ArrayAdapter展示列表
查看>>
苦逼的程序员
查看>>
android checkbox 扩大点击区域
查看>>
如何更改Oracle字符集
查看>>
vim中的命令
查看>>
如何让电脑开机自动拨号联网
查看>>
FastDFS的安装和配置过程
查看>>
Squid学习之旅之透明代理回顾
查看>>
以操作系统方式执行和充当操作系统的一部分
查看>>
C#实现DNS解析服务
查看>>
zz: C++ 智能指针详解
查看>>
centos hadoop安装及配置
查看>>
js验证
查看>>