ChannelHandlers which handles or intercepts inbound events and outbound operations of a
Channel. ChannelPipeline implements an advanced form of the
Intercepting Filter pattern
to give a user full control over how an event is handled and how the ChannelHandlers in a pipeline
interact with each other.
ChannelHandlers in a ChannelPipeline
typically. An I/O event is handled by a ChannelHandler (which may handle inbound or / and outbound events)
and be forwarded to its closest handler by calling the event propagation methods defined in
ChannelHandlerContext, such as ChannelHandlerContext#fireChannelRead(Object) and
ChannelHandlerContext#write(Object).
I/O Request
via Channel or
ChannelHandlerContext
|
+---------------------------------------------------+---------------+
| ChannelPipeline | |
| \|/ |
| +---------------------+ +-----------+----------+ |
| | Inbound Handler N | | Outbound Handler 1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler N-1 | | Outbound Handler 2 | |
| +----------+----------+ +-----------+----------+ |
| /|\ . |
| . . |
| ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
| [ method call] [method call] |
| . . |
| . \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 2 | | Outbound Handler M-1 | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
| | \|/ |
| +----------+----------+ +-----------+----------+ |
| | Inbound Handler 1 | | Outbound Handler M | |
| +----------+----------+ +-----------+----------+ |
| /|\ | |
+---------------+-----------------------------------+---------------+
| \|/
+---------------+-----------------------------------+---------------+
| | | |
| [ Socket.read() ] [ Socket.write() ] |
| |
| Netty Internal I/O Threads (Transport Implementation) |
+-------------------------------------------------------------------+
An inbound event is handled by the inbound handlers in the bottom-up direction as shown on the left side of the
diagram. An inbound handler usually handles the inbound data generated by the I/O thread on the bottom of the
diagram. The inbound data is often read from a remote peer via the actual input operation such as
SocketChannel#read(ByteBuffer). If an inbound event goes beyond the top inbound handler, it is discarded
silently, or logged if it needs your attention.
An outbound event is handled by the outbound handler in the top-down direction as shown on the right side of the
diagram. An outbound handler usually generates or transforms the outbound traffic such as write requests.
If an outbound event goes beyond the bottom outbound handler, it is handled by an I/O thread associated with the
Channel. The I/O thread often performs the actual output operation such as
SocketChannel#write(ByteBuffer).
For example, let us assume that we created the following pipeline:
ChannelPipeline p = ...;
p.addLast("1", new InboundHandlerA());
p.addLast("2", new InboundHandlerB());
p.addLast("3", new OutboundHandlerA());
p.addLast("4", new OutboundHandlerB());
p.addLast("5", new InboundOutboundHandlerX());
In the example above, the class whose name starts with Inbound means it is an inbound handler.
The class whose name starts with Outbound means it is a outbound handler.
In the given example configuration, the handler evaluation order is 1, 2, 3, 4, 5 when an event goes inbound.
When an event goes outbound, the order is 5, 4, 3, 2, 1. On top of this principle, ChannelPipeline skips
the evaluation of certain handlers to shorten the stack depth:
ChannelHandler,
and therefore the actual evaluation order of an inbound event will be: 1, 2, and 5.ChannelHandler, and therefore the actual evaluation
order of an outbound event will be: 5, 4, and 3.ChannelHandler, the evaluation order of
an inbound and a outbound event could be 125 and 543 respectively.ChannelHandlerContext to forward an event to its next handler. Those methods include:
ChannelHandlerContext#fireChannelRegistered()ChannelHandlerContext#fireChannelActive()ChannelHandlerContext#fireChannelRead(Object)ChannelHandlerContext#fireChannelReadComplete()ChannelHandlerContext#fireChannelExceptionCaught(Throwable)ChannelHandlerContext#fireChannelInboundEvent(Object)ChannelHandlerContext#fireChannelWritabilityChanged()ChannelHandlerContext#fireChannelInactive()ChannelHandlerContext#fireChannelShutdown(ChannelShutdownDirection)ChannelHandlerContext#fireChannelUnregistered()ChannelHandlerContext#bind(SocketAddress)ChannelHandlerContext#connect(SocketAddress, SocketAddress)ChannelHandlerContext#write(Object)ChannelHandlerContext#flush()ChannelOutboundInvoker#read(ReadBufferAllocator)ChannelHandlerContext#disconnect()ChannelHandlerContext#close()ChannelHandlerContext#shutdown(ChannelShutdownDirection)ChannelHandlerContext#sendOutboundEvent(Object)ChannelHandlerContext#deregister()public class MyInboundHandler implementsChannelHandler{@Overridepublic void channelActive(ChannelHandlerContextctx) { System.out.println("Connected!"); ctx.fireChannelActive(); } } public class MyOutboundHandler implementsChannelHandler{@Overridepublic Future<Void> close(ChannelHandlerContextctx) { System.out.println("Closing .."); return ctx.close(); } }
A user is supposed to have one or more ChannelHandlers in a pipeline to receive I/O events (e.g. read) and
to request I/O operations (e.g. write and close). For example, a typical server will have the following handlers
in each channel's pipeline, but your mileage may vary depending on the complexity and characteristics of the
protocol and business logic:
Buffer) into a Java object....ChannelPipelinepipeline = ch.pipeline(); pipeline.addLast("decoder", new MyProtocolDecoder()); pipeline.addLast("encoder", new MyProtocolEncoder()); // If your business logic does block or take a lot of time you should offload the work to an extra //java.to ensure you don't block theutil. concurrent. Executor EventLoop. pipeline.addLast("handler", new MyBusinessLogicHandler());
A ChannelHandler can be added or removed at any time because a ChannelPipeline is thread safe.
For example, you can insert an encryption handler when sensitive information is about to be exchanged, and remove it
after the exchange.
| Modifier and Type | Method and Description |
|---|---|
| public ChannelPipeline | addAfter(String
the name of the existing handler baseName, String the name of the handler to insert after name, ChannelHandler the handler to insert after handler)Inserts a |
| public ChannelPipeline | addBefore(String
the name of the existing handler baseName, String the name of the handler to insert before name, ChannelHandler the handler to insert before handler)Inserts a |
| public ChannelPipeline | addFirst(String
the name of the handler to insert first name, ChannelHandler the handler to insert first handler)Inserts a |
| public ChannelPipeline | addFirst(ChannelHandler...
the handlers to insert first handlers)Inserts |
| public ChannelPipeline | addLast(String
the name of the handler to append name, ChannelHandler the handler to append handler)Appends a |
| public ChannelPipeline | addLast(ChannelHandler...
the handlers to insert last handlers)Inserts |
| public Channel | |
| public ChannelHandlerContext | Returns: the context object of the specified handler.null if there's no such handler in this pipeline.Returns the context object of the specified |
| public ChannelHandlerContext | Returns: the context object of the handler with the specified name.null if there's no such handler in this pipeline.Returns the context object of the |
| public ChannelHandlerContext | Returns: the context object of the handler of the specified type.null if there's no such handler in this pipeline.Returns the context object of the |
| public ChannelPipeline | fireChannelActive()
Redeclares io. Channel is active now, which means it is connected.
|
| public ChannelPipeline | fireChannelExceptionCaught(Throwable cause)
Redeclares io. Channel received an Throwable in one of its inbound operations.
|
| public ChannelPipeline | fireChannelInactive()
Redeclares io. Channel is inactive now, which means it is closed.
|
| public ChannelPipeline | fireChannelInboundEvent(Object event)
Redeclares io. Channel received a custom defined inbound event.
|
| public ChannelPipeline | fireChannelRead(Object msg)
Redeclares io. Channel received a message.
|
| public ChannelPipeline | fireChannelReadComplete()
Redeclares io. ChannelHandler#channelReadComplete(ChannelHandlerContext)
event to the next ChannelHandler in the ChannelPipeline.
|
| public ChannelPipeline | fireChannelRegistered()
Redeclares io. Channel was registered to its EventLoop.
|
| public ChannelPipeline | fireChannelShutdown(ChannelShutdownDirection direction)
Redeclares io. Channel was shutdown in a specific direction.
|
| public ChannelPipeline | fireChannelUnregistered()
Redeclares io. Channel was unregistered from its EventLoop.
|
| public ChannelPipeline | fireChannelWritabilityChanged()
Redeclares io. ChannelHandler#channelWritabilityChanged(ChannelHandlerContext)
event to the next ChannelHandler in the ChannelPipeline.
|
| public default ChannelHandler | Returns: the first handler.null if this pipeline is empty.Returns the first |
| public ChannelHandlerContext | Returns: the context of the first handler.null if this pipeline is empty.Returns the context of the first |
| public ChannelPipeline | flush()
Redeclares io. |
| public ChannelHandler | Returns: the handler with the specified name.null if there's no such handler in this pipeline.Returns the |
| public <T extends ChannelHandler> T | Returns: the handler of the specified handler type.null if there's no such handler in this pipeline.Returns the |
| public default boolean | |
| public default ChannelHandler | Returns: the last handler.null if this pipeline is empty.Returns the last |
| public ChannelHandlerContext | Returns: the context of the last handler.null if this pipeline is empty.Returns the context of the last |
| public default List | |
| public long | Returns: the number of buffered / queued bytes.The number of the outbound bytes that are buffered / queued in this |
| public ChannelPipeline | read(ReadBufferAllocator
The readBufferAllocator)ReadBufferAllocator that should be used to allocate a Buffer
if needed (for reading the data).Redeclares io. Channel, triggers an
ChannelHandler#channelRead(ChannelHandlerContext, Object) event if data was
read, and triggers a
channelReadComplete event so the
handler can decide to continue reading.
|
| public ChannelPipeline | |
| public default ChannelPipeline | remove(ChannelHandler
the handler)ChannelHandler to removeRemoves the specified |
| public default ChannelHandler | Returns: the removed handlerthe name under which the name)ChannelHandler was stored.Removes the |
| public default < the type of the handler T extends ChannelHandler> T | Returns: the removed handlerthe type of the handler handlerType)Removes the |
| public ChannelHandler | |
| public <T extends ChannelHandler> T | Returns: the removed handlerthe name under which the name)ChannelHandler was stored.Removes the |
| public < the type of the handler T extends ChannelHandler> T | Returns: the removed handler ornull if it didn't exist.the type of the handler handlerType)Removes the |
| public <T extends ChannelHandler> T | Returns: the removed handler ornull if it didn't exist.the handler)ChannelHandler to removeRemoves the specified |
| public ChannelHandler | |
| public ChannelPipeline | Returns: itselfthe oldHandler, String ChannelHandler to be replacedthe name under which the replacement should be added newName, ChannelHandler the newHandler)ChannelHandler which is used as replacementReplaces the specified |
| public ChannelHandler | Returns: the removed handlerthe name of the oldName, String ChannelHandler to be replacedthe name under which the replacement should be added newName, ChannelHandler the newHandler)ChannelHandler which is used as replacementReplaces the |
| public <T extends ChannelHandler> T | Returns: the removed handlerthe type of the handler to be removed oldHandlerType, String the name under which the replacement should be added newName, ChannelHandler the newHandler)ChannelHandler which is used as replacementReplaces the |
| public Map |
| addAfter | back to summary |
|---|---|
| public ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler) Inserts a
| |
| addBefore | back to summary |
|---|---|
| public ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler) Inserts a
| |
| addFirst | back to summary |
|---|---|
| public ChannelPipeline addFirst(String name, ChannelHandler handler) Inserts a
| |
| addFirst | back to summary |
|---|---|
| public ChannelPipeline addFirst(ChannelHandler... handlers) Inserts
| |
| addLast | back to summary |
|---|---|
| public ChannelPipeline addLast(String name, ChannelHandler handler) Appends a
| |
| addLast | back to summary |
|---|---|
| public ChannelPipeline addLast(ChannelHandler... handlers) Inserts
| |
| channel | back to summary |
|---|---|
| public Channel channel() Returns the
| |
| context | back to summary |
|---|---|
| public ChannelHandlerContext context(ChannelHandler handler) Returns the context object of the specified
| |
| context | back to summary |
|---|---|
| public ChannelHandlerContext context(String name) Returns the context object of the
| |
| context | back to summary |
|---|---|
| public ChannelHandlerContext context(Class<? extends ChannelHandler> handlerType) Returns the context object of the
| |
| fireChannelActive | back to summary |
|---|---|
| public ChannelPipeline fireChannelActive() Redeclares io. Doc from io. A
| |
| fireChannelExceptionCaught | back to summary |
|---|---|
| public ChannelPipeline fireChannelExceptionCaught(Throwable cause) Redeclares io. Doc from io. A
| |
| fireChannelInactive | back to summary |
|---|---|
| public ChannelPipeline fireChannelInactive() Redeclares io. Doc from io. A
| |
| fireChannelInboundEvent | back to summary |
|---|---|
| public ChannelPipeline fireChannelInboundEvent(Object event) Redeclares io. Doc from io. A
| |
| fireChannelRead | back to summary |
|---|---|
| public ChannelPipeline fireChannelRead(Object msg) Redeclares io. Doc from io. A
| |
| fireChannelReadComplete | back to summary |
|---|---|
| public ChannelPipeline fireChannelReadComplete() Redeclares io. Doc from io. Triggers an
| |
| fireChannelRegistered | back to summary |
|---|---|
| public ChannelPipeline fireChannelRegistered() Redeclares io. Doc from io. A
| |
| fireChannelShutdown | back to summary |
|---|---|
| public ChannelPipeline fireChannelShutdown(ChannelShutdownDirection direction) Redeclares io. Doc from io. A
| |
| fireChannelUnregistered | back to summary |
|---|---|
| public ChannelPipeline fireChannelUnregistered() Redeclares io. Doc from io. A
| |
| fireChannelWritabilityChanged | back to summary |
|---|---|
| public ChannelPipeline fireChannelWritabilityChanged() Redeclares io. Doc from io. Triggers an
| |
| first | back to summary |
|---|---|
| public default ChannelHandler first() Returns the first
| |
| firstContext | back to summary |
|---|---|
| public ChannelHandlerContext firstContext() Returns the context of the first
| |
| flush | back to summary |
|---|---|
| public ChannelPipeline flush() Redeclares io. Doc from io. Request to flush all pending messages via this ChannelOutboundInvoker.
| |
| get | back to summary |
|---|---|
| public ChannelHandler get(String name) Returns the
| |
| get | back to summary |
|---|---|
| public <T extends ChannelHandler> T get(Class<T> handlerType) Returns the
| |
| isEmpty | back to summary |
|---|---|
| public default boolean isEmpty() Returns | |
| last | back to summary |
|---|---|
| public default ChannelHandler last() Returns the last
| |
| lastContext | back to summary |
|---|---|
| public ChannelHandlerContext lastContext() Returns the context of the last
| |
| names | back to summary |
|---|---|
| public default List Returns the | |
| pendingOutboundBytes | back to summary |
|---|---|
| public long pendingOutboundBytes() The number of the outbound bytes that are buffered / queued in this
| |
| read | back to summary |
|---|---|
| public ChannelPipeline read(ReadBufferAllocator readBufferAllocator) Redeclares io. Doc from io. Request to read data from the
This will result in having the
| |
| read | back to summary |
|---|---|
| public ChannelPipeline read() Redeclares io.
| |
| remove | back to summary |
|---|---|
| public default ChannelPipeline remove(ChannelHandler handler) Removes the specified
| |
| remove | back to summary |
|---|---|
| public default ChannelHandler remove(String name) Removes the
| |
| remove | back to summary |
|---|---|
| public default <T extends ChannelHandler> T remove(Class<T> handlerType) Removes the
| |
| removeFirst | back to summary |
|---|---|
| public ChannelHandler removeFirst() Removes the first
| |
| removeIfExists | back to summary |
|---|---|
| public <T extends ChannelHandler> T removeIfExists(String name) Removes the
| |
| removeIfExists | back to summary |
|---|---|
| public <T extends ChannelHandler> T removeIfExists(Class<T> handlerType) Removes the
| |
| removeIfExists | back to summary |
|---|---|
| public <T extends ChannelHandler> T removeIfExists(ChannelHandler handler) Removes the specified
| |
| removeLast | back to summary |
|---|---|
| public ChannelHandler removeLast() Removes the last
| |
| replace | back to summary |
|---|---|
| public ChannelPipeline replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler) Replaces the specified
| |
| replace | back to summary |
|---|---|
| public ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler) Replaces the
| |
| replace | back to summary |
|---|---|
| public <T extends ChannelHandler> T replace(Class<T> oldHandlerType, String newName, ChannelHandler newHandler) Replaces the
| |
| toMap | back to summary |
|---|---|
| public Map Converts this pipeline into an ordered | |