SimpleChannelInboundHandler channelInactive userEventTriggered channelRead0
ChannelInitializer
SpringBeanFactory
@Component @PostConstruct @PreDestroy
IdleStateHandler
HeartbeatDecoder ByteToMessageDecoder
@Configuration
ChannelRead channelRead0 messageReceived
org.jboss.netty.bootstrap.; First, in Netty 3.X the packages were from org.jboss.netty. io.netty.bootstrap.ServerBootstrap; But started Netty 4.X the packeages are from io.netty.*
Now, if you are using Netty 4.X to read message use the method
ChannelRead(ChannelHandleContext ctx, Object msg) { ... } inherited from ChannelInboundHandlerAdapter class. The method:
messageReceived(ChannelHandlerContext, I) { ...} was used in Netty 3.X version.
ByteBuf ByteBuf buf = Unpooled.buffer();
池化buffer Netty4引入了一种高效缓存池(buffer pool),它是结合了 buddy allocation 以及 slab allocation 的 jemalloc 的变体。
减小buffer的频繁分配及回收导致的GC压力 减少新建buffer时0值填充产生的内存带宽消耗 及时回收直接内存(direct buffers) 为了可以利用这些特性,用户都应该使用 ByteBufAllocator 获取buffer,除非你希望使用非池化buffer:
Channel channel = ...; ByteBufAllocator alloc = channel.alloc(); ByteBuf buf = alloc.buffer(512);
ChannelInboundHandlerAdapter channelRead (ChannelHandlerContext ctx, Object msg) channelReadComplete(ChannelHandlerContext ctx) exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
Netty的核心组件 在本节中我将要讨论Netty的主要构件块: Channel; 回调; Future; 事件和ChannelHandler。 ChannelFutureListener ChannelFuture