2012-06-23 7 views
5

Zuerst, hier ist ein Hinweis darauf, wo ich alles lesen, was ich weiß, diese Frage jetzt zu: http://docs.jboss.org/netty/3.2/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bind%28%29Netty ServerBootstrap - asynchrone Bindung?

Obwohl nicht explizit von der Dokumentation angegeben, so scheint es, dass ServerBootstrap.bind synchron ist - weil es keine ChannelFuture zurückkehrt, sondern ein Kanal. Wenn das der Fall ist, sehe ich keinen Weg, eine asynchrone Bindung mit der ServerBootstrap Klasse zu machen. Fehle ich etwas oder muss ich meine eigene Lösung rollen lassen?

Mit freundlichen Grüßen

Antwort

4

ich meine eigene Bootstrap-Implementierung mit folgendem Zusatz am Ende rollen:

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; 
}