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接口,覆盖里面相应的方法,或者创建自己的方法即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值