2013-07-30 35 views
0

我開始與多個業務線程netty4 NIO服務器用於處理長期業務 像下面的Netty 4多線程DefaultEventExecutorGroup

public void start(int listenPort, final ExecutorService ignore) 
     throws Exception { 
      ... 
    bossGroup = new NioEventLoopGroup(); 
    ioGroup = new NioEventLoopGroup(); 
    businessGroup = new DefaultEventExecutorGroup(businessThreads); 

    ServerBootstrap b = new ServerBootstrap(); 
    b.group(bossGroup, ioGroup).channel(NioServerSocketChannel.class) 
      .childOption(ChannelOption.TCP_NODELAY, 
         Boolean.parseBoolean(System.getProperty(
           "nfs.rpc.tcp.nodelay", "true"))) 
      .childOption(ChannelOption.SO_REUSEADDR, 
         Boolean.parseBoolean(System.getProperty(
           "nfs.rpc.tcp.reuseaddress", "true"))) 
      .childHandler(new ChannelInitializer<SocketChannel>() { 
       @Override 
       public void initChannel(SocketChannel ch) 
         throws Exception { 
        ch.pipeline().addLast("decoder", 
          new Netty4ProtocolDecoder()); 
        ch.pipeline().addLast("encoder", 
          new Netty4ProtocolEncoder()); 
        ch.pipeline().addLast(businessGroup, "handler", 
          new Netty4ServerHandler()); 
       } 
      }); 

    b.bind(listenPort).sync(); 
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads); 
} 

我發現,只有一個線程在服務器接受一個連接工作。 如何引導只能爲一個連接啓動多個業務線程的服務器?

感謝, 敏思

回答

2

Netty中總是會使用同一個線程的一個連接。這是設計。如果你想改變它,你可以實現一個自定義的EventExecutorGroup,並在你的ChannelHandler添加到ChannelPipeline時傳遞它。

請注意,這可能會導致數據包亂序。