Netty学习笔记一

Handler模块:

ChannelHandler

{@link ChannelHandler} itself does not provide any method.  To handle a
 * {@link ChannelEvent} you need to implement its sub-interfaces.  There are
 * two sub-interfaces which handles a received event, one for upstream events
 * and the other for downstream events

不提供任何方法,在应用中,要根据需要选择实现它的子接口:ChannelUpstreamHandler或者ChannelDownstreamHandler,或者两者的合成接口--SimpleChannelHandler(继承了ChannelUpstreamHandler接口和ChannelDownstreamHandler接口)

ChannelUpstreamHandler :【 extends ChannelHandler】

Handles or intercepts an upstream {@link ChannelEvent}, and sends a
 {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
The most common use case of this interface is to intercept an I/O event
generated by I/O workers to transform the received messages or execute
the relevant business logic.

ChannelDownstreamHandler:【 extends ChannelHandler】

 Handles or intercepts a downstream {@link ChannelEvent}, and sends a
 {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
The most common use case of this interface is to intercept an I/O request
such as {@link Channel#write(Object)} and {@link Channel#close()}.

SimpleChannelHandler:【 implements ChannelUpstreamHandler, ChannelDownstreamHandler】

A {@link ChannelHandler} which provides an individual handler method
 * for each event type.  This handler down-casts the received upstream or
 * or downstream event into more meaningful sub-type event and calls an
 * appropriate handler method with the down-cast event.


SimpleChannelHandler提供了足够多的网络事件响应方法:

handleUpstream

messageReceived

exceptionCaught

channelOpen

channelBound

channelConnected

channelInterestChanged

channelDisconnected

channelUnbound

channelClosed

writeComplete

childChannelOpen

childChannelClosed

handleDownstream

writeRequested

bindRequested

connectRequested

setInterestOpsRequested

disconnectRequested

unbindRequested

closeRequested

大多数情况下,我们开发过程中要创建自己的ChannelHandler,一般实现自SimpleChannelHandler接口,覆盖里面相应的方法,或者创建自己的方法即可。

你可能感兴趣的:(Netty学习笔记一)