Nettyを調べた
基本
何をやるものか
High Performance Non-Blocking Socket Server
を作れる
別の言葉で言うと
NIO client server framework
依存
なし! jdkのみ!
人気度
レイヤーが異なるが,高速コンカレントフレームワークとして考えた場合scala akkaでも似たようなことは可能なので
でGoogl Trend
で比較すると
傾向としては2013年ごろから
地域別だと
なぜか
- nettyは中国,韓国
- akkaはアメリカ,イギリス,インド
と結構場所依存性の人気度があるようだ.確かにnetty関連でググると中国語のページが引っかかる.なぜだ?
Users
抜粋
Apple, Airbnb, Google, Facebook, Instagram Netflix, Pivotal, Spotify, Square, Twitter, Yahoo, Zynga, Apach Spark, Neo4j, Elasticsearch
結構なビッグネームが使っているな.
New and noteworthy in 5.0
ChannelInboundHandlerAdapter, ChannelOutboundHandlerAdapter, and ChannelDuplexHandlerAdapter have been deprecated and replaced by ChannelHandlerAdapter.
4.0のサンプルにでてきていたChannelInboundHandlerAdapterはなんだったのか...
Install
ここから好きなbuild toolに対応したものをコピペして良きにはからう
大事なことの引用
ByteBuf is a reference-counted object which has to be released explicitly via the release() method. Please keep in mind that it is the handler's responsibility to release any reference-counted object passed to the handler.
ByteBuf has two pointers; one for read operations and the other for write operations. The writer index increases when you write something to a ByteBuf while the reader index does not change. The reader index and the writer index represents where the message starts and ends respectively.
A ChannelFuture represents an I/O operation which has not yet occurred.
you need to call the close() method after the ChannelFuture is complete, which was returned by the write() method, and it notifies its listeners when the write operation has been done.
The biggest and only difference between a server and a client in Netty is that different Bootstrap and Channel implementations are used.
Unfortunately, the buffer of a stream-based transport is not a queue of packets but a queue of bytes. It means, even if you sent two messages as two independent packets, an operating system will not treat them as two messages but as just a bunch of bytes. Therefore, there is no guarantee that what you read is exactly what your remote peer wrote
- さらなるsample
io.netty.example.factorial for a binary protocol, and
io.netty.example.telnet for a text line-based protocol.
- refernce count object
The general rule of thumb is that the party who accesses a reference-counted object lastly is responsible for the destruction of the reference-counted object.
全体まとめ
- ServerBootstrapは関数型言語ぽく,メソッドがオブジェクト自身を返してそこに対してまたメソッドを呼ぶみたいなメソッドチェインな感じ
- ctx (context)
- channelを持っている.
- いろいろ持っている.see AbstractChannelHandlerContext
- Bootstrap (cilent), BootstrapServer (server)
- fragmentが発生している可能性が高いので,defrag処理を忘れないこと
- channel
- pipelineを持っている
- pipelineに順にhandlerを追加していくのが一般的な感じ
- handler
- 1クラスで1つのロジックを担当するのが設計上いいと思われる
- downstream/upstream
- downstream (node to network) = outbound
- upstream (network to node) = inbound
- pipeline
- downstream/upstreamも同じpipelineを使う
- downstream: pipelineの後ろから処理される (と思われる)
- upstream: pipelineの前から処理される (と思われる)
- io.netty.buffer.ByteBuf
- 綺麗なascii artが書いてあるのでdocを読んだほうがいい
全体所感
- 多言語間通信をしたいとなるとthriftやprotocol-bufferが上位の選択肢になりそう
- Getting Startedだけだと全体像は何となくわからないところまでは理解できるが,簡単な使い方は理解可能,本を見ないとわからないかも.
- 人気度,人気地域,使い方,将来性などを考慮すると,NettyよりはAkkaを勉強すべしと思った
- Lightweight言語やFrameworkばっかりやっていたゆとり世代には若干きつい...
参考
- http://netty.io/
- http://netty.io/wiki/related-articles.html
- http://netty.io/wiki/
- http://netty.io/wiki/user-guide-for-4.x.html
- http://d.hatena.ne.jp/fatrow/20110208/netty
- http://netty.io/wiki/all-documents.html
- http://netty.io/wiki/new-and-noteworthy-in-5.0.html
- http://netty.io/wiki/reference-counted-objects.html
- http://netty.io/wiki/thread-model.html