What exactly is netty’s Sharable for

  netty

Order

Here is a summary of netty’s knowledge.

SimpleChannelInboundHandler

When an implementation of a ChannelInboundHandler overwrites the channelRead () method, it will be responsible for explicitly freeing the memory associated with the pooled ByteBuf instance. Netty provides a practical method for this, ReferenceCountUtil.release (), but managing resources in this way can be tedious.

A simpler way is to use SimpleChannelInboundHandler. Since SimpleChannelInboundHandler automatically releases resources, you should not store references to any messages for future use, because these references will become invalid.

Sharable

Note that a channel handler can be safely shared by multiple channels.

ChannelHandlerAdapter also provides a practical method isSharable (). If its corresponding implementation is marked Sharable, this method will return true, indicating that it can be added to multiple ChannelPipeline.

Because a ChannelHandler can be subordinate to multiple ChannelPipeline, it can also be bound to multiple ChannelHandlerContext instances. ChannelHandler for this use must be annotated with @Sharable annotation; Otherwise, attempting to add it to multiple ChannelPipeline will trigger an exception. Obviously, in order to be used safely for multiple concurrent Channels (i.e. connections), such ChannelHandler must be thread safe.

ChannelPipeline

ChannelPipeline is a chain of ChannelHandler instances that intercept inbound and outbound events flowing through a Channel.

Each newly created Channel will be assigned a new ChannelPipeline. This association is permanent; Channel can neither attach another ChannelPipeline nor separate its current. This is a fixed operation in the life cycle of the Netty component and does not require any intervention by developers. According to the origin of the event, the event will be handled by ChannelInboundHandler or ChannelOutbboundHandler. Subsequently, by calling the ChannelHandlerContext implementation, it will be forwarded to the next ChannelHandler of the same supertype.

ChannelPipeline addition order

For inbound events, add first and execute first. For outbound events, the ones added later are executed first.

ChannelPipeline event flow

A reference to the Channel will be obtained through ChannelHandlerContext. Calling the write () method on the Channel will cause write events to flow through the ChannelPipeline from tail to head:

ctx.getPipeline().write() 

In order to call the processing procedure starting from a specific ChannelHandler, the ChannnelHandlerContext associated with the ChannelHandler before the ChannelHandler must be obtained. This ChannelHandlerContext will call the ChannelHandler after its associated ChannelHandler:

ctx.write()

Messages will flow through the ChannelPipeline from the next ChannelHandler, bypassing all previous Channel Handlers.

ChannelInitializer

Netty provides an easy way to add multiple ChannelHandler to a ChannelPipeline. All you need to do is simply provide your ChannelInitializer implementation to the instance of Bootstrap or ServerBootstrap, and once the Channel is registered with its EventLoop, your initChannel () version will be called. After the method returns, the instance of ChannelInitializer will remove itself from the ChannelPipeline.

In most scenarios, if you don’t need to use a method that only exists on the SocketChannel, you can use ChannelInitializer, otherwise you can use ChannelInitializer, in which SocketChannel extends Channel. If your application uses multiple ChannelHandler, define your own ChannelInitializer implementation to install them into the ChannelPipeline.