Netty ChannelPipeline¶
Netty ChannelPipeline is a container of [[ChannelHandlers]] that handles or intercepts I/O events for a channel. It acts as an interceptor chain that processes events, such as incoming data or outgoing write requests, in a defined order.
Core Architecture¶
A ChannelPipeline is automatically created when a new channel is created^[600-developer__big-data__netty__netty-ChannelPipeline.md]. Each channel possesses its own unique pipeline instance^[600-developer__big-data__netty__netty-ChannelPipeline.md].
The pipeline maintains a list of handlers and propagates events through them. The flow is typically divided into: * Inbound handlers: Processing events like reading data (moving "down" the pipeline contextually, though often starting from the head). * Outbound handlers: Processing events like writing data (moving "up" the pipeline contextually).
Handler Management¶
The pipeline allows for dynamic modification of the handler chain. Handlers can be added to or removed from the pipeline at any time^[600-developer-big-data-netty-netty-channelpipeline.md].
A ChannelPipeline holds references to its ChannelHandlers, allowing for the interaction and state management associated with specific handlers^[600-developer-big-data-netty-netty-channelpipeline.md].
Execution Order¶
The order in which handlers are executed depends on their type and the direction of the I/O event.
Inbound Events¶
For an inbound event (e.g., data read), the execution starts from the beginning of the pipeline and proceeds forward. Based on the example code: * Handler Setup: Inbound (1), Inbound (2), Outbound (3), Outbound (4), Mixed (5). * Execution Order: 1 → 2 → 5^[600-developer__big-data__netty__netty-ChannelPipeline.md]. * Note: Outbound handlers (3, 4) are ignored during inbound processing unless they implement both interfaces.
Outbound Events¶
For an outbound event (e.g., data write), the execution starts from the end of the pipeline and proceeds backward. * Execution Order: 5 → 4 → 3^[600-developer__big-data__netty__netty-ChannelPipeline.md]. * Note: Inbound-only handlers (1, 2) are ignored during outbound processing.
Handlers that implement both inbound and outbound interfaces (like Handler 5 in the example) can participate in both directions.
Blocking Operations and Thread Safety¶
To prevent blocking the I/O threads during long-running tasks (such as database interactions or complex calculations), specific handlers can be executed in a separate thread group.
This is achieved by providing an EventExecutorGroup when adding the handler to the pipeline^[600-developer-big-data-netty-netty-channelpipeline.md]:
EventExecutorGroup group = new DefaultEventExecutorGroup(16);
pipeline.addLast(group, "handler", new MyBusinessLogicHandler());
This ensures that MyBusinessLogicHandler executes in the thread pool group rather than the Netty I/O transport thread.
Sources¶
^[600-developer-big-data-netty-netty-channelpipeline.md]