我有一些NETTY的問題。Netty只處理一個線程中的消息
幾年前我用smartfox服務器1.6x,而且它的多線程模型讓我能夠在不同的線程中爲我的連接處理消息。並且該功能迫使我編寫一些線程同步代碼來讓所有消息都能正確處理。
現在我使用netty,因爲它對於我的目標來說是非常有用的產品。
所以,我實現了一些基於服務器的邏輯。我的測試是以下
- 服務器消息隊列增加,處理每封郵件和當前線程休眠3秒鐘
- 客戶端修建垃圾每秒
大約10封郵件,我看到了之前第一消息(前十)不被處理(通常約30秒的時間),第二包消息沒有在並行線程中處理。
所以主要問題是:
是否網狀處理中只有一個線程一個連接的所有郵件? 如果是這樣,我應該實現一些額外的代碼在不同的線程中做到這一點,或者單線程單連接模型運行良好嗎?
Netty的配置:
_boss = new NioEventLoopGroup(1);
_worker = new NioEventLoopGroup(8);
_bootstrap = new ServerBootstrap();
_bootstrap.group(_boss, _worker)
.channel(NioServerSocketChannel.class)
.childHandler(new NettyChannelInitializer())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
_bootstrap.bind(_port).sync();
NettyChannelInitializer:
private static final ExtentionHandler SharedExtentionHandler = new ExtentionHandler();
private static final EventExecutorGroup ExtentionEventThreadPool = new DefaultEventExecutorGroup(20);
public void initChannel(SocketChannel channel) throws Exception
{
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(ExtentionEventThreadPool, "extention", SharedExtentionHandler);
}
Netty利用線程重用,因爲創建線程代價高昂。這是使其更快速的原因之一 – mangusta