2012-06-23 56 views
5

首先,這裏的地方我讀了所有的參考我所知道的現在關於這個問題:http://docs.jboss.org/netty/3.2/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bind%28%29Netty ServerBootstrap - 異步綁定?

雖然沒有明確的文件規定,它似乎ServerBootstrap.bind是同步的 - 因爲它不返回ChannelFuture,而是一個頻道。如果是這樣的話,那麼我認爲沒有辦法使用ServerBootstrap類來進行異步綁定。我是否錯過了某些東西,還是必須推出我自己的解決方案?

問候

回答

4

我結束了滾動使用以下除了我自己的自舉的實現:

public ChannelFuture bindAsync(final SocketAddress localAddress) 
{ 
    if (localAddress == null) { 
     throw new NullPointerException("localAddress"); 
    } 
    final BlockingQueue<ChannelFuture> futureQueue = 
     new LinkedBlockingQueue<ChannelFuture>(); 
    ChannelHandler binder = new Binder(localAddress, futureQueue); 
    ChannelHandler parentHandler = getParentHandler(); 
    ChannelPipeline bossPipeline = pipeline(); 
    bossPipeline.addLast("binder", binder); 
    if (parentHandler != null) { 
     bossPipeline.addLast("userHandler", parentHandler); 
    } 
    getFactory().newChannel(bossPipeline); 
    ChannelFuture future = null; 
    boolean interrupted = false; 
    do { 
     try { 
      future = futureQueue.poll(Integer.MAX_VALUE, TimeUnit.SECONDS); 
     } catch (InterruptedException e) { 
      interrupted = true; 
     } 
    } while (future == null); 
    if (interrupted) { 
     Thread.currentThread().interrupt(); 
    } 
    return future; 
}