Notes for Netty 3.x with a simple NIO chat application

本文介绍了Netty框架的应用场景,包括聊天服务器、媒体流服务器等。深入探讨了其高级特性如Codec、SSL/TLS支持及WebSocket集成等,并解析了Netty的强大架构组成及其工作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值