Netty源碼分析之ChannelEvent

ChannelEvent是和這個Channel相關(guān)的IO事件和請求渗勘,會由各個ChannelHandler來處理。
事件分為上行(Upstream)和下行(Downstream)兩種。
當(dāng)服務(wù)器從客戶端收到一個消息豹缀,那么與之相關(guān)的就是一個上行事件(Upstream Event)藤巢,流水線(Pipeline)中的UpstreamChannelHandler會處理它;如果服務(wù)器要響應(yīng)這個客戶端撵溃,那么與響應(yīng)消息對應(yīng)的就是下行事件(Downstream Event)疚鲤,流水線(Pipeline)中的DownstreamChannelHandler會處理它。

org.jboss.netty.channel.ChannelEvent源碼:

public interface ChannelEvent {

    /**
     * Returns the {@link Channel} which is associated with this event.
     */
    Channel getChannel();

    /**
     * Returns the {@link ChannelFuture} which is associated with this event.
     * If this event is an upstream event, this method will always return a
     * {@link SucceededChannelFuture} because the event has occurred already.
     * If this event is a downstream event (i.e. I/O request), the returned
     * future will be notified when the I/O request succeeds or fails.
     */
    ChannelFuture getFuture();
}

An I/O event or I/O request associated with a Channel.

A ChannelEvent is handled by a series of ChannelHandlers in a ChannelPipeline.

Upstream events and downstream events, and their interpretation
Every event is either an upstream event or a downstream event. If an event flows forward from the first handler to the last handler in a ChannelPipeline, we call it an upstream event and say "an event goes upstream." If an event flows backward from the last handler to the first handler in a ChannelPipeline, we call it a downstream event and say "an event goes downstream." (Please refer to the diagram in ChannelPipeline for more explanation.)

When your server receives a message from a client, the event associated with the received message is an upstream event. When your server sends a message or reply to the client, the event associated with the write request is a downstream event. The same rule applies for the client side. If your client sent a request to the server, it means your client triggered a downstream event. If your client received a response from the server, it means your client will be notified with an upstream event. Upstream events are often the result of inbound operations such as InputStream.read(byte[]), and downstream events are the request for outbound operations such as OutputStream.write(byte[]), Socket.connect(SocketAddress), and Socket.close().

Upstream events

Event name Event type and condition Meaning
"messageReceived" MessageEvent a message object (e.g. ChannelBuffer) was received from a remote peer
"exceptionCaught" ExceptionEvent an exception was raised by an I/O thread or a ChannelHandler
"channelOpen" ChannelStateEvent(state = OPEN, value = true) a Channel is open, but not bound nor connected
"channelClosed" ChannelStateEvent(state = OPEN, value = false) a Channel was closed and all its related resources were released
"channelBound" ChannelStateEvent(state = BOUND, value = SocketAddress) a Channel is open and bound to a local address, but not connected
"channelUnbound" ChannelStateEvent(state = BOUND, value = null) a Channel was unbound from the current local address
"channelConnected" ChannelStateEvent(state = CONNECTED, value = SocketAddress) a Channel is open, bound to a local address, and connected to a remote address
"writeComplete" WriteCompletionEvent something has been written to a remote peer
"channelDisconnected" ChannelStateEvent(state = CONNECTED, value = null) a Channel was disconnected from its remote peer
"channelInterestChanged" ChannelStateEvent(state = INTEREST_OPS, no value) a Channel's interestOps was changed

These two additional event types are used only for a parent channel which can have a child channel (e.g. ServerSocketChannel).

Event name Event type and condition Meaning
"childChannelOpen" ChildChannelStateEvent(childChannel.isOpen() = true) a child Channel was open (e.g. a server channel accepted a connection.)
"childChannelClosed" ChildChannelStateEvent(childChannel.isOpen() = false) a child Channel was closed (e.g. the accepted connection was closed.)

Downstream events

Event name Event type and condition Meaning
"write" MessageEvent Send a message to the Channel.
"bind" ChannelStateEvent(state = BOUND, value = SocketAddress) Bind the Channel to the specified local address.
"unbind" ChannelStateEvent(state = BOUND, value = null) Unbind the Channel from the current local address.
"connect" ChannelStateEvent(state = CONNECTED, value = SocketAddress) Connect the Channel to the specified remote address.
"disconnect" ChannelStateEvent(state = CONNECTED, value = null) Disconnect the Channel from the current remote address.
"close" ChannelStateEvent(state = OPEN, value = false) Close the Channel.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末缘挑,一起剝皮案震驚了整個濱河市集歇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贯卦,死亡現(xiàn)場離奇詭異内狗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)砾隅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人纺荧,你說我怎么就攤上這事÷睿” “怎么了宙暇?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長议泵。 經(jīng)常有香客問我占贫,道長,這世上最難降的妖魔是什么肢簿? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任靶剑,我火速辦了婚禮,結(jié)果婚禮上池充,老公的妹妹穿的比我還像新娘桩引。我一直安慰自己,他們只是感情好收夸,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布坑匠。 她就那樣靜靜地躺著,像睡著了一般卧惜。 火紅的嫁衣襯著肌膚如雪厘灼。 梳的紋絲不亂的頭發(fā)上夹纫,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天,我揣著相機(jī)與錄音设凹,去河邊找鬼舰讹。 笑死,一個胖子當(dāng)著我的面吹牛闪朱,可吹牛的內(nèi)容都是我干的月匣。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼奋姿,長吁一口氣:“原來是場噩夢啊……” “哼锄开!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起称诗,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤萍悴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后寓免,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體癣诱,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年再榄,在試婚紗的時候發(fā)現(xiàn)自己被綠了狡刘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡困鸥,死狀恐怖嗅蔬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情疾就,我是刑警寧澤澜术,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站猬腰,受9級特大地震影響鸟废,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜姑荷,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一盒延、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鼠冕,春花似錦添寺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春票罐,著一層夾襖步出監(jiān)牢的瞬間叉趣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工该押, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留疗杉,地道東北人。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓蚕礼,卻偏偏與公主長得像乡数,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子闻牡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,493評論 5 6
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,874評論 0 0
  • 2月14日情人節(jié)罩润。我會想起那句“愿天下所有的情侶都是失散多年的兄妹”,也會想起有人唱“其實愛對了人翼馆,情人節(jié)每天都過...
    慕容小妖閱讀 607評論 0 0
  • 今晚的咨詢就像溪水一樣平靜割以。 我安靜的說,你安靜的聽应媚。 卻又覺得淡淡的悲傷严沥。我好像無法改變什么。 咨詢馬上要結(jié)束了...
    綠籮小姐閱讀 162評論 0 0
  • 原本是打算畫可愛的貓咪中姜,怎奈沒有光滑的石頭消玄,就抱著僥幸的心理,想著試一試丢胚,萬一可以呢翩瓜,結(jié)果涂過底色之后,用鉛筆描形...
    韓小冰閱讀 3,917評論 10 15