2015-02-05 1 views
0

Netty Client를 구현하여 응용 프로그램 스레드를 저장할 수 있는지 알고 싶습니다.I/O 클라이언트를 통한 Netty 클라이언트의 이점은 무엇입니까?

데모 클라이언트를 작성했습니다. 아래 코드를 찾으십시오. 단일 스레드가 다른 포트에 연결하여 효율적으로 처리 할 수 ​​있다고 예상했지만 잘못되었습니다. Netty는 스레드 연결마다 만듭니다.

public class NettyClient { 
    public static void main(String[] args) { 
    Runnable runA = new Runnable() { 
     public void run() { 
      Connect(5544); 
     } 
    }; 

    Thread threadA = new Thread(runA, "threadA"); 
    threadA.start(); 

    try { 
     Thread.sleep(1000); 
    } catch (InterruptedException x) { 
    } 

    Runnable runB = new Runnable() { 
     public void run() { 
      Connect(5544); 
     } 
    }; 

    Thread threadB = new Thread(runB, "threadB"); 
    threadB.start(); 

} 

static ClientBootstrap bootstrap = null; 
static NettyClient ins = new NettyClient(); 
public NettyClient() { 

    bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); 
    /* 
    * ClientBootstrap A helper class which creates a new client-side 
    * Channel and makes a connection attempt. 
    * 
    * NioClientSocketChannelFactory A ClientSocketChannelFactory which 
    * creates a client-side NIO-based SocketChannel. It utilizes the 
    * non-blocking I/O mode which was introduced with NIO to serve many 
    * number of concurrent connections efficiently 
    * 
    * There are two types of threads :Boss thread Worker threads Boss 
    * Thread passes control to worker thread. 
    */ 
    // Configure the client. 

    ChannelGroup channelGroup = new DefaultChannelGroup(NettyClient.class.getName()); 
    // Only 1 thread configured but still aceepts threadA and Thread B 
    // connection 
    OrderedMemoryAwareThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor(
      1, 1048576, 1073741824, 1, TimeUnit.MILLISECONDS, 
      new NioDataSizeEstimator(), new NioThreadFactory("NioPipeline")); 

    bootstrap.setPipelineFactory(new NioCommPipelineFactory(channelGroup, 
      pipelineExecutor)); 

    // bootstrap.setPipelineFactory(new 
    // BackfillClientSocketChannelFactory()); 
    bootstrap.setOption("child.tcpNoDelay", true); 

    bootstrap.setOption("child.keepAlive", true); 
    bootstrap.setOption("child.reuseAddress", true); 
    bootstrap.setOption("readWriteFair", true); 
} 

public static NettyClient getins() { 
    return ins; 
} 

public static void Connect(int port) { 
    ChannelFuture future = bootstrap 
      .connect(new InetSocketAddress("localhost", port)); 

    Channel channel = future.awaitUninterruptibly().getChannel(); 
    System.out.println(channel.getId()); 
    channel.getCloseFuture().awaitUninterruptibly(); 
} 

}

는 지금은 인 Netty 클라이언트를 사용할 때 얻을 수있는 이점은 무엇을 알고 싶어? 스레드를 저장합니까?

답변

1

Netty는 스레드를 저장합니다. NettyClient는 연결을 열거 나 닫을 때 동 기적으로 대기 할 때 스레드를 낭비합니다 (awaitUninterruptibly() 호출).

BTW 클라이언트의 연결 수는 몇 개입니까? 고전적인 동기식 1 스레드/연결 방식을 사용해도 충분할까요? 보통 우리는 서버 측에 쓰레드를 저장해야한다.

0

Netty는 소수의 스레드으로 개의 연결을 처리 할 수 ​​있습니다. 클라이언트 응용 프로그램에서 사용하면 소수의 스레드가 서버에 수천 개의 동시 연결을 설정할 수 있습니다.

스레드에 sleep()을 넣었습니다. 절대은 Netty 작업자/보스 스레드를 차단해야합니다. 일회용 차단 작업을 수행해야 할 필요가있는 경우에도 다른 집행자에게 오프로드해야합니다. Netty는 NIO를 사용하며 이전 연결이 입력 버퍼에서 일부 데이터를 가져 오는 동안 동일한 스레드를 사용하여 새 연결을 만들 수 있습니다.

관련 문제