Netty handler execution order¶
The execution order of handlers in Netty is strictly managed by the ChannelPipeline, which intercepts and handles I/O events for a specific [[Channel]].^[600-developer__big-data__netty__netty-ChannelPipeline.md]
Pipeline Structure¶
Each Channel possesses its own unique pipeline, which is automatically created upon the channel's creation.^[600-developer__big-data__netty__netty-ChannelPipeline.md] This pipeline maintains a list of channel handlers, where handlers can be added or removed dynamically at runtime.^[600-developer__big-data__netty__netty-ChannelPipeline.md]
The pipeline typically holds a chain of handlers, categorized by the direction of the event flow: Inbound Handlers (processing data read from the socket) and Outbound Handlers (processing data written to the socket).^[600-developer__big-data__netty__netty-ChannelPipeline.md]
Inbound Event Order¶
Inbound events, such as receiving data, travel through the pipeline starting from the beginning (head) of the handler chain.^[600-developer__big-data__netty__netty-ChannelPipeline.md]
If handlers are added in the sequence 1, 2, 3, 4, 5, an inbound event will trigger handlers in ascending numerical order: 1, 2, then 5 (assuming 1, 2, and 5 are handlers implementing the inbound interface).^[600-developer__big-data__netty__netty-ChannelPipeline.md] This corresponds to the flow from Socket.read() up through Inbound Handler 1 to Inbound Handler N.^[600-developer__big-data__netty__netty-ChannelPipeline.md]
Outbound Event Order¶
Outbound events, such as writing data, travel through the pipeline in reverse, starting from the end (tail) of the handler chain towards the head.^[600-developer__big-data__netty__netty-ChannelPipeline.md]
Using the same sequence of handlers 1, 2, 3, 4, 5, an outbound event will trigger handlers in descending numerical order: 5, 4, then 3 (assuming 5, 4, and 3 implement the outbound interface).^[600-developer__big-data__netty__netty-ChannelPipeline.md] This corresponds to the flow from Outbound Handler M down to Socket.write().^[600-developer__big-data__netty__netty-ChannelPipeline.md]
Related Concepts¶
- [[Netty]]
- [[Event Loop]]
- [[ChannelHandler]]
Sources¶
^[600-developer__big-data__netty__netty-ChannelPipeline.md]