Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty ServerBootstrap - asynchronous binding?

First, here's a reference to where I read all of what I know now regarding this question: http://docs.jboss.org/netty/3.2/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bind%28%29

Although not explicitly specified by the documentation, it would seem that ServerBootstrap.bind is synchronous - because it does not return a ChannelFuture, but rather a Channel. If that is the case, then I do not see any way to make an asynchronous bind using the ServerBootstrap class. Am I missing something or will I have to roll my own solution?

Best regards

like image 303
Luke A. Leber Avatar asked Oct 08 '22 10:10

Luke A. Leber


1 Answers

I ended up rolling my own bootstrap implementation with the following addition:

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;
}
like image 58
Luke A. Leber Avatar answered Oct 13 '22 01:10

Luke A. Leber