study link: http://netty.io/3.6/guide/#architecture
应用场景:
-
Chat server that requires persistent connections and server push technology (e.g. Comet)
-
Media streaming server that needs to keep the connection open until the whole media is streamed (e.g. 2 hours of video)
-
File server that allows the uploading of large files without memory pressure (e.g. uploading 1GB per request)
-
Scalable mash-up client that connects to tens of thousands of 3rd party web services asynchronously
高级特性:
1. Codec (means encode and decode - usually you need to codec the requests from socket stream)
2. SSL (secure socket layer) / TLS (transport layer security) support
3. HTTP easy implementation
4. webSocket support
5. Google Protocol Buffer Integration - binary protocol ?!
powerful architecture. It is composed of three components - buffer, channel, and event model- and all advanced features are built on top of the three core components
Guide:
Request will be read by Channel into ChannelBuffer
Meanwhile it will publish the ChannelEvent to the ChannelHandlers in the ChannelPipeline
ChannelEvent.getChannel()
Response will be generated by ChannelHandlers into ChannelBuffer, lastly write into Channel back to client
Netty channel state event
open -> bound (to port) -> connect (to port)
disconnect (on port) -> unbound (on port) -> closed
sample logs:
INFO: [id: 0x7c224ac7, /127.0.0.1:61176 => /127.0.0.1:8443] OPEN
Sep 09, 2013 10:35:40 AM com.pc.liaotian.nio.server.handler.LianTianServerHanlder handleUpstream
INFO: [id: 0x7c224ac7, /127.0.0.1:61176 => /127.0.0.1:8443] BOUND: /127.0.0.1:8443
Sep 09, 2013 10:35:40 AM com.pc.liaotian.nio.server.handler.LianTianServerHanlder handleUpstream
INFO: [id: 0x7c224ac7, /127.0.0.1:61176 => /127.0.0.1:8443] CONNECTED: /127.0.0.1:61176
Sep 09, 2013 10:41:45 AM com.pc.liaotian.nio.server.handler.LianTianServerHanlder handleUpstream
INFO: [id: 0x4a841d0f, /127.0.0.1:61136 :> /127.0.0.1:8443] DISCONNECTED
Sep 09, 2013 10:41:45 AM com.pc.liaotian.nio.server.handler.LianTianServerHanlder handleUpstream
INFO: [id: 0x4a841d0f, /127.0.0.1:61136 :> /127.0.0.1:8443] UNBOUND
Sep 09, 2013 10:41:45 AM com.pc.liaotian.nio.server.handler.LianTianServerHanlder handleUpstream
INFO: [id: 0x4a841d0f, /127.0.0.1:61136 :> /127.0.0.1:8443] CLOSED
=======SSLContext===========================
Netty 3.x NIO program flow
===========================================
@Server side
1. ServerBootStrap [bind a port]
2. NioServerSocketChannelFactory(boss threads, worker threads) * Executors.newCachedThreadPool(),
3. Your channelPipelineFactory * implements ChannelPipelineFactory
4. Add your/provided channelHandler to ChannelPipeline, generally you will have below handlers (same on the client side):
-"ssl" - SslHandler - if you apply SSL
-"framer" (it use to frame the protocol(e.g. TCP/IP) data packets),
-"decoder" (make channelBuffer(generally bytes) -> string(or sth. else)),
-"encoder" (make string -> channelBuffer)
-"handler" your business handler (implement business logic here)
@Client side
1. ClientBootStrap [connect(host, port)]
2. NioClientSocketChannelFactory(boss threads, worker threads)
3. Your clientChannelPipelineFactory
4. Add your/provided channelHandler to ChannelPipeline
5. ChannelFuture (derivate by connect) [isSuccess() -> GO biz] *** awaitUninterruptibly() - wait until I/O operation (connect,read, write,disconnect...) completed
-get the channel for Read/Write:
channelFuture.awaitUninterruptibly().getChannel();
6. nio selector on the connected channel - listen...
7. break, release, close
channel.close().awaitUninterruptibly();//wait for channel close operation to complete
clientBootstrap.releaseExternalResources();//wait for external rsc to be released
自己空闲时间做的简陋聊天程序:
http://download.youkuaiyun.com/download/paulmin/6321073
==Other Info==
* new NioServerSocketChannelFactory( Executors.newCachedThreadPool()
无界线程池,可以进行自动线程回收。
在JDK帮助文档中,有如此一段话: “强烈建议程序员使用较为方便的 Executors 工厂方法 Executors.newCachedThreadPool()(无界线程池,可以进行自动线程回收)、Executors.newFixedThreadPool(int)(固定大小线程池)和 Executors.newSingleThreadExecutor()(单个后台线程),它们均为大多数使用场景预定义了设置。”
More details about ThreadPool tech, you can check http://dongxuan.iteye.com/blog/901689
newFixedThreadPool newSingleThreadExecutor newCachedThreadPool